Skip to content

What restrictions should be applied to aliases? #6767

@zygoloid

Description

@zygoloid

Summary of issue:

We currently have contradictory design documentation for aliases. The top-level design page says:

Note that the right-hand side of the equal sign (=) is a name not a value, so alias four = 4; is not allowed. This allows alias to work with entities like namespaces, which aren’t values in Carbon.

Whereas the specific documentation on aliases says:

Carbon provides a fully general name aliasing facility to declare a new name as an alias for a value; everything is a value in Carbon. This is a fully general facility because everything is a value in Carbon, including types.

... which would allow alias four = 4;.

Previously in leads issue #749, we had decided on the "aliases are only for names" rule; while we did not clearly define what a "name" is, we did say that Int(32) isn't one.

The toolchain implements a third rule, that the right-hand side of an alias must either be a type literal instruction or a name reference. This allows alias X = Int(32).PromotedType; but not alias X = Int(32);, and allows alias X = type; but not alias X = bool; (rewritten to Core.Bool()) nor alias X = i32; (rewritten to i32).

So, what rules do we want?

Details:

In various parts of our design, we are currently using alias syntax for the following:

  • Aliasing a namespace
  • Aliasing a package
  • Aliasing a function
  • Aliasing a method of an interface into a class
  • Aliasing a type

Presumably we want the above to all work.

For C++ migration, it would also be good if we had a consistent story for how to migrate C++'s

using MyT = YourScope::YourT;

and

using YourScope::YourName;

into Carbon. Currently the story we have is that you use an alias if there are no template arguments involved, and otherwise presumably you would use a let template... declaration once that's supported. That does not seem like a good story.

Any other information that you want to share?

We once had an idea that it would be good if alias expansion could be done without any additional semantic information. I believe the intent was to support a really simple mapping from names to the named entity, in order to allow simple tools to accurately understand some portion of the Carbon semantics. In particular, at the time we thought we would allow aliases to be used when redeclaring entities, so that redeclarations of the same entity could differ in whether an alias was used. We have since decided against that; declarations of an entity must use the One True Name of the entity and all of its enclosing scopes. So the restriction to pure names may no longer be justified -- and indeed because aliases can be imported, I'm not sure the restriction ever really bought us anything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    examples-2026An issue that is relevant for examples we are trying to port to Carbon in 2026.leads questionA question for the leads team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions