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

+39 -13
View File
@@ -81,7 +81,9 @@ struct CalleeState {
};
// State for local pattern matching.
struct LocalState {};
struct LocalState {
bool in_global_init;
};
// State for thunk pattern matching.
struct ThunkState {
@@ -432,6 +434,8 @@ auto MatchContext::DoPostWork(State state,
}
if (specific_id_stack_.size() > 1) {
// TODO: Is this dead code? It's not reached in any of our tests.
// If anything has been pushed onto the specific stack, that means
// that `binding_pattern` is a constant inst. The bindings for constant
// insts are not precomputed (see the documentation for bind_name_map), so
@@ -442,6 +446,9 @@ auto MatchContext::DoPostWork(State state,
return;
}
auto* local_state = std::get_if<LocalState*>(&state);
bool in_global_init = local_state && (*local_state)->in_global_init;
// We're logically consuming this map entry, so we invalidate it in order
// to avoid accidentally consuming it twice.
auto [bind_name_id, type_expr_region_id] =
@@ -449,15 +456,38 @@ auto MatchContext::DoPostWork(State state,
{.bind_name_id = SemIR::InstId::None,
.type_expr_region_id = SemIR::ExprRegionId::None});
if (type_expr_region_id.has_value()) {
// For a global binding, put the type expression in the file block, not in
// the global_init function.
if (in_global_init) {
context_.global_init().Suspend();
}
InsertHere(context_, type_expr_region_id);
if (in_global_init) {
context_.global_init().Resume();
}
}
CARBON_CHECK(bind_name_id.has_value(), "bind_name_map entry used twice");
// For a global binding, put the binding in the file block if it's constant.
// Otherwise it needs to go in the global_init block so that it can see the
// non-constant value.
bool suspend_global_init =
in_global_init && context_.constant_values().Get(value_id).is_constant();
if (suspend_global_init) {
context_.global_init().Suspend();
}
auto bind_name = context_.insts().GetAs<SemIR::AnyBinding>(bind_name_id);
CARBON_CHECK(!bind_name.value_id.has_value());
bind_name.value_id = value_id;
ReplaceInstBeforeConstantUse(context_, bind_name_id, bind_name);
context_.inst_block_stack().AddInstId(bind_name_id);
if (suspend_global_init) {
context_.global_init().Resume();
}
}
// Returns the inst kind to use for the parameter corresponding to the given
@@ -677,13 +707,6 @@ auto MatchContext::DoVarPreWorkImpl(State state,
return scrutinee_id;
}
case CARBON_KIND(LocalState* _): {
// TODO: Find a more efficient way to put these insts in the global_init
// block (or drop the distinction between the global_init block and the
// file scope?)
if (UseGlobalInit(context_)) {
context_.global_init().Resume();
}
// In a `var`/`let` declaration, the `VarStorage` inst is created before
// we start pattern matching.
auto storage_id =
@@ -698,10 +721,6 @@ auto MatchContext::DoVarPreWorkImpl(State state,
AddInst<SemIR::Assign>(context_, SemIR::LocId(entry.pattern_id),
{.lhs_id = storage_id, .rhs_id = init_id});
}
if (UseGlobalInit(context_)) {
context_.global_init().Suspend();
}
return storage_id;
}
case CARBON_KIND(CallerState* _): {
@@ -1395,11 +1414,18 @@ auto CallerPatternMatch(
auto LocalPatternMatch(Context& context, SemIR::LocId loc_id,
SemIR::InstId pattern_id, SemIR::InstId scrutinee_id)
-> void {
LocalState state;
bool use_global_init = UseGlobalInit(context);
if (use_global_init) {
context.global_init().Resume();
}
LocalState state = {.in_global_init = use_global_init};
MatchContext match(context, loc_id);
match.Match(&state,
{.pattern_id = pattern_id,
.work = MatchContext::PreWork{.scrutinee_id = scrutinee_id}});
if (use_global_init) {
context.global_init().Suspend();
}
}
} // namespace Carbon::Check