Handle references to global lets and enclosing locals. (#8061)

When building an expression referencing the result of name lookup, if
the lookup result is non-constant then check we can access it from the
current scope.

* If it is a global, then use a new `GlobalRef` instruction to get the
value of that global. In lowering, create global variables to hold the
values of global `let` bindings so that `GlobalRef` can access those
values.
* If it is a local, walk the lexical scopes between it and the point of
use and ensure it is usable through from all of them. Error if there's
an intervening declarative scope such as a class, add a TODO to handle
captures for intervening functions, and allow `typeof(...)` to name
things it can't read the value of since it's unevaluated.

Global `let`s (which includes `let`s at class scope) now also have their
initializers collected into the global inits block. If the initializer
is non-constant, the wrapper binding also lives in the global inits
block; otherwise it continues to live in the file block and the variable
emitted for it is a global constant.

Assisted-by: Gemini and Claude via Antigravity
This commit is contained in:
Richard Smith authored and GitHub committed 2026-11-06 02:53:03 +00:00
1 parent bb6486b553
commit d50576ca16
94 files changed
+3394 -1293

No files matched your search

+10 -2
View File
@@ -8,6 +8,7 @@
#include "toolchain/check/convert.h"
#include "toolchain/check/cpp/export.h"
#include "toolchain/check/eval.h"
#include "toolchain/check/full_pattern_stack.h"
#include "toolchain/check/function.h"
#include "toolchain/check/generic.h"
#include "toolchain/check/impl.h"
@@ -457,19 +458,26 @@ auto ComputeClassObjectRepr(Context& context, Parse::ClassDefinitionId node_id,
}
auto InNonStaticFieldDecl(Context& context) -> bool {
return context.full_pattern_stack().IsCurrentKindClassScopeVarDecl() &&
return context.full_pattern_stack().CurrentKindIs(
FullPatternStack::Kind::ClassScopeVarDecl) &&
!context.decl_introducer_state_stack()
.innermost()
.modifier_set.HasAnyOf(KeywordModifierSet::Static);
}
auto InStaticClassScopeVar(Context& context) -> bool {
return context.full_pattern_stack().IsCurrentKindClassScopeVarDecl() &&
return context.full_pattern_stack().CurrentKindIs(
FullPatternStack::Kind::ClassScopeVarDecl) &&
context.decl_introducer_state_stack()
.innermost()
.modifier_set.HasAnyOf(KeywordModifierSet::Static);
}
auto InClassScopeLet(Context& context) -> bool {
return context.full_pattern_stack().CurrentKindIs(
FullPatternStack::Kind::ClassScopeLetDecl);
}
auto LookupClassFieldByStructField(Context& context,
const SemIR::NameScope& class_scope,
const SemIR::StructTypeField& struct_field)