mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 10:01:06 +01:00
Expect parentheses after an alternative only if they were present in its declaration (#2605)
The design of choice types expects the declaration of an alternative to match the usage: if an alternative is declared as `None`, then it should be used as `None` not `None()`, and if it is declared as `None()` then it should be used as `None()` not `None`. Update explorer to match. Also clean up the handling of choice types and alternative values a little in general, by moving away from identifying choice types and alternatives as strings and towards identifying them symbolically. Closes #2422
This commit is contained in:
+22
-18
@@ -5,7 +5,7 @@
|
||||
// AUTOUPDATE
|
||||
// RUN: %{explorer-run}
|
||||
// RUN: %{explorer-run-trace}
|
||||
// CHECK:STDOUT: result: 0
|
||||
// CHECK:STDOUT: result: 1
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
@@ -15,26 +15,30 @@ choice Ints {
|
||||
Two(i32, i32)
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
var x: Ints = Ints.None();
|
||||
var y: Ints = Ints.One(42);
|
||||
var n: i32 = 0;
|
||||
match (y) {
|
||||
case Ints.None() => { n = n + 2; }
|
||||
case Ints.One(x: auto) => { n = x + 1 - 42; }
|
||||
case Ints.Two(a: auto, b: auto) => { n = 2; }
|
||||
}
|
||||
match (x) {
|
||||
case Ints.One(x: auto) => { n = x + 2; }
|
||||
case Ints.None() => { n = n - 1; }
|
||||
case Ints.Two(x: auto, y: auto) => { n = 5; }
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
// Test some alternate syntaxes
|
||||
choice MoreInts {
|
||||
None(),
|
||||
One(i32),
|
||||
Two(i32, i32),
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
var x: Ints = Ints.None;
|
||||
var y: Ints = Ints.One(42);
|
||||
var z: MoreInts = MoreInts.None();
|
||||
var n: i32 = 0;
|
||||
match (y) {
|
||||
case Ints.None => { n = n + 2; }
|
||||
case Ints.One(x: auto) => { n = x + 1 - 42; }
|
||||
case Ints.Two(a: auto, b: auto) => { n = 2; }
|
||||
}
|
||||
match (x) {
|
||||
case Ints.One(x: auto) => { n = x + 2; }
|
||||
case Ints.None => { n = n - 1; }
|
||||
case Ints.Two(x: auto, y: auto) => { n = 5; }
|
||||
}
|
||||
match (z) {
|
||||
case MoreInts.None() => { ++n; }
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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
|
||||
//
|
||||
// AUTOUPDATE
|
||||
// RUN: %{not} %{explorer-run}
|
||||
// RUN: %{not} %{explorer-run-trace}
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
choice Ints {
|
||||
None,
|
||||
One(i32),
|
||||
Two(i32, i32)
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/basic_syntax/fail_choice_extra_parens.carbon:[[@LINE+1]]: alternative `choice Ints.None` does not expect an argument list
|
||||
match (Ints.None()) {
|
||||
case Ints.None => { return 0; }
|
||||
default => { return 1; }
|
||||
}
|
||||
}
|
||||
@@ -9,17 +9,17 @@
|
||||
package ExplorerTest api;
|
||||
|
||||
choice Ints {
|
||||
None,
|
||||
None(),
|
||||
One(i32),
|
||||
Two(i32, i32)
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
match (Ints.None()) {
|
||||
case Ints.None => { return 0; }
|
||||
match (Ints.None) {
|
||||
case Ints.None() => { return 0; }
|
||||
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/basic_syntax/fail_choice_no_parens.carbon:[[@LINE+3]]: type error in `match` pattern type
|
||||
// CHECK:STDERR: expected: fn () -> choice Ints
|
||||
// CHECK:STDERR: actual: choice Ints
|
||||
// CHECK:STDERR: expected: choice Ints
|
||||
// CHECK:STDERR: actual: fn () -> choice Ints
|
||||
default => { return 1; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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
|
||||
//
|
||||
// AUTOUPDATE
|
||||
// RUN: %{not} %{explorer-run}
|
||||
// RUN: %{not} %{explorer-run-trace}
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
choice Ints {
|
||||
None,
|
||||
One(i32),
|
||||
Two(i32, i32)
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
match (Ints.None) {
|
||||
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/basic_syntax/fail_choice_pattern_extra_parens.carbon:[[@LINE+1]]: alternative `choice Ints.None` does not expect an argument list
|
||||
case Ints.None() => { return 0; }
|
||||
default => { return 1; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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
|
||||
//
|
||||
// AUTOUPDATE
|
||||
// RUN: %{not} %{explorer-run}
|
||||
// RUN: %{not} %{explorer-run-trace}
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
choice Ints {
|
||||
None(),
|
||||
One(i32),
|
||||
Two(i32, i32)
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
match (Ints.None()) {
|
||||
case Ints.None => { return 0; }
|
||||
// CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/basic_syntax/fail_choice_pattern_no_parens.carbon:[[@LINE+3]]: type error in `match` pattern type
|
||||
// CHECK:STDERR: expected: fn () -> choice Ints
|
||||
// CHECK:STDERR: actual: choice Ints
|
||||
default => { return 1; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user