From d9e37da4fb318a6b384aa6d73d41642cf2d6a6e8 Mon Sep 17 00:00:00 2001 From: Manmeet Singh <49202620+Maan2003@users.noreply.github.com> Date: Tue, 11 Apr 2023 01:51:32 +0530 Subject: [PATCH] fix: set StaticScope::NameStatus correctly for Associated Constants (#2752) The StaticScope::NameStatus pattern is not being correctly follow for associated constant deceleration. Closes #2728 Closes #2730 --- explorer/interpreter/resolve_names.cpp | 6 +++++- .../fail_use_assoc_const_before_decl.carbon | 19 +++++++++++++++++++ .../fail_use_assoc_const_in_defn.carbon | 18 ++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 explorer/testdata/interface/fail_use_assoc_const_before_decl.carbon create mode 100644 explorer/testdata/interface/fail_use_assoc_const_in_defn.carbon diff --git a/explorer/interpreter/resolve_names.cpp b/explorer/interpreter/resolve_names.cpp index 8b12c012192a..e4aad27f9bd2 100644 --- a/explorer/interpreter/resolve_names.cpp +++ b/explorer/interpreter/resolve_names.cpp @@ -227,7 +227,9 @@ auto NameResolver::AddExposedNames(const Declaration& declaration, case DeclarationKind::AssociatedConstantDeclaration: { const auto& let = cast(declaration); if (let.binding().name() != AnonymousName) { - CARBON_RETURN_IF_ERROR(enclosing_scope.Add(let.binding().name(), &let)); + CARBON_RETURN_IF_ERROR( + enclosing_scope.Add(let.binding().name(), &let, + StaticScope::NameStatus::KnownButNotDeclared)); } break; } @@ -829,7 +831,9 @@ auto NameResolver::ResolveNames(Declaration& declaration, case DeclarationKind::AssociatedConstantDeclaration: { auto& let = cast(declaration); StaticScope constant_scope(&enclosing_scope); + enclosing_scope.MarkDeclared(let.binding().name()); CARBON_RETURN_IF_ERROR(ResolveNames(let.binding(), constant_scope)); + enclosing_scope.MarkUsable(let.binding().name()); break; } diff --git a/explorer/testdata/interface/fail_use_assoc_const_before_decl.carbon b/explorer/testdata/interface/fail_use_assoc_const_before_decl.carbon new file mode 100644 index 000000000000..754eac6d3fdd --- /dev/null +++ b/explorer/testdata/interface/fail_use_assoc_const_before_decl.carbon @@ -0,0 +1,19 @@ +// 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; + +interface IFace { + // CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/interface/fail_use_assoc_const_before_decl.carbon:[[@LINE+1]]: 'C' has not been declared yet + let B:! C; + let C:! IFace; +} + +fn Main() -> i32 { + // return T; +} diff --git a/explorer/testdata/interface/fail_use_assoc_const_in_defn.carbon b/explorer/testdata/interface/fail_use_assoc_const_in_defn.carbon new file mode 100644 index 000000000000..26648cb0e58f --- /dev/null +++ b/explorer/testdata/interface/fail_use_assoc_const_in_defn.carbon @@ -0,0 +1,18 @@ +// 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; + +interface IFace { + // CHECK:STDERR: COMPILATION ERROR: {{.*}}/explorer/testdata/interface/fail_use_assoc_const_in_defn.carbon:[[@LINE+1]]: 'T' is not usable until after it has been completely declared + let T:! T.IFace; +} + +fn Main() -> i32 { + return 0; +}