Skip to content

Commit

Permalink
Disallow [UnscopedRef] on explicitly scoped parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Aug 5, 2022
1 parent ab3fe7a commit 66fbae6
Show file tree
Hide file tree
Showing 23 changed files with 290 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7211,4 +7211,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_RuntimeDoesNotSupportRefFields" xml:space="preserve">
<value>Target runtime doesn't support ref fields.</value>
</data>
<data name="ERR_UnscopedScoped" xml:space="preserve">
<value>UnscopedRefAttribute cannot be applied to parameters that have a 'scoped' modifier.</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2113,6 +2113,7 @@ internal enum ErrorCode
ERR_ScopedTypeNameDisallowed = 9062,
ERR_UnscopedRefAttributeUnsupportedTarget = 9063,
ERR_RuntimeDoesNotSupportRefFields = 9064,
ERR_UnscopedScoped = 9066,

#endregion

Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2214,6 +2214,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_ScopedTypeNameDisallowed:
case ErrorCode.ERR_UnscopedRefAttributeUnsupportedTarget:
case ErrorCode.ERR_RuntimeDoesNotSupportRefFields:
case ErrorCode.ERR_UnscopedScoped:
return false;
default:
// NOTE: All error codes must be explicitly handled in this switch statement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ private PEParameterSymbol(

if (_moduleSymbol.Module.HasUnscopedRefAttribute(_handle))
{
if (_moduleSymbol.Module.HasScopedRefAttribute(_handle))
{
isBad = true;
}
scope = DeclarationScope.Unscoped;
}
else if (refKind == RefKind.Out)
Expand Down
5 changes: 2 additions & 3 deletions src/Compilers/CSharp/Portable/Symbols/ParameterSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,12 @@ internal sealed override ObsoleteAttributeData? ObsoleteAttributeData
internal abstract bool HasInterpolatedStringHandlerArgumentError { get; }

/// <summary>
/// The declared scope. From source, this is from the <c>scope</c> keyword
/// and any implicit scope, ignoring any <c>UnscopedRefAttribute</c>.
/// The declared scope. From source, this is from the <c>scope</c> keyword only.
/// </summary>
internal abstract DeclarationScope DeclaredScope { get; }

/// <summary>
/// The effective scope. This is from the declared scope and any
/// The effective scope. This is from the declared scope, implicit scope and any
/// <c>UnscopedRefAttribute</c>.
/// </summary>
internal abstract DeclarationScope EffectiveScope { get; }
Expand Down
5 changes: 0 additions & 5 deletions src/Compilers/CSharp/Portable/Symbols/Source/LambdaSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,6 @@ private ImmutableArray<ParameterSymbol> MakeParameters(
scope = DeclarationScope.Unscoped;
}

if (refKind == RefKind.Out && scope == DeclarationScope.Unscoped)
{
scope = DeclarationScope.RefScoped;
}

var attributeLists = unboundLambda.ParameterAttributes(p);
var name = unboundLambda.ParameterName(p);
var location = unboundLambda.ParameterLocation(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ private static ImmutableArray<TParameterSymbol> MakeParameters<TParameterSyntax,
{
diagnostics.Add(ErrorCode.ERR_ThisInBadContext, thisKeyword.GetLocation());
}
if (refKind == RefKind.Out && scope == DeclarationScope.Unscoped)
{
scope = DeclarationScope.RefScoped;
}

if (parameterSyntax is ParameterSyntax concreteParam)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ internal sealed override DeclarationScope EffectiveScope
{
get
{
var scope = DeclaredScope;
var scope = (RefKind == RefKind.Out && DeclaredScope == DeclarationScope.Unscoped) ? DeclarationScope.RefScoped : DeclaredScope;

if (scope != DeclarationScope.Unscoped &&
HasUnscopedRefAttribute)
{
Expand Down Expand Up @@ -821,6 +822,10 @@ protected override void DecodeWellKnownAttributeImpl(ref DecodeWellKnownAttribut
{
diagnostics.Add(ErrorCode.ERR_UnscopedRefAttributeUnsupportedTarget, arguments.AttributeSyntaxOpt.Location);
}
else if (DeclaredScope != DeclarationScope.Unscoped)
{
diagnostics.Add(ErrorCode.ERR_UnscopedScoped, arguments.AttributeSyntaxOpt.Location);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ internal override ConstantValue DefaultValueFromAttributes
get { return ConstantValue.NotAvailable; }
}

internal override DeclarationScope EffectiveScope => DeclaredScope;
internal override DeclarationScope EffectiveScope
{
get
{
if (RefKind == RefKind.Out && DeclaredScope == DeclarationScope.Unscoped)
{
return DeclarationScope.RefScoped;
}

return DeclaredScope;
}
}
}
}
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/xlf/CSharpResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 66fbae6

Please sign in to comment.