Skip to content

Commit

Permalink
Disallow using ScopedRef in source
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Jul 29, 2022
1 parent 5de5410 commit cf4a98a
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 4 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 @@ -7205,4 +7205,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_ScopedTypeNameDisallowed" xml:space="preserve">
<value>Types and aliases cannot be named 'scoped'.</value>
</data>
<data name="ERR_ExplicitScopedRef" xml:space="preserve">
<value>Do not use 'System.Runtime.CompilerServices.ScopedRefAttribute'. Use the 'scoped' keyword instead.</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 @@ -2111,6 +2111,7 @@ internal enum ErrorCode
ERR_CannotMatchOnINumberBase = 9060,
ERR_MisplacedScoped = 9061,
ERR_ScopedTypeNameDisallowed = 9062,
ERR_ExplicitScopedRef = 9063,

#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 @@ -2212,6 +2212,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_CannotMatchOnINumberBase:
case ErrorCode.ERR_MisplacedScoped:
case ErrorCode.ERR_ScopedTypeNameDisallowed:
case ErrorCode.ERR_ExplicitScopedRef:
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 @@ -748,7 +748,8 @@ protected override void DecodeWellKnownAttributeImpl(ref DecodeWellKnownAttribut
ReservedAttributes.IsByRefLikeAttribute |
ReservedAttributes.TupleElementNamesAttribute |
ReservedAttributes.NullableAttribute |
ReservedAttributes.NativeIntegerAttribute))
ReservedAttributes.NativeIntegerAttribute |
ReservedAttributes.ScopedRefAttribute))
{
}
else if (attribute.IsTargetAttribute(this, AttributeDescription.AllowNullAttribute))
Expand Down
7 changes: 7 additions & 0 deletions src/Compilers/CSharp/Portable/Symbols/Symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,7 @@ internal enum ReservedAttributes
NativeIntegerAttribute = 1 << 9,
CaseSensitiveExtensionAttribute = 1 << 10,
RequiredMemberAttribute = 1 << 11,
ScopedRefAttribute = 1 << 12,
}

internal bool ReportExplicitUseOfReservedAttributes(in DecodeWellKnownAttributeArguments<AttributeSyntax, CSharpAttributeData, AttributeLocation> arguments, ReservedAttributes reserved)
Expand Down Expand Up @@ -1438,6 +1439,12 @@ internal bool ReportExplicitUseOfReservedAttributes(in DecodeWellKnownAttributeA
// Do not use 'System.Runtime.CompilerServices.RequiredMemberAttribute'. Use the 'required' keyword on required fields and properties instead.
diagnostics.Add(ErrorCode.ERR_ExplicitRequiredMember, arguments.AttributeSyntaxOpt.Location);
}
else if ((reserved & ReservedAttributes.ScopedRefAttribute) != 0 &&
attribute.IsTargetAttribute(this, AttributeDescription.ScopedRefAttribute))
{
// Do not use 'System.Runtime.CompilerServices.ScopedRefAttribute'. Use the 'scoped' keyword instead.
diagnostics.Add(ErrorCode.ERR_ExplicitScopedRef, arguments.AttributeSyntaxOpt.Location);
}
else
{
return false;
Expand Down
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.

Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,17 @@ public static void Main([ScopedRef] string[] args)
}
}";
var comp = CreateCompilation(new[] { ScopedRefAttributeDefinition, source });
// https://github.com/dotnet/roslyn/issues/62124: Re-enable check for ScopedRefAttribute in ReportExplicitUseOfReservedAttributes.
comp.VerifyDiagnostics();
comp.VerifyDiagnostics(
// (2,18): error CS9063: Do not use 'System.Runtime.CompilerServices.ScopedRefAttribute'. Use the 'scoped' keyword instead.
// delegate void D([ScopedRef] ref int i);
Diagnostic(ErrorCode.ERR_ExplicitScopedRef, "ScopedRef").WithLocation(2, 18),
// (5,30): error CS9063: Do not use 'System.Runtime.CompilerServices.ScopedRefAttribute'. Use the 'scoped' keyword instead.
// public static void Main([ScopedRef] string[] args)
Diagnostic(ErrorCode.ERR_ExplicitScopedRef, "ScopedRef").WithLocation(5, 30),
// (7,17): error CS9063: Do not use 'System.Runtime.CompilerServices.ScopedRefAttribute'. Use the 'scoped' keyword instead.
// D d = ([ScopedRef] ref int i) => { };
Diagnostic(ErrorCode.ERR_ExplicitScopedRef, "ScopedRef").WithLocation(7, 17)
);
}

[WorkItem(62124, "https://github.com/dotnet/roslyn/issues/62124")]
Expand Down Expand Up @@ -140,7 +149,6 @@ public sealed class ScopedRefAttribute : Attribute
}
";
var comp = CreateCompilation(source);
// https://github.com/dotnet/roslyn/issues/62124: Re-enable check for ScopedRefAttribute in ReportExplicitUseOfReservedAttributes.
comp.VerifyDiagnostics(
// (6,24): warning CS0169: The field 'Program.F' is never used
// [ScopedRef] object F;
Expand All @@ -150,6 +158,33 @@ public sealed class ScopedRefAttribute : Attribute
Diagnostic(ErrorCode.WRN_UnreferencedEvent, "E").WithArguments("Program.E").WithLocation(7, 36));
}

[WorkItem(62124, "https://github.com/dotnet/roslyn/issues/62124")]
[Fact]
public void ExplicitAttribute_ReferencedInSource_03()
{
var source = @"
using System.Runtime.CompilerServices;
record struct R1([ScopedRef] ref int i);
record struct R2([ScopedRef] R i);
ref struct R { }
";
var comp = CreateCompilation(new[] { ScopedRefAttributeDefinition, source });
comp.VerifyDiagnostics(
// (3,19): error CS9063: Do not use 'System.Runtime.CompilerServices.ScopedRefAttribute'. Use the 'scoped' keyword instead.
// record struct R1([ScopedRef] ref int i);
Diagnostic(ErrorCode.ERR_ExplicitScopedRef, "ScopedRef").WithLocation(3, 19),
// (3,30): error CS0631: ref and out are not valid in this context
// record struct R1([ScopedRef] ref int i);
Diagnostic(ErrorCode.ERR_IllegalRefParam, "ref").WithLocation(3, 30),
// (4,19): error CS9063: Do not use 'System.Runtime.CompilerServices.ScopedRefAttribute'. Use the 'scoped' keyword instead.
// record struct R2([ScopedRef] R i);
Diagnostic(ErrorCode.ERR_ExplicitScopedRef, "ScopedRef").WithLocation(4, 19),
// (4,30): error CS8345: Field or auto-implemented property cannot be of type 'R' unless it is an instance member of a ref struct.
// record struct R2([ScopedRef] R i);
Diagnostic(ErrorCode.ERR_FieldAutoPropCantBeByRefLike, "R").WithArguments("R").WithLocation(4, 30)
);
}

[Fact]
public void ExplicitAttribute_UnexpectedParameterTargets()
{
Expand Down

0 comments on commit cf4a98a

Please sign in to comment.