From 30642927b077eb994f561b6576e9c61e8f65ffb0 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 18 May 2022 16:27:58 -0700 Subject: [PATCH] Fix type of a choice value to be the choice type, not the type-of-choice type. (#1276) --- explorer/interpreter/type_checker.cpp | 16 +++++++--------- explorer/testdata/basic_syntax/choice.carbon | 6 +++--- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/explorer/interpreter/type_checker.cpp b/explorer/interpreter/type_checker.cpp index bb93d554165f..2a52ce39cb0a 100644 --- a/explorer/interpreter/type_checker.cpp +++ b/explorer/interpreter/type_checker.cpp @@ -952,8 +952,7 @@ auto TypeChecker::TypeCheckExp(Nonnull e, << " does not have a field named " << access.field(); } access.set_static_type(arena_->New( - *parameter_types, llvm::None, &aggregate_type, llvm::None, - llvm::None)); + *parameter_types, llvm::None, &choice, llvm::None, llvm::None)); access.set_value_category(ValueCategory::Let); return Success(); } @@ -1740,17 +1739,16 @@ auto TypeChecker::TypeCheckPattern( return CompilationError(alternative.source_loc()) << "alternative pattern does not name a choice type."; } - if (expected) { - CARBON_RETURN_IF_ERROR(ExpectExactType( - alternative.source_loc(), "alternative pattern", *expected, - &alternative.choice_type().static_type())); - } const ChoiceType& choice_type = cast(alternative.choice_type().static_type()) .choice_type(); + if (expected) { + CARBON_RETURN_IF_ERROR(ExpectType(alternative.source_loc(), + "alternative pattern", &choice_type, + *expected)); + } std::optional> parameter_types = - cast(choice_type) - .FindAlternative(alternative.alternative_name()); + choice_type.FindAlternative(alternative.alternative_name()); if (parameter_types == std::nullopt) { return CompilationError(alternative.source_loc()) << "'" << alternative.alternative_name() diff --git a/explorer/testdata/basic_syntax/choice.carbon b/explorer/testdata/basic_syntax/choice.carbon index 4ee9c3ae32ef..0a0a5ab8e484 100644 --- a/explorer/testdata/basic_syntax/choice.carbon +++ b/explorer/testdata/basic_syntax/choice.carbon @@ -18,9 +18,9 @@ choice Ints { } fn Main() -> i32 { - var x: auto = Ints.None(); - var y: auto = Ints.One(42); - var n: auto = 0; + var x: Ints = Ints.None(); + var y: Ints = Ints.One(42); + var n: i32 = 0; match (y) { case Ints.None => n = n + 2;