-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for using alias = any_type
.
#50167
Merged
CyrusNajmabadi
merged 85 commits into
dotnet:features/UsingAliasesTypes
from
CyrusNajmabadi:usingAliasType
Feb 3, 2023
Merged
Changes from all commits
Commits
Show all changes
85 commits
Select commit
Hold shift + click to select a range
cdf67db
Add parsing of using aliases that point to types.
CyrusNajmabadi 7acfeba
Update API surface.
CyrusNajmabadi ba290a2
Fix tests
CyrusNajmabadi 4f970f5
Add parsing tests
CyrusNajmabadi 538637f
Add binding support
CyrusNajmabadi b7adad3
Update IDE side.
CyrusNajmabadi b64aeac
Add semantic tests
CyrusNajmabadi 984bf0f
Support functptr types
CyrusNajmabadi b492649
Disallow ref types
CyrusNajmabadi 523de61
Merge remote-tracking branch 'upstream/main' into usingAliasType
CyrusNajmabadi 28c54b4
Merge
CyrusNajmabadi eb37ef3
Merge remote-tracking branch 'upstream/main' into usingAliasType
CyrusNajmabadi dda5b30
Fix
CyrusNajmabadi a314010
Fix
CyrusNajmabadi c67a9a1
move more cases to type
CyrusNajmabadi aa49836
Move error
CyrusNajmabadi 27c0fcd
Add unsafe modifier
CyrusNajmabadi af583bb
Bind with unsafe
CyrusNajmabadi e2a1d78
No nullable reference types
CyrusNajmabadi f90faaa
Update apis
CyrusNajmabadi 0185eac
Add apis
CyrusNajmabadi e6a3b1f
Add api
CyrusNajmabadi bc8ba4d
Add api
CyrusNajmabadi 313b69c
Add api
CyrusNajmabadi 6aeac08
Fix
CyrusNajmabadi 308acb0
NRT
CyrusNajmabadi 288b7c9
NRT
CyrusNajmabadi 0f12baa
NRT
CyrusNajmabadi 6e7cee3
Fix
CyrusNajmabadi f81f89f
Move
CyrusNajmabadi 2b27982
Add error facts
CyrusNajmabadi a5e8b9e
Add error facts
CyrusNajmabadi 9bbda42
Add tests
CyrusNajmabadi 2e6998e
Add tests
CyrusNajmabadi 5dddfc2
Add c# 11 test
CyrusNajmabadi 9e2c969
Add unsafe tests
CyrusNajmabadi 492c9ea
Add unsafe tests
CyrusNajmabadi 74cd663
Dynamic work
CyrusNajmabadi 0fae0b0
Update feature status
CyrusNajmabadi 0eb5ee5
Delete
CyrusNajmabadi e169a14
Update
CyrusNajmabadi e5e06c4
Add comment
CyrusNajmabadi b860116
Fix test
CyrusNajmabadi 972e75a
Fix test
CyrusNajmabadi 54374bd
Fix test
CyrusNajmabadi 42d7759
Add tests
CyrusNajmabadi 82254d4
Add tests
CyrusNajmabadi 28c7cff
Update docs/Language Feature Status.md
CyrusNajmabadi 762bd01
Add tests
CyrusNajmabadi 2c4c046
Add support for top level dynamic
CyrusNajmabadi 9bab6d6
Add support for top level dynamic
CyrusNajmabadi f78bbd7
Fix loc string
CyrusNajmabadi 275a688
Move into 12.0 section
CyrusNajmabadi 5f3a2c4
Add test
CyrusNajmabadi 3a80d86
Add test
CyrusNajmabadi 8dbb1ec
Move errors to binding
CyrusNajmabadi 087b508
Add tests
CyrusNajmabadi 0f25385
Use file scoped namespace
CyrusNajmabadi 73982a7
Allow aliases to nint/nuint.
CyrusNajmabadi ef40e33
Add tests
CyrusNajmabadi 4979ff0
Add tests
CyrusNajmabadi 3cf2096
Add comment
CyrusNajmabadi 07a27aa
Update test
CyrusNajmabadi 6321355
Merge remote-tracking branch 'upstream/features/UsingAliasesTypes' in…
CyrusNajmabadi cb49fae
Merge remote-tracking branch 'upstream/features/UsingAliasesTypes' in…
CyrusNajmabadi 8125da5
'dynamic' should bind to type
CyrusNajmabadi 08279fb
Update docs/Language Feature Status.md
CyrusNajmabadi 3b78a6d
Update tests
CyrusNajmabadi f7593d3
Merge branch 'usingAliasType' of https://github.com/CyrusNajmabadi/ro…
CyrusNajmabadi 50e0ff4
PR feedback
CyrusNajmabadi 3a5efe6
switch to typesymbol
CyrusNajmabadi 149e5b5
Simplify
CyrusNajmabadi e154966
Add tests
CyrusNajmabadi 73531cf
Add tests
CyrusNajmabadi fbf9b8d
Merge remote-tracking branch 'upstream/features/UsingAliasesTypes' in…
CyrusNajmabadi 33ebdbe
Named parameter
CyrusNajmabadi bcbdcd6
Remove unused usings
CyrusNajmabadi 3e1619a
Improve tests
CyrusNajmabadi 8bffc64
Use RegularPreview
CyrusNajmabadi b114890
Update tests
CyrusNajmabadi e056e6b
Fix comment
CyrusNajmabadi 6cc95af
Merge tests
CyrusNajmabadi b5a3ba3
Merge tests
CyrusNajmabadi c9d5b90
Add tests
CyrusNajmabadi 97161ee
Syntax test only
CyrusNajmabadi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -477,10 +477,19 @@ internal NamespaceOrTypeOrAliasSymbolWithAnnotations BindNamespaceOrTypeOrAliasS | |
{ | ||
// ref needs to be handled by the caller | ||
var refTypeSyntax = (RefTypeSyntax)syntax; | ||
var refToken = refTypeSyntax.RefKeyword; | ||
if (!syntax.HasErrors) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_UnexpectedToken, refToken.GetLocation(), refToken.ToString()); | ||
var refToken = refTypeSyntax.RefKeyword; | ||
|
||
// Specialized diagnostic if our parent is a using directive. | ||
if (refTypeSyntax.Parent is UsingDirectiveSyntax) | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_BadRefInUsingAlias, refToken.GetLocation()); | ||
} | ||
else | ||
{ | ||
diagnostics.Add(ErrorCode.ERR_UnexpectedToken, refToken.GetLocation(), refToken.ToString()); | ||
} | ||
} | ||
|
||
return BindNamespaceOrTypeOrAliasSymbol(refTypeSyntax.Type, diagnostics, basesBeingResolved, suppressUseSiteDiagnostics); | ||
|
@@ -830,29 +839,6 @@ private NamespaceOrTypeOrAliasSymbolWithAnnotations BindSimpleNamespaceOrTypeOrA | |
} | ||
} | ||
|
||
private static bool IsViableType(LookupResult result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved to be helper local function in the one place it is used. |
||
{ | ||
if (!result.IsMultiViable) | ||
{ | ||
return false; | ||
} | ||
|
||
foreach (var s in result.Symbols) | ||
{ | ||
switch (s.Kind) | ||
{ | ||
case SymbolKind.Alias: | ||
if (((AliasSymbol)s).Target.Kind == SymbolKind.NamedType) return true; | ||
break; | ||
case SymbolKind.NamedType: | ||
case SymbolKind.TypeParameter: | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected NamespaceOrTypeOrAliasSymbolWithAnnotations BindNonGenericSimpleNamespaceOrTypeOrAliasSymbol( | ||
IdentifierNameSyntax node, | ||
BindingDiagnosticBag diagnostics, | ||
|
@@ -875,7 +861,7 @@ protected NamespaceOrTypeOrAliasSymbolWithAnnotations BindNonGenericSimpleNamesp | |
} | ||
|
||
var errorResult = CreateErrorIfLookupOnTypeParameter(node.Parent, qualifierOpt, identifierValueText, 0, diagnostics); | ||
if ((object)errorResult != null) | ||
if (errorResult is not null) | ||
{ | ||
return TypeWithAnnotations.Create(errorResult); | ||
} | ||
|
@@ -891,23 +877,24 @@ protected NamespaceOrTypeOrAliasSymbolWithAnnotations BindNonGenericSimpleNamesp | |
|
||
// If we were looking up "dynamic" or "nint" at the topmost level and didn't find anything good, | ||
// use that particular type (assuming the /langversion is supported). | ||
if ((object)qualifierOpt == null && | ||
!IsViableType(result)) | ||
if (qualifierOpt is null && | ||
!isViableType(result)) | ||
{ | ||
if (node.Identifier.ValueText == "dynamic") | ||
{ | ||
if ((node.Parent == null || | ||
node.Parent.Kind() != SyntaxKind.Attribute && // dynamic not allowed as attribute type | ||
SyntaxFacts.IsInTypeOnlyContext(node)) && | ||
Compilation.LanguageVersion >= MessageID.IDS_FeatureDynamic.RequiredVersion()) | ||
if (dynamicAllowed()) | ||
{ | ||
bindingResult = Compilation.DynamicType; | ||
ReportUseSiteDiagnosticForDynamic(diagnostics, node); | ||
} | ||
} | ||
else | ||
{ | ||
bindingResult = BindNativeIntegerSymbolIfAny(node, diagnostics); | ||
// nint/nuint is allowed to bind to an existing namespace. | ||
if (!isViableNamespace(result)) | ||
{ | ||
bindingResult = BindNativeIntegerSymbolIfAny(node, diagnostics); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -919,15 +906,80 @@ protected NamespaceOrTypeOrAliasSymbolWithAnnotations BindNonGenericSimpleNamesp | |
if (bindingResult.Kind == SymbolKind.Alias) | ||
{ | ||
var aliasTarget = ((AliasSymbol)bindingResult).GetAliasTarget(basesBeingResolved); | ||
if (aliasTarget.Kind == SymbolKind.NamedType && ((NamedTypeSymbol)aliasTarget).ContainsDynamic()) | ||
if (aliasTarget is TypeSymbol type) | ||
{ | ||
ReportUseSiteDiagnosticForDynamic(diagnostics, node); | ||
if (type.ContainsDynamic()) | ||
{ | ||
ReportUseSiteDiagnosticForDynamic(diagnostics, node); | ||
} | ||
|
||
if (type.IsUnsafe()) | ||
{ | ||
ReportUnsafeIfNotAllowed(node, diagnostics); | ||
} | ||
} | ||
} | ||
} | ||
|
||
result.Free(); | ||
return NamespaceOrTypeOrAliasSymbolWithAnnotations.CreateUnannotated(AreNullableAnnotationsEnabled(node.Identifier), bindingResult); | ||
|
||
bool dynamicAllowed() | ||
{ | ||
if (Compilation.LanguageVersion < MessageID.IDS_FeatureDynamic.RequiredVersion()) | ||
return false; | ||
|
||
if (node.Parent == null) | ||
return true; | ||
|
||
// dynamic not allowed as attribute type | ||
CyrusNajmabadi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (node.Parent.Kind() == SyntaxKind.Attribute) | ||
return false; | ||
|
||
if (SyntaxFacts.IsInTypeOnlyContext(node)) | ||
return true; | ||
|
||
// using X = dynamic; is legal. | ||
if (node.Parent is UsingDirectiveSyntax { Alias: not null }) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
static bool isViableType(LookupResult result) | ||
{ | ||
if (!result.IsMultiViable) | ||
return false; | ||
|
||
foreach (var s in result.Symbols) | ||
{ | ||
switch (s.Kind) | ||
{ | ||
case SymbolKind.Alias: | ||
if (((AliasSymbol)s).Target.Kind == SymbolKind.NamedType) return true; | ||
break; | ||
case SymbolKind.NamedType: | ||
case SymbolKind.TypeParameter: | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
static bool isViableNamespace(LookupResult result) | ||
{ | ||
if (!result.IsMultiViable) | ||
return false; | ||
|
||
foreach (var s in result.Symbols) | ||
{ | ||
if (s.Kind == SymbolKind.Namespace) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
/// <summary> | ||
|
@@ -936,24 +988,26 @@ protected NamespaceOrTypeOrAliasSymbolWithAnnotations BindNonGenericSimpleNamesp | |
/// </summary> | ||
private NamedTypeSymbol BindNativeIntegerSymbolIfAny(IdentifierNameSyntax node, BindingDiagnosticBag diagnostics) | ||
{ | ||
SpecialType specialType; | ||
switch (node.Identifier.Text) | ||
{ | ||
case "nint": | ||
specialType = SpecialType.System_IntPtr; | ||
break; | ||
case "nuint": | ||
specialType = SpecialType.System_UIntPtr; | ||
break; | ||
default: | ||
CyrusNajmabadi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return null; | ||
} | ||
var specialType = | ||
node.IsNint ? SpecialType.System_IntPtr : | ||
node.IsNuint ? SpecialType.System_UIntPtr : SpecialType.None; | ||
|
||
if (specialType == SpecialType.None) | ||
return null; | ||
|
||
switch (node.Parent) | ||
{ | ||
case AttributeSyntax parent when parent.Name == node: // [nint] | ||
return null; | ||
case UsingDirectiveSyntax parent when parent.Name == node: // using nint; using A = nuint; | ||
case UsingDirectiveSyntax usingDirective: | ||
if (usingDirective.Alias != null && usingDirective.Type == node) | ||
{ | ||
// legal to write `using A = nuint;` as long as using-alias-to-type is enabled (checked later). | ||
break; | ||
} | ||
|
||
// `using nint;` not legal where 'nint' has the System.IntPtr meaning. It is legal if you were to | ||
// have `namespace nint { }` somewhere. That is handled though in our caller. | ||
return null; | ||
case ArgumentSyntax parent when // nameof(nint) | ||
(IsInsideNameof && | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
26 changes: 13 additions & 13 deletions
26
src/Compilers/CSharp/Portable/Generated/CSharp.Generated.g4
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this moved from teh parser to here (since there's difficulty representing this in the syntax model). Note: we could just get rid of this specialized error (since we have the more generic one below). But i don't mind keeping this.