docs/design/name_lookup.md
Names are always introduced into some scope which defines where they can be
referenced. Many of these scopes are themselves named. For example, types are
named, and name scopes can be defined directly with namespace, see
"Code and name organization".
Unqualified name lookup in Carbon searches the current and enclosing scopes up to the top-level file scope. Names found include:
fn Foo.Bar(...)), the scopes nominated by the
qualifier (Foo and Foo.Bar) act as enclosing scopes for unqualified
name lookup within that declaration's signature and body. This applies
to namespaces, classes, and interfaces.import library "..."), including the implicit import of the API file
from an impl file.Note that a package's name is not injected into the scope of its files, and imports within a single package do not specify the package name. Symbols from other packages must be imported and accessed through their package namespace.
In declarative scopes (such as namespaces, classes, and interfaces), if unqualified name lookup is performed for a name and fails to find it, that name is said to be poisoned in that scope. It is an error if a poisoned name is later introduced in that scope, as that would alter the meaning of earlier lookups. Within a library, poisoned names persist from the API file to implementation files.
In sequential scopes (such as function bodies), redeclaring an entity is disallowed.
The Carbon standard library is in the Core package. A subset of this package,
called the "prelude", is implicitly imported in every file, so the package name
Core is always available.
Some keywords and type literals, such as bool and i32, are aliases for
entities in the prelude. Similarly, some of the Carbon language syntax, such as
operators and for loops, is defined in terms of interfaces in the prelude.
Name shadowing has been discussed in proposals #3763: Matching redeclarations. One rule under consideration is:
Always look in all enclosing scopes, and diagnose an ambiguity if an unqualified name is found in more than one enclosing scope.
This is aligned with the name shadowing discussion in the "low context-sensitivity" principle.
Main package