Skip to content

Consider nullability in conversion to constraint type#46405

Merged
cston merged 6 commits intodotnet:masterfrom
cston:46150
Aug 7, 2020
Merged

Consider nullability in conversion to constraint type#46405
cston merged 6 commits intodotnet:masterfrom
cston:46150

Conversation

@cston
Copy link
Contributor

@cston cston commented Jul 29, 2020

Fixes #46150
Fixes #46410

@cston cston requested a review from a team as a code owner July 29, 2020 00:59
var comp = CreateCompilation(source);
var comp = CreateCompilation(source, parseOptions: TestOptions.RegularPreview);
comp.VerifyEmitDiagnostics();
}
Copy link
Contributor Author

@cston cston Jul 29, 2020

Choose a reason for hiding this comment

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

The only changes in LambdaNullReturn_01 to _06 is that each pair of tests have been combined. Two new tests 04 and _05 were added at the end. (There were already too many similar tests and adding 4 new tests rather than combining pairs would have made the situation worse.)

@cston
Copy link
Contributor Author

cston commented Aug 5, 2020

@dotnet/roslyn-compiler please review.

@333fred
Copy link
Member

333fred commented Aug 5, 2020

Looking

Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

LGTM (commit 4)

@@ -6474,9 +6483,25 @@ static NullableFlowState getReferenceConversionResultState(TypeWithAnnotations t
// Converting to a less-derived type (object, interface, type parameter).
// If the operand is MaybeNull or MaybeDefault, the result should be
Copy link
Member

@jcouv jcouv Aug 6, 2020

Choose a reason for hiding this comment

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

Is the comment still accurate? I'm having trouble mapping it to the logic below. #Resolved

}

// If type parameter 1 depends on type parameter 2,
// returns the corresponding annotation.
Copy link
Member

@jcouv jcouv Aug 6, 2020

Choose a reason for hiding this comment

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

what does "corresponding annotation" mean? #Resolved

else if (targetType.NullableAnnotation.IsNotAnnotated() &&
type is TypeParameterSymbol typeParameter1 &&
targetType.Type is TypeParameterSymbol typeParameter2 &&
dependsOnTypeParameter(typeParameter1, typeParameter2, NullableAnnotation.NotAnnotated, out var annotation))
Copy link
Member

@jcouv jcouv Aug 6, 2020

Choose a reason for hiding this comment

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

Can dependsOnTypeParameter ever return false here (and above in getReferenceConversionResultState)? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The dependsOnTypeParameter() check can fail in getReferenceConversionResultState(), with the following for instance (see NullabilityOfTypeParameters_171()):

#nullable enable
class C
{
    static U F<T, U>(T t)
        where T : C?
        where U : T
    {
        return (U)t;
    }
}

I suspect the check here in getBoxingConversionResultState() cannot fail, because both the target type and the operand type must be unconstrained type parameters, and we only create boxing conversions for those cases when there is an implicit conversion from operand to target type which requires checking the constraint types.


In reply to: 466677704 [](ancestors = 466677704)

{
return NullableFlowState.MaybeDefault;
var type = operandType.Type;
if (type?.IsTypeParameterDisallowingAnnotationInCSharp8() != true)
Copy link
Member

@jcouv jcouv Aug 6, 2020

Choose a reason for hiding this comment

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

nit: consider aligning with next local function if (type is null || !type.IsTypeParameterDisallowingAnnotationInCSharp8()) #Resolved

static object F3<T>(T? t) where T : class => t; // 1
static object? F4<T>(T? t) where T : class => t;
}";
comp = CreateCompilation(source2, parseOptions: TestOptions.RegularPreview);
Copy link
Member

@jcouv jcouv Aug 6, 2020

Choose a reason for hiding this comment

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

is the language version relevant? If not, consider using C# 8.
Also applicable to other scenarios in this test. #Resolved

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

Done with review pass (iteration 4)

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (Iteration 6)

@cston cston merged commit 2c5e11c into dotnet:master Aug 7, 2020
@ghost ghost added this to the Next milestone Aug 7, 2020
@cston cston deleted the 46150 branch August 7, 2020 17:28
333fred added a commit that referenced this pull request Aug 10, 2020
…features/function-pointers

* upstream/master: (465 commits)
  Stop specifying Rich Nav's version of dotnet
  Fix nullable reference warning
  Fix up some calls to SymbolKey.Resolve now that it's annotated
  Remove try/catch of ArgumentException
  Null annotate SymbolKey
  Annotate Location.IsInSource as ensuring SourceTree is non-null
  Fix nullable annotation of Compilation.CreateErrorTypeSymbol
  Update null state for add/remove on field-like events (#46477)
  Throw IndexOutOfRangeException from BitVector.this[int] if index < 0 (#46627)
  Delete some dead support for changing legacy options
  Delete our use of Microsoft.VisualStudio.CodingConventions
  Fix crash in the preview of a code action that modified an .editorconfig
  Move null check above where the variable is used (#46558)
  Add integration test to configure diagnostic severity via editorconfig
  Disable a few Mac tests
  Consider nullability in conversion to constraint type (#46405)
  Added interpolated strings and tests
  Introduce request context (#46557)
  Simplify NativeIntegerAttribute encoding (#46522)
  Update extension getenumerator status. (#46607)
  ...
@RikkiGibson RikkiGibson modified the milestones: Next, 16.8.P2 Aug 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

4 participants