From 058b5e77c07ba186338a44b7725952b726bb6770 Mon Sep 17 00:00:00 2001 From: pk19604014 <95385881+pk19604014@users.noreply.github.com> Date: Tue, 7 Jun 2022 16:10:55 -0400 Subject: [PATCH] Use is_usable logic for recent continuation and choice recursive name lookup fixes (#1312) --- explorer/interpreter/resolve_names.cpp | 18 ++++++++++-------- .../fail_alternative_uses_choice.carbon | 2 +- .../fail_recursive_continuation.carbon | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/explorer/interpreter/resolve_names.cpp b/explorer/interpreter/resolve_names.cpp index 7399c8e610af..ba78f9792021 100644 --- a/explorer/interpreter/resolve_names.cpp +++ b/explorer/interpreter/resolve_names.cpp @@ -47,8 +47,9 @@ static auto AddExposedNames(const Declaration& declaration, break; } case DeclarationKind::ChoiceDeclaration: { - // Choice name is added to the scope after the choice's alternatives. - // See https://github.com/carbon-language/carbon-lang/issues/1248. + auto& choice = cast(declaration); + CARBON_RETURN_IF_ERROR( + enclosing_scope.Add(choice.name(), &choice, /*usable=*/false)); break; } case DeclarationKind::VariableDeclaration: { @@ -314,13 +315,14 @@ static auto ResolveNames(Statement& statement, StaticScope& enclosing_scope) break; } case StatementKind::Continuation: { + auto& continuation = cast(statement); + CARBON_RETURN_IF_ERROR(enclosing_scope.Add( + continuation.name(), &continuation, /*usable=*/false)); StaticScope continuation_scope; continuation_scope.AddParent(&enclosing_scope); - auto& continuation = cast(statement); - CARBON_RETURN_IF_ERROR( - ResolveNames(continuation.body(), continuation_scope)); - CARBON_RETURN_IF_ERROR( - enclosing_scope.Add(continuation.name(), &continuation)); + CARBON_RETURN_IF_ERROR(ResolveNames(cast(statement).body(), + continuation_scope)); + enclosing_scope.MarkUsable(continuation.name()); break; } case StatementKind::Run: @@ -441,7 +443,7 @@ static auto ResolveNames(Declaration& declaration, StaticScope& enclosing_scope) << "` in choice type"; } } - CARBON_RETURN_IF_ERROR(enclosing_scope.Add(choice.name(), &choice)); + enclosing_scope.MarkUsable(choice.name()); break; } case DeclarationKind::VariableDeclaration: { diff --git a/explorer/testdata/basic_syntax/fail_alternative_uses_choice.carbon b/explorer/testdata/basic_syntax/fail_alternative_uses_choice.carbon index d94101b7286f..c692fc6ef035 100644 --- a/explorer/testdata/basic_syntax/fail_alternative_uses_choice.carbon +++ b/explorer/testdata/basic_syntax/fail_alternative_uses_choice.carbon @@ -11,7 +11,7 @@ package ExplorerTest api; choice Ch { - // CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/basic_syntax/fail_alternative_uses_choice.carbon:[[@LINE+1]]: could not resolve 'Ch' + // CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/basic_syntax/fail_alternative_uses_choice.carbon:[[@LINE+1]]: 'Ch' is not usable until after it has been completely declared Opt(Ch) } diff --git a/explorer/testdata/experimental_continuation/fail_recursive_continuation.carbon b/explorer/testdata/experimental_continuation/fail_recursive_continuation.carbon index 915ea3f24111..c53d5c769885 100644 --- a/explorer/testdata/experimental_continuation/fail_recursive_continuation.carbon +++ b/explorer/testdata/experimental_continuation/fail_recursive_continuation.carbon @@ -12,7 +12,7 @@ package ExplorerTest api; fn f() { __continuation k { - // CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/experimental_continuation/fail_recursive_continuation.carbon:[[@LINE+1]]: could not resolve 'k' + // CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/experimental_continuation/fail_recursive_continuation.carbon:[[@LINE+1]]: 'k' is not usable until after it has been completely declared __run k; } }