提交 84685f57 编写于 作者: N nmgafter

1090472-Ensure retargeted error symbols cause use-site errors

Also includes VB implementation
 (changeset 1383936)
上级 75c73cc1
......@@ -3544,6 +3544,15 @@ internal class CSharpResources {
}
}
/// <summary>
/// Looks up a localized string similar to There is an error in a referenced assembly &apos;{0}&apos;..
/// </summary>
internal static string ERR_ErrorInReferencedAssembly {
get {
return ResourceManager.GetString("ERR_ErrorInReferencedAssembly", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to A &apos;{0}&apos; character may only be escaped by doubling &apos;{0}{0}&apos; in an interpolated string..
/// </summary>
......
......@@ -4014,4 +4014,7 @@
<data name="ERR_EmptyFormatSpecifier" xml:space="preserve">
<value>Empty format specifier.</value>
</data>
<data name="ERR_ErrorInReferencedAssembly" xml:space="preserve">
<value>There is an error in a referenced assembly '{0}'.</value>
</data>
</root>
......@@ -1301,5 +1301,6 @@ internal enum ErrorCode
ERR_EscapedCurly = 8087,
ERR_TrailingWhitespaceInFormatSpecifier = 8088,
ERR_EmptyFormatSpecifier = 8089,
ERR_ErrorInReferencedAssembly = 8090,
}
}
......@@ -50,6 +50,18 @@ internal ExtendedErrorTypeSymbol(NamespaceOrTypeSymbol containingSymbol, string
this.resultKind = LookupResultKind.Empty;
}
private ExtendedErrorTypeSymbol(NamespaceOrTypeSymbol containingSymbol, string name, int arity, DiagnosticInfo errorInfo, bool unreported, bool variableUsedBeforeDeclaration, ImmutableArray<Symbol> candidateSymbols, LookupResultKind resultKind)
{
this.name = name;
this.errorInfo = errorInfo;
this.containingSymbol = containingSymbol;
this.arity = arity;
this.unreported = unreported;
this.VariableUsedBeforeDeclaration = variableUsedBeforeDeclaration;
this.candidateSymbols = candidateSymbols;
this.resultKind = resultKind;
}
internal ExtendedErrorTypeSymbol(NamespaceOrTypeSymbol guessSymbol, LookupResultKind resultKind, DiagnosticInfo errorInfo, bool unreported = false)
: this(guessSymbol.ContainingNamespaceOrType(), guessSymbol, resultKind, errorInfo, unreported)
{
......@@ -68,6 +80,12 @@ internal ExtendedErrorTypeSymbol(NamespaceOrTypeSymbol containingSymbol, Immutab
Debug.Assert(candidateSymbols.IsEmpty || resultKind != LookupResultKind.Viable, "Shouldn't use LookupResultKind.Viable with candidate symbols");
}
internal ExtendedErrorTypeSymbol AsUnreported()
{
return this.Unreported ? this :
new ExtendedErrorTypeSymbol(containingSymbol, name, arity, errorInfo, true, VariableUsedBeforeDeclaration, candidateSymbols, resultKind);
}
private static ImmutableArray<Symbol> UnwrapErrorCandidates(ImmutableArray<Symbol> candidateSymbols)
{
var candidate = candidateSymbols.IsEmpty ? null : candidateSymbols[0] as ErrorTypeSymbol;
......
......@@ -699,8 +699,20 @@ public PointerTypeSymbol Retarget(PointerTypeSymbol type)
public static ErrorTypeSymbol Retarget(ErrorTypeSymbol type)
{
// TODO: produce an error symbol that will trigger an error on use
return type;
// TODO: if it is a missing symbol error but no longer missing in the target assembly, then we can resolve it here.
var useSiteDiagnostic = type.GetUseSiteDiagnostic();
if (useSiteDiagnostic != null)
{
return type;
}
// A retargeted error symbol must trigger an error on use so that a dependent compilation won't
// improperly succeed. We therefore ensure we have a use-site diagnostic.
return
(type as ExtendedErrorTypeSymbol)?.AsUnreported() ?? // preserve diagnostic information if possible
new ExtendedErrorTypeSymbol(type, type.ResultKind,
type.ErrorInfo ?? new CSDiagnosticInfo(ErrorCode.ERR_ErrorInReferencedAssembly, type.ContainingAssembly?.Identity.GetDisplayName() ?? string.Empty), true);
}
public ImmutableArray<Symbol> Retarget(ImmutableArray<Symbol> arr)
......
......@@ -4880,8 +4880,12 @@ public void M1(ITest34 y)
";
DiagnosticDescription[] expected = {
// error CS0246: The type or namespace name 'ITest33' could not be found (are you missing a using directive or an assembly reference?)
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound).WithArguments("ITest33")
// error CS0246: The type or namespace name 'ITest33' could not be found (are you missing a using directive or an assembly reference?)
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound).WithArguments("ITest33").WithLocation(1, 1),
// error CS0246: The type or namespace name 'ITest33' could not be found (are you missing a using directive or an assembly reference?)
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound).WithArguments("ITest33").WithLocation(1, 1),
// error CS0246: The type or namespace name 'ITest33' could not be found (are you missing a using directive or an assembly reference?)
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound).WithArguments("ITest33").WithLocation(1, 1)
};
var compilation1 = CreateCompilationWithMscorlib(consumer, options: TestOptions.ReleaseExe,
......@@ -4933,7 +4937,7 @@ public void M1(ITest34 y)
{
// (10,9): error CS0246: The type or namespace name 'ITest33' could not be found (are you missing a using directive or an assembly reference?)
// y.M2(null);
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "y.M2(null)").WithArguments("ITest33")
Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "y.M2").WithArguments("ITest33").WithLocation(10, 9)
};
VerifyEmitDiagnostics(compilation1, true, expected);
......
......@@ -1672,6 +1672,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
ERR_ExpressionDoesntHaveName = 37244
ERR_InvalidNameOfSubExpression = 37245
ERR_MethodTypeArgsUnexpected = 37246
ERR_InReferencedAssembly = 37247
ERR_LastPlusOne
......
......@@ -771,15 +771,17 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols.Retargeting
End Function
Public Function Retarget(type As ErrorTypeSymbol) As ErrorTypeSymbol
Dim missing As MissingMetadataTypeSymbol = TryCast(type, MissingMetadataTypeSymbol)
' TODO: if it is no longer missing in the target assembly, then we can resolve it here.
If missing IsNot Nothing Then
' TODO: if it is no longer missing in the target assembly, then we can resolve it here.
Return missing
' A retargeted error symbol must trigger an error on use so that a dependent compilation won't
' improperly succeed. We therefore ensure we have a use-site diagnostic.
Dim useSiteDiagnostic = type.GetUseSiteErrorInfo
If useSiteDiagnostic IsNot Nothing Then
Return type
End If
'TODO: produce an error symbol that will trigger an error on use
Return type
Dim errorInfo = If(type.ErrorInfo, ErrorFactory.ErrorInfo(ERRID.ERR_InReferencedAssembly, If(type.ContainingAssembly?.Identity.GetDisplayName, "")))
Return New ExtendedErrorTypeSymbol(errorInfo, type.Name, type.Arity, type.CandidateSymbols, type.ResultKind, True)
End Function
Public Function Retarget(sequence As IEnumerable(Of NamedTypeSymbol)) As IEnumerable(Of NamedTypeSymbol)
......
......@@ -5432,6 +5432,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Get
End Property
'''<summary>
''' Looks up a localized string similar to There is an error in a referenced assembly &apos;{0}&apos;..
'''</summary>
Friend ReadOnly Property ERR_InReferencedAssembly() As String
Get
Return ResourceManager.GetString("ERR_InReferencedAssembly", resourceCulture)
End Get
End Property
'''<summary>
''' Looks up a localized string similar to An expression is too long or complex to compile.
'''</summary>
......
......@@ -4717,4 +4717,7 @@
<data name="ERR_InvalidAssemblyCulture" xml:space="preserve">
<value>Assembly culture strings may not contain embedded NUL characters.</value>
</data>
<data name="ERR_InReferencedAssembly" xml:space="preserve">
<value>There is an error in a referenced assembly '{0}'.</value>
</data>
</root>
......@@ -3894,7 +3894,9 @@ End Class
]]></file>
</compilation>
Dim errors = <errors>
BC36970: Failed to emit module 'Consumer.dll'.
BC30002: Type 'I1' is not defined.
o.M2(Nothing)
~~~~~~~~~~~~~
</errors>
Dim piaCompilation2 = CreateCompilationWithMscorlib(pia2)
'CompileAndVerify(piaCompilation2, emitOptions:=EmitOptions.RefEmitBug)
......
......@@ -319,26 +319,54 @@ BC30652: Reference required to assembly 'CL2, Version=0.0.0.0, Culture=neutral,
Dim errors2 =
<errors>
BC30002: Type 'CL2_C1' is not defined.
CL3_C1.Test1()
~~~~~~~~~~~~
BC30002: Type 'CL2_C1' is not defined.
Global.CL3_C1.Test1()
~~~~~~~~~~~~~~~~~~~
BC30002: Type 'CL2_C1' is not defined.
Dim z As Object = y.x
~~~
BC30002: Type 'CL2_C1' is not defined.
Inherits CL3_C1
~~~~~~
BC30258: Classes can inherit only from other classes.
Inherits CL3_S1
~~~~~~
BC30002: Type 'CL2_I1' is not defined.
Inherits CL3_I1, I1(Of CL3_I1)
~~~~~~
BC30002: Type 'CL2_I1' is not defined.
Implements CL3_I1, I1(Of CL3_I1)
~~~~~~
BC30002: Type 'CL2_C1' is not defined.
CL3_C2.Test1()
~~~~~~~~~~~~~~
BC30469: Reference to a non-shared member requires an object reference.
CL3_C2.Test1(1)
~~~~~~~~~~~~
BC30002: Type 'CL2_C1' is not defined.
CL3_C2.Test3()
~~~~~~~~~~~~~~
BC30516: Overload resolution failed because no accessible 'Test4' accepts this number of arguments.
CL3_C2.Test4()
~~~~~
BC30521: Overload resolution failed because no accessible 'Test4' is most specific for these arguments:
'Public Shared Sub Test4(x As CL3_C1)': Not most specific.
'Public Shared Sub Test4(x As CL3_C3)': Not most specific.
BC30002: Type 'CL2_C1' is not defined.
CL3_C2.Test4(nothing)
~~~~~
BC42025: Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
~~~~~~~~~~~~~~~~~~~~~
BC30002: Type 'CL2_C1' is not defined.
y.Test1()
~~~~~~~
BC31143: Method 'Public Sub Test2(x As Integer)' does not have a signature compatible with delegate 'Delegate Sub CL3_D1(x As CL2_C1)'.
~~~~~~~~~
BC30002: Type 'CL2_C1' is not defined.
Dim d1 As CL3_D1 = AddressOf y.Test2
~~~~~~~
~~~~~~~~~~~~~~~~~
BC30002: Type 'CL2_C1' is not defined.
y.w(Nothing)
~~~
BC30002: Type 'CL2_C1' is not defined.
Dim u As CL3_D1 = Sub(uuu) System.Console.WriteLine()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
</errors>
CompilationUtils.AssertTheseDiagnostics(compilation4, errors2)
......@@ -385,8 +413,87 @@ BC30002: Type 'CL2_I1' is not defined.
Dim compilation5 = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntimeAndReferences(source, {New VisualBasicCompilationReference(cl3BadCompilation2)}, options:=TestOptions.ReleaseExe)
CompilationUtils.AssertTheseDiagnostics(compilation5, errors2)
Dim errors5 =
<errors>
BC30258: Classes can inherit only from other classes.
Inherits CL3_S1
~~~~~~
BC30469: Reference to a non-shared member requires an object reference.
CL3_C2.Test1(1)
~~~~~~~~~~~~
BC30516: Overload resolution failed because no accessible 'Test4' accepts this number of arguments.
CL3_C2.Test4()
~~~~~
BC30521: Overload resolution failed because no accessible 'Test4' is most specific for these arguments:
'Public Shared Sub Test4(x As CL3_C1)': Not most specific.
'Public Shared Sub Test4(x As CL3_C3)': Not most specific.
CL3_C2.Test4(nothing)
~~~~~
BC42025: Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
y.Test1()
~~~~~~~
BC31143: Method 'Public Sub Test2(x As Integer)' does not have a signature compatible with delegate 'Delegate Sub CL3_D1(x As CL2_C1)'.
Dim d1 As CL3_D1 = AddressOf y.Test2
~~~~~~~
</errors>
CompilationUtils.AssertTheseDiagnostics(compilation5, errors5)
Dim errors6 =
<errors>
BC30258: Classes can inherit only from other classes.
Inherits CL3_S1
~~~~~~
BC30469: Reference to a non-shared member requires an object reference.
CL3_C2.Test1(1)
~~~~~~~~~~~~
BC30516: Overload resolution failed because no accessible 'Test4' accepts this number of arguments.
CL3_C2.Test4()
~~~~~
BC30521: Overload resolution failed because no accessible 'Test4' is most specific for these arguments:
'Public Shared Sub Test4(x As CL3_C1)': Not most specific.
'Public Shared Sub Test4(x As CL3_C3)': Not most specific.
CL3_C2.Test4(nothing)
~~~~~
BC42025: Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
y.Test1()
~~~~~~~
BC31143: Method 'Public Sub Test2(x As Integer)' does not have a signature compatible with delegate 'Delegate Sub CL3_D1(x As CL2_C1)'.
Dim d1 As CL3_D1 = AddressOf y.Test2
~~~~~~~
BC30002: Type 'CL2_C1' is not defined.
Inherits CL2_C1
~~~~~~
BC30002: Type 'CL2_C1' is not defined.
Public Shared Function Test2() As CL2_C1
~~~~~~
BC30002: Type 'CL2_C1' is not defined.
Public Function Test3() As CL2_C1
~~~~~~
BC30002: Type 'CL2_C1' is not defined.
Public Shared Function Test1() As CL2_C1
~~~~~~
BC30002: Type 'CL2_C1' is not defined.
Public x As CL2_C1
~~~~~~
BC30002: Type 'CL2_C1' is not defined.
Public Shared Function Test3() As CL2_C1
~~~~~~
BC30002: Type 'CL2_I1' is not defined.
Implements CL2_I1, CL2_I2
~~~~~~
BC30002: Type 'CL2_I2' is not defined.
Implements CL2_I1, CL2_I2
~~~~~~
BC30002: Type 'CL2_C1' is not defined.
Public Delegate Sub CL3_D1(x As CL2_C1)
~~~~~~
BC30002: Type 'CL2_I1' is not defined.
Implements CL2_I1
~~~~~~
BC30002: Type 'CL2_I1' is not defined.
Inherits CL2_I1
~~~~~~
</errors>
Dim cl4Source =
<compilation name="cl4">
<%= a_vb %>
......@@ -395,8 +502,7 @@ BC30002: Type 'CL2_I1' is not defined.
Dim compilation6 = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(cl4Source)
CompilationUtils.AssertTheseDiagnostics(compilation6,
<error><%= errors2.Value.TrimEnd() + errors3.Value %></error>)
CompilationUtils.AssertTheseDiagnostics(compilation6, errors6)
CompilationUtils.AssertNoErrors(compilation1)
End Sub
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册