Skip to content

Commit

Permalink
Report use-site errors for types with possibly inaccurate TypeKind va…
Browse files Browse the repository at this point in the history
…lue.

Fixes dotnet#14267.
  • Loading branch information
AlekseyTs committed Nov 7, 2016
1 parent aff88fa commit 298281d
Show file tree
Hide file tree
Showing 17 changed files with 440 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,7 @@ private void CheckTupleUnderlying(NamedTypeSymbol namedTypeSymbol, SyntaxNode sy
// but if it does happen we should make it a failure.
// NOTE: declaredBase could be null for interfaces
var declaredBase = namedTypeSymbol.BaseTypeNoUseSiteDiagnostics;
if (declaredBase == null ||
(declaredBase.SpecialType != SpecialType.System_ValueType && !declaredBase.IsErrorType()))
if (declaredBase == null || declaredBase.SpecialType != SpecialType.System_ValueType)
{
// Try to decrease noise by not complaining about the same type over and over again.
if (_reportedErrorTypesMap.Add(namedTypeSymbol))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,27 @@ protected virtual DiagnosticInfo GetUseSiteDiagnosticImpl()
{
diagnostic = new CSDiagnosticInfo(ErrorCode.ERR_BogusType, this);
}
else if (TypeKind == TypeKind.Class && SpecialType != SpecialType.System_Enum)
{
TypeSymbol @base = GetDeclaredBaseType(null);
if (@base?.SpecialType == SpecialType.None && @base.ContainingAssembly?.IsMissing == true)
{
var missingType = @base as MissingMetadataTypeSymbol.TopLevel;
if ((object)missingType != null && missingType.Arity == 0)
{
string emittedName = MetadataHelpers.BuildQualifiedName(missingType.NamespaceName, missingType.MetadataName);
switch (SpecialTypes.GetTypeFromMetadataName(emittedName))
{
case SpecialType.System_Enum:
case SpecialType.System_MulticastDelegate:
case SpecialType.System_ValueType:
// This might be a structure, an enum, or a delegate
diagnostic = missingType.GetUseSiteDiagnostic();
break;
}
}
}
}
}

return diagnostic;
Expand Down
17 changes: 15 additions & 2 deletions src/Compilers/CSharp/Test/Emit/Attributes/AttributeTests_Tuples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ static void Main()
comp.VerifyDiagnostics(
// (6,11): error CS0518: Predefined type 'System.String' is not defined or imported
// D<(int x, int y)> d = o => { };
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "(int x, int y)").WithArguments("System.String").WithLocation(6, 11)
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "(int x, int y)").WithArguments("System.String").WithLocation(6, 11),
// (6,11): error CS0012: The type 'ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
// D<(int x, int y)> d = o => { };
Diagnostic(ErrorCode.ERR_NoTypeDef, "(int x, int y)").WithArguments("System.ValueType", "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089").WithLocation(6, 11),
// (7,11): error CS0012: The type 'ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
// d((0, 0));
Diagnostic(ErrorCode.ERR_NoTypeDef, "(0, 0)").WithArguments("System.ValueType", "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089").WithLocation(7, 11)
);
}

Expand Down Expand Up @@ -193,7 +199,14 @@ class C
comp.VerifyDiagnostics(
// (4,12): error CS0518: Predefined type 'System.String' is not defined or imported
// static (int x, int y) M() => (0, 0);
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "(int x, int y)").WithArguments("System.String").WithLocation(4, 12));
Diagnostic(ErrorCode.ERR_PredefinedTypeNotFound, "(int x, int y)").WithArguments("System.String").WithLocation(4, 12),
// (4,12): error CS0012: The type 'ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
// static (int x, int y) M() => (0, 0);
Diagnostic(ErrorCode.ERR_NoTypeDef, "(int x, int y)").WithArguments("System.ValueType", "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a").WithLocation(4, 12),
// (4,34): error CS0012: The type 'ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
// static (int x, int y) M() => (0, 0);
Diagnostic(ErrorCode.ERR_NoTypeDef, "(0, 0)").WithArguments("System.ValueType", "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a").WithLocation(4, 34)
);
}

[Fact]
Expand Down
34 changes: 30 additions & 4 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenTupleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2833,7 +2833,7 @@ static void Main()
}
";

var comp = CreateCompilationWithMscorlib(source, references: new[] { ValueTupleRef });
var comp = CreateCompilationWithMscorlib(source, references: s_valueTupleRefs);
comp.VerifyDiagnostics(
// (6,37): error CS8125: Tuple member name 'Item10' is only allowed at position 10.
// (int Item1, int Item01, int Item10) x = (Item01: 1, Item1: 2, Item10: 3);
Expand Down Expand Up @@ -2904,7 +2904,7 @@ static void Main()
}
";

var comp = CreateCompilationWithMscorlib(source, references: new[] { ValueTupleRef });
var comp = CreateCompilationWithMscorlib(source, references: s_valueTupleRefs);
comp.VerifyDiagnostics(
// (6,28): error CS8125: Tuple member name 'Item3' is only allowed at position 3.
// (int Item1, string Item3, string Item2, int Item4, int Item5, int Item6, int Item7, string Rest) x = (Item2: "bad", Item4: "bad", Item3: 3, Item4: 4, Item5: 5, Item6: 6, Item7: 7, Rest: "bad");
Expand Down Expand Up @@ -2940,7 +2940,7 @@ static void Main()
}
";

var comp = CreateCompilationWithMscorlib(source, references: new[] { ValueTupleRef });
var comp = CreateCompilationWithMscorlib(source, references: s_valueTupleRefs);
comp.VerifyDiagnostics(
// (6,18): error CS8126: Tuple member name 'CompareTo' is disallowed at any position.
// var x = (CompareTo: 2, Create: 3, Deconstruct: 4, Equals: 5, GetHashCode: 6, Rest: 8, ToString: 10);
Expand Down Expand Up @@ -13936,7 +13936,7 @@ public static void Main()

var comp = CreateCompilationWithMscorlib(source3,
references: new[] { comp1.ToMetadataReference(), comp2.ToMetadataReference().WithAliases(ImmutableArray.Create("alias1")),
ValueTupleRef },
SystemRuntimeFacadeRef, ValueTupleRef },
parseOptions: TestOptions.Regular,
options: TestOptions.DebugExe);

Expand Down Expand Up @@ -19738,6 +19738,7 @@ implicit operator AA
--
--");
}

[WorkItem(14708, "https://github.com/dotnet/roslyn/issues/14708")]
[WorkItem(14709, "https://github.com/dotnet/roslyn/issues/14709")]
[Fact]
Expand Down Expand Up @@ -19902,5 +19903,30 @@ class C2: ClassLibrary1.C1
Assert.Equal("ref (System.Int32, dynamic) ClassLibrary1.C1.Foo.get", b.GetMethod.ToTestDisplayString());
}

[Fact]
[WorkItem(14267, "https://github.com/dotnet/roslyn/issues/14267")]
public void NoSystemRuntimeFacade()
{
var source = @"
class C
{
static void M()
{
var o = (1, 2);
}
}
";

var compilation = CreateCompilationWithMscorlib(source,
references: new[] { ValueTupleRef });

Assert.Equal(TypeKind.Class, compilation.GetWellKnownType(WellKnownType.System_ValueTuple_T2).TypeKind);

compilation.VerifyDiagnostics(
// (6,17): error CS0012: The type 'ValueType' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
// var o = (1, 2);
Diagnostic(ErrorCode.ERR_NoTypeDef, "(1, 2)").WithArguments("System.ValueType", "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a").WithLocation(6, 17)
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ static void M((dynamic, int) t)
var compilation0 = CreateCompilationWithMscorlib(
source,
options: TestOptions.DebugDll,
references: new[] { SystemCoreRef, CSharpRef, ValueTupleRef });
references: new[] { SystemCoreRef, CSharpRef, ValueTupleRef, SystemRuntimeFacadeRef });
var compilation1 = compilation0.WithSource(source);

var testData0 = new CompilationTestData();
Expand Down
4 changes: 2 additions & 2 deletions src/Compilers/CSharp/Test/Emit/PDB/PDBTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ static void F()
}
}";

var debug = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef }, options: TestOptions.DebugWinMD);
var debug = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef, SystemRuntimeFacadeRef }, options: TestOptions.DebugWinMD);
debug.VerifyPdb(
@"<symbols>
<methods>
Expand All @@ -429,7 +429,7 @@ static void F()
</methods>
</symbols>");

var release = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef }, options: TestOptions.ReleaseWinMD);
var release = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef, SystemRuntimeFacadeRef }, options: TestOptions.ReleaseWinMD);
release.VerifyPdb(
@"<symbols>
<methods>
Expand Down
10 changes: 5 additions & 5 deletions src/Compilers/CSharp/Test/Emit/PDB/PDBTupleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static void F()
(int A, int B, (int C, int), int, int, int G, int H, int I) t = (1, 2, (3, 4), 5, 6, 7, 8, 9);
}
}";
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef }, options: TestOptions.DebugDll);
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef, SystemRuntimeFacadeRef }, options: TestOptions.DebugDll);
comp.VerifyPdb(
@"<symbols>
<methods>
Expand Down Expand Up @@ -63,7 +63,7 @@ static void F()
const C<(int A, int B)> c = null;
}
}";
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef }, options: TestOptions.DebugDll);
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef, SystemRuntimeFacadeRef }, options: TestOptions.DebugDll);
comp.VerifyPdb(
@"<symbols>
<methods>
Expand Down Expand Up @@ -112,7 +112,7 @@ static void F()
}
}
}";
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef }, options: TestOptions.DebugDll);
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef, SystemRuntimeFacadeRef }, options: TestOptions.DebugDll);
comp.VerifyPdb(
@"<symbols>
<methods>
Expand Down Expand Up @@ -176,7 +176,7 @@ static void F()
(int \u1234, int, int \u005f\u1200\u005f) \u1200 = (1, 2, 3);
}
}";
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef }, options: TestOptions.DebugDll);
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef, SystemRuntimeFacadeRef }, options: TestOptions.DebugDll);
comp.VerifyPdb(
string.Format(@"<symbols>
<methods>
Expand Down Expand Up @@ -224,7 +224,7 @@ static void F(System.Collections.Generic.IEnumerable<(int a, int b)> ie)
} //10,9
} //11,5
}";
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef }, options: TestOptions.DebugDll);
var comp = CreateCompilationWithMscorlib(source, new[] { ValueTupleRef, SystemRuntimeFacadeRef }, options: TestOptions.DebugDll);
comp.VerifyPdb(
string.Format(@"<symbols>
<methods>
Expand Down
Loading

0 comments on commit 298281d

Please sign in to comment.