Skip to content

Commit

Permalink
Ref fields feature requires ByRefFields runtime flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv committed Jul 26, 2022
1 parent 1d72c32 commit fb5fcda
Show file tree
Hide file tree
Showing 26 changed files with 405 additions and 182 deletions.
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 @@ -7196,4 +7196,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="IDS_PointerElementAccess" xml:space="preserve">
<value>pointer element access</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 @@ -2109,6 +2109,7 @@ internal enum ErrorCode
ERR_FeatureNotAvailableInVersion11 = 9058,
ERR_RefFieldInNonRefStruct = 9059,
ERR_CannotMatchOnINumberBase = 9060,
ERR_RuntimeDoesNotSupportRefFields = 9061,

#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 @@ -2209,6 +2209,7 @@ internal static bool IsBuildOnlyDiagnostic(ErrorCode code)
case ErrorCode.ERR_RefFieldInNonRefStruct:
case ErrorCode.WRN_AnalyzerReferencesNewerCompiler:
case ErrorCode.ERR_CannotMatchOnINumberBase:
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
{
// 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
15 changes: 15 additions & 0 deletions src/Compilers/CSharp/Portable/Symbols/MissingCorLibrarySymbol.cs
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
Debug.Assert((object)CorLibrary == this);
return false;
}
set
{
Debug.Assert((object)CorLibrary == this);
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.

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 fb5fcda

Please sign in to comment.