Resolve using declared before the file namespace from the global namespace - #11552
Open
timotheeguerin wants to merge 3 commits into
Open
Resolve using declared before the file namespace from the global namespace#11552timotheeguerin wants to merge 3 commits into
using declared before the file namespace from the global namespace#11552timotheeguerin wants to merge 3 commits into
Conversation
β¦m the global namespace A `using` written above a blockless namespace declaration was resolved as if it were inside the file namespace, so `using TypeSpec.Http;` in a file with `namespace _Specs_.TypeSpec.Foo;` bound `TypeSpec` to `_Specs_.TypeSpec`. Match C#: usings declared before the file namespace resolve from the global namespace, while those declared after it (or inside a namespace block) keep resolving relative to that namespace. Fixes microsoft#1840
commit: |
Contributor
|
All changed packages have been documented.
Show changes
|
|
You can try these changes here
|
timotheeguerin
marked this pull request as ready for review
August 5, 2026 20:32
timotheeguerin
requested review from
catalinaperalta,
iscai-msft,
markcowl and
xirzec
as code owners
August 5, 2026 20:32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Writing a
usingabove a file-level namespace silently changed what it meant. Because theusingwas resolved as if it lived inside the file namespace, this failed:TypeSpecbound to_Specs_.TypeSpecinstead of the globalTypeSpec, even though theusingis written before the namespace declaration. Switching to a block namespace made it work, which is confusing.Our namespace system is modelled on C#, and C# does not have this problem: a
usingbefore a file-scoped namespace is compilation-unit level and resolves from the global namespace; ausingafter it lives inside the namespace and resolves relatively. This PR adopts the same rule.Breaking change
A relative name in a
usingwritten above the file namespace now needs to be fully qualified, otherwise it reportsUnknown identifier.Follow-ups
usingbefore the file namespace fails to resolveΒ #11560: a dedicated diagnostic with a quick fix suggesting the fully qualified name for the breaking case above.global::, TypeSpec has no equivalent.Fixes #1840