From 7670f08200f8edd29bcbe0f86dcade11689beca9 Mon Sep 17 00:00:00 2001 From: Geoff Romer Date: Mon, 18 Oct 2021 15:41:33 -0700 Subject: [PATCH] Additional test cases for returning from `match` (#897) Requested during review of #882 --- .../function/fail_match_no_return.carbon | 20 +++++++++++++++++++ .../function/fail_match_partial_return.carbon | 20 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 executable_semantics/testdata/function/fail_match_no_return.carbon create mode 100644 executable_semantics/testdata/function/fail_match_partial_return.carbon diff --git a/executable_semantics/testdata/function/fail_match_no_return.carbon b/executable_semantics/testdata/function/fail_match_no_return.carbon new file mode 100644 index 000000000000..9431a79a153d --- /dev/null +++ b/executable_semantics/testdata/function/fail_match_no_return.carbon @@ -0,0 +1,20 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// RUN: not executable_semantics %s 2>&1 | \ +// RUN: FileCheck --match-full-lines --allow-unused-prefixes=false %s +// RUN: not executable_semantics --trace %s 2>&1 | \ +// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s +// AUTOUPDATE: executable_semantics %s +// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/function/fail_match_no_return.carbon:17: control-flow reaches end of function that provides a `->` return type without reaching a return statement + +package ExecutableSemanticsTest api; + +fn main() -> i32 { + var x: i32 = 0; + match(0) { + case 1 => { x = 1; } + case _: auto => { x = 2; } + } +} diff --git a/executable_semantics/testdata/function/fail_match_partial_return.carbon b/executable_semantics/testdata/function/fail_match_partial_return.carbon new file mode 100644 index 000000000000..f5cbfa3339e4 --- /dev/null +++ b/executable_semantics/testdata/function/fail_match_partial_return.carbon @@ -0,0 +1,20 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// RUN: not executable_semantics %s 2>&1 | \ +// RUN: FileCheck --match-full-lines --allow-unused-prefixes=false %s +// RUN: not executable_semantics --trace %s 2>&1 | \ +// RUN: FileCheck --match-full-lines --allow-unused-prefixes %s +// AUTOUPDATE: executable_semantics %s +// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/function/fail_match_partial_return.carbon:17: control-flow reaches end of function that provides a `->` return type without reaching a return statement + +package ExecutableSemanticsTest api; + +fn main() -> i32 { + var x: i32 = 0; + match (0) { + case 1 => { x = 1; } + case _: auto => { return 0; } + } +}