Skip to content

Commit

Permalink
Skip assignment nullability warning in error case (#61895)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcouv authored Jun 17, 2022
1 parent 92b69bf commit 2e3d609
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8083,7 +8083,7 @@ private TypeWithState VisitConversion(

TypeWithState resultType = calculateResultType(targetTypeWithNullability, fromExplicitCast, resultState, isSuppressed, targetType);

if (operandType.Type?.IsErrorType() != true && !targetType.IsErrorType())
if (!conversionOperand.HasErrors && !targetType.IsErrorType())
{
// Need to report all warnings that apply since the warnings can be suppressed individually.
if (reportTopLevelWarnings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2646,18 +2646,12 @@ static void M(C<string> x)
// (6,26): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('C<string>')
// C<string?>* y1 = &x;
Diagnostic(ErrorCode.ERR_ManagedAddr, "&x").WithArguments("C<string>").WithLocation(6, 26),
// (6,26): warning CS8619: Nullability of reference types in value of type 'C<string>*' doesn't match target type 'C<string?>*'.
// C<string?>* y1 = &x;
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "&x").WithArguments("C<string>*", "C<string?>*").WithLocation(6, 26),
// (7,9): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('C<string?>')
// C<string?>* y2 = &x!;
Diagnostic(ErrorCode.ERR_ManagedAddr, "C<string?>*").WithArguments("C<string?>").WithLocation(7, 9),
// (7,26): error CS0208: Cannot take the address of, get the size of, or declare a pointer to a managed type ('C<string>')
// C<string?>* y2 = &x!;
Diagnostic(ErrorCode.ERR_ManagedAddr, "&x!").WithArguments("C<string>").WithLocation(7, 26),
// (7,26): warning CS8619: Nullability of reference types in value of type 'C<string>*' doesn't match target type 'C<string?>*'.
// C<string?>* y2 = &x!;
Diagnostic(ErrorCode.WRN_NullabilityMismatchInAssignment, "&x!").WithArguments("C<string>*", "C<string?>*").WithLocation(7, 26),
// (7,27): error CS8598: The suppression operator is not allowed in this context
// C<string?>* y2 = &x!;
Diagnostic(ErrorCode.ERR_IllegalSuppression, "x").WithLocation(7, 27)
Expand Down Expand Up @@ -156369,5 +156363,31 @@ public static void M(C c1, C? c2)
Diagnostic(ErrorCode.WRN_NullableValueTypeMayBeNull, "c2").WithLocation(25, 13)
);
}

[Fact, WorkItem(61462, "https://github.com/dotnet/roslyn/issues/61462")]
public void NoNullabilityWarningUnlessAssignmentConversionExists()
{
var comp = CreateCompilation("""
#nullable enable

public static class S
{
public const string A = "a";
}

public class C
{
public (string, string) M()
{
return ("a", S.B);
}
}
""");
comp.VerifyDiagnostics(
// (12,24): error CS0117: 'S' does not contain a definition for 'B'
// return ("a", S.B);
Diagnostic(ErrorCode.ERR_NoSuchMember, "B").WithArguments("S", "B").WithLocation(12, 24)
);
}
}
}

0 comments on commit 2e3d609

Please sign in to comment.