mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Relax alias restrictions. (#7190)
Implement the alias rules from proposal #5389, wherein an alias is permitted so long as the target has a constant value. While that proposal is not yet accepted, this seems like a reasonable basis for further iteration, and will be useful for the examples we're currently pursuing.
This commit is contained in:
@@ -46,29 +46,16 @@ auto HandleParseNode(Context& context, Parse::AliasId /*node_id*/) -> bool {
|
||||
{.name_id = name_context.name_id_for_new_inst(),
|
||||
.parent_scope_id = name_context.parent_scope_id});
|
||||
|
||||
auto alias_type_id = SemIR::TypeId::None;
|
||||
auto alias_value_id = SemIR::InstId::None;
|
||||
if (auto inst = context.insts().TryGetAs<SemIR::NameRef>(expr_id)) {
|
||||
// Pass through name references, albeit changing the name in use.
|
||||
alias_type_id = inst->type_id;
|
||||
alias_value_id = inst->value_id;
|
||||
} else if (auto inst =
|
||||
context.insts().TryGetAs<SemIR::TypeLiteral>(expr_id)) {
|
||||
// Treat type literals such as `type` or `bool` like name references.
|
||||
alias_type_id = inst->type_id;
|
||||
alias_value_id = inst->value_id;
|
||||
} else {
|
||||
CARBON_DIAGNOSTIC(AliasRequiresNameRef, Error,
|
||||
"alias initializer must be a name reference");
|
||||
context.emitter().Emit(expr_node, AliasRequiresNameRef);
|
||||
alias_type_id = SemIR::ErrorInst::TypeId;
|
||||
alias_value_id = SemIR::ErrorInst::InstId;
|
||||
if (!context.constant_values().Get(expr_id).is_constant()) {
|
||||
CARBON_DIAGNOSTIC(AliasRequiresConstantValue, Error,
|
||||
"alias refers to a runtime value");
|
||||
context.emitter().Emit(expr_node, AliasRequiresConstantValue);
|
||||
}
|
||||
auto alias_id =
|
||||
AddInst<SemIR::AliasBinding>(context, name_context.loc_id,
|
||||
{.type_id = alias_type_id,
|
||||
.entity_name_id = entity_name_id,
|
||||
.value_id = alias_value_id});
|
||||
auto alias_id = AddInst<SemIR::AliasBinding>(
|
||||
context, name_context.loc_id,
|
||||
{.type_id = context.insts().Get(expr_id).type_id(),
|
||||
.entity_name_id = entity_name_id,
|
||||
.value_id = expr_id});
|
||||
|
||||
// Add the name of the binding to the current scope.
|
||||
context.decl_name_stack().PopScope();
|
||||
|
||||
Reference in New Issue
Block a user