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

Correctly set IsRetargetable for source assemblies #55066

Merged
merged 2 commits into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,8 @@ private AssemblyIdentity ComputeIdentity()
VersionHelper.GenerateVersionFromPatternAndCurrentTime(_compilation.Options.CurrentLocalTime, AssemblyVersionAttributeSetting),
this.AssemblyCultureAttributeSetting,
StrongNameKeys.PublicKey,
hasPublicKey: !StrongNameKeys.PublicKey.IsDefault);
hasPublicKey: !StrongNameKeys.PublicKey.IsDefault,
isRetargetable: (AssemblyFlags & AssemblyFlags.Retargetable) == AssemblyFlags.Retargetable);
}

//This maps from assembly name to a set of public keys. It uses concurrent dictionaries because it is built,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -676,5 +676,17 @@ namespace B;
// int x; // 1
Diagnostic(ErrorCode.ERR_NamespaceUnexpected, "x").WithLocation(3, 5));
}

[Fact, WorkItem(54836, "https://github.com/dotnet/roslyn/issues/54836")]
public void AssemblyRetargetableAttributeIsRespected()
{
var code = @"
using System.Reflection;
[assembly: AssemblyFlags(AssemblyNameFlags.Retargetable)]";

var comp = CreateCompilation(code);
Assert.True(comp.Assembly.Identity.IsRetargetable);
Copy link
Member

Choose a reason for hiding this comment

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

If I understand correctly, the main purpose of AssemblyNameFlags.Retargetable is to cause a flag to be emitted in metadata.
FWIW, it looks like that was working correctly despite this bug in AssemblyIdentity (see test AssemblyFlagsAttribute which checks the emitted metadata).

Copy link
Member Author

Choose a reason for hiding this comment

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

AssemblyFlags is just written out directly, it doesn't read from Identity at all when doing so: https://sourceroslyn.io/#Microsoft.CodeAnalysis/PEWriter/MetadataWriter.cs,1969.

comp.VerifyEmitDiagnostics();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ internal AssemblyIdentity(
Version version,
string? cultureName,
ImmutableArray<byte> publicKeyOrToken,
bool hasPublicKey)
bool hasPublicKey,
bool isRetargetable)
{
Debug.Assert(name != null);
Debug.Assert(IsValid(version));
Expand All @@ -168,7 +169,7 @@ internal AssemblyIdentity(
_name = name;
_version = version ?? NullVersion;
_cultureName = NormalizeCultureName(cultureName);
_isRetargetable = false;
_isRetargetable = isRetargetable;
_contentType = AssemblyContentType.Default;
InitializeKey(publicKeyOrToken, hasPublicKey, out _publicKey, out _lazyPublicKeyToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
VersionHelper.GenerateVersionFromPatternAndCurrentTime(_compilation.Options.CurrentLocalTime, AssemblyVersionAttributeSetting),
Me.AssemblyCultureAttributeSetting,
StrongNameKeys.PublicKey,
hasPublicKey:=Not StrongNameKeys.PublicKey.IsDefault)
hasPublicKey:=Not StrongNameKeys.PublicKey.IsDefault,
isRetargetable:=(AssemblyFlags And AssemblyFlags.Retargetable) = AssemblyFlags.Retargetable)

End Function

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,18 @@ BC30560: 'Task' is ambiguous in the namespace 'System.Threading.Tasks'.
</expected>)
End Sub

<Fact, WorkItem(54836, "https://github.com/dotnet/roslyn/issues/54836")>
Public Sub RetargetableAttributeIsRespectedInSource()
Dim code = <![CDATA[
Imports System.Reflection
<Assembly: AssemblyFlags(AssemblyNameFlags.Retargetable)>
]]>

Dim comp = CreateCompilation(code.Value)
Assert.True(comp.Assembly.Identity.IsRetargetable)
AssertTheseEmitDiagnostics(comp)
End Sub

End Class

End Namespace