Skip to content
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

Ref fields feature requires ByRefFields runtime flag #62941

Merged
merged 12 commits into from
Aug 5, 2022
4 changes: 4 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7260,6 +7260,10 @@ protected BoundExpression BindFieldAccess(
fieldSymbol.RefKind != RefKind.None)
{
CheckFeatureAvailability(node, MessageID.IDS_FeatureRefFields, diagnostics);
if (!Compilation.Assembly.RuntimeSupportsByRefFields)
{
diagnostics.Add(ErrorCode.ERR_RuntimeDoesNotSupportRefFields, node.Location);
}
}

TypeSymbol fieldType = fieldSymbol.GetFieldType(this.FieldsBeingBound).Type;
Expand Down
3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -7208,4 +7208,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_UnscopedRefAttributeUnsupportedTarget" xml:space="preserve">
<value>UnscopedRefAttribute can only be applied to 'out' parameters, 'ref' parameters that refer to 'ref struct' types, and instance methods and properties on 'struct' types other than constructors and 'init' accessors.</value>
</data>
<data name="ERR_RuntimeDoesNotSupportRefFields" xml:space="preserve">
<value>Target runtime doesn't support ref fields.</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 @@ -2112,6 +2112,7 @@ internal enum ErrorCode
ERR_MisplacedScoped = 9061,
ERR_ScopedTypeNameDisallowed = 9062,
ERR_UnscopedRefAttributeUnsupportedTarget = 9063,
ERR_RuntimeDoesNotSupportRefFields = 9064,

#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 @@ -2213,6 +2213,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_MisplacedScoped:
case ErrorCode.ERR_ScopedTypeNameDisallowed:
case ErrorCode.ERR_UnscopedRefAttributeUnsupportedTarget:
case ErrorCode.ERR_RuntimeDoesNotSupportRefFields:
return false;
default:
// NOTE: All error codes must be explicitly handled in this switch statement
Expand Down
15 changes: 13 additions & 2 deletions src/Compilers/CSharp/Portable/Symbols/AssemblySymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,19 @@ protected bool RuntimeSupportsFeature(SpecialMember feature)
internal bool RuntimeSupportsUnmanagedSignatureCallingConvention
=> RuntimeSupportsFeature(SpecialMember.System_Runtime_CompilerServices_RuntimeFeature__UnmanagedSignatureCallingConvention);

internal bool RuntimeSupportsByRefFields
=> RuntimeSupportsFeature(SpecialMember.System_Runtime_CompilerServices_RuntimeFeature__ByRefFields);
internal virtual bool RuntimeSupportsByRefFields
{
get
Copy link
Member

@333fred 333fred Aug 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: please use consistent style between these two one-line methods :). #Resolved

{
// CorLibrary should never be null, but that invariant is broken in some cases for MissingAssemblySymbol.
// Tracked by https://github.com/dotnet/roslyn/issues/61262
return CorLibrary?.RuntimeSupportsByRefFields == true;
}

// The setter should be removed once TargetFramework.Net70 is added
// Tracked by https://github.com/dotnet/roslyn/issues/61463
set => CorLibrary.RuntimeSupportsByRefFields = value;
}

/// <summary>
/// True if the target runtime support covariant returns of methods declared in classes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal abstract class MetadataOrSourceAssemblySymbol

private NativeIntegerTypeSymbol[] _lazyNativeIntegerTypes;
private ThreeState _lazyRuntimeSupportsNumericIntPtr = ThreeState.Unknown;
private ThreeState _lazyRuntimeSupportsByRefFields = ThreeState.Unknown;

internal override bool RuntimeSupportsNumericIntPtr
{
Expand Down Expand Up @@ -65,6 +66,37 @@ internal override bool RuntimeSupportsNumericIntPtr
}
}

internal override bool RuntimeSupportsByRefFields
{
get
{
if ((object)CorLibrary == this)
{
if (!_lazyRuntimeSupportsByRefFields.HasValue())
{
_lazyRuntimeSupportsByRefFields = RuntimeSupportsFeature(SpecialMember.System_Runtime_CompilerServices_RuntimeFeature__ByRefFields).ToThreeState();
}

return _lazyRuntimeSupportsByRefFields.Value();
}

return base.RuntimeSupportsByRefFields;
}
set
{
Debug.Assert(value);
Debug.Assert(!RuntimeSupportsFeature(SpecialMember.System_Runtime_CompilerServices_RuntimeFeature__ByRefFields));
if ((object)CorLibrary == this)
{
Debug.Assert(!_lazyRuntimeSupportsByRefFields.HasValue());
_lazyRuntimeSupportsByRefFields = value.ToThreeState();
return;
}

base.RuntimeSupportsByRefFields = value;
}
}

/// <summary>
/// Lookup declaration for predefined CorLib type in this Assembly.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,20 @@ internal override bool RuntimeSupportsNumericIntPtr
throw ExceptionUtilities.Unreachable;
}
}

internal override bool RuntimeSupportsByRefFields
{
get
{
// For now we assume that it is not supported by default
333fred marked this conversation as resolved.
Show resolved Hide resolved
Debug.Assert((object)CorLibrary == this);
return false;
}
set
{
Debug.Assert((object)CorLibrary == this);
333fred marked this conversation as resolved.
Show resolved Hide resolved
throw ExceptionUtilities.Unreachable;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,11 @@ private TypeAndRefKind GetTypeAndRefKind(ConsList<FieldSymbol> fieldsBeingBound)
if (refKind != RefKind.None)
{
MessageID.IDS_FeatureRefFields.CheckFeatureAvailability(diagnostics, compilation, typeSyntax.Location);
if (!compilation.Assembly.RuntimeSupportsByRefFields)
{
diagnostics.Add(ErrorCode.ERR_RuntimeDoesNotSupportRefFields, ErrorLocation);
}

if (!containingType.IsRefLikeType)
{
diagnostics.Add(ErrorCode.ERR_RefFieldInNonRefStruct, ErrorLocation);
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.

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

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

Loading