Skip to content

Nullable Return Issue in Shims #246

@JasonBock

Description

@JasonBock

To reproduce:

var code =
	"""
	using Rocks;

	#nullable enable

	public interface IReadOnlyNavigationBase
	{
		IReadOnlyNavigationBase? Inverse { get; }
	}

	public interface IReadOnlySkipNavigation
		: IReadOnlyNavigationBase
	{
		new IReadOnlySkipNavigation Inverse { get; }

		IReadOnlyNavigationBase IReadOnlyNavigationBase.Inverse
		{
			get => Inverse;
		}
	}

	public interface IConventionSkipNavigation 
		: IReadOnlySkipNavigation
	{
		new IConventionSkipNavigation? Inverse
		{
			get => (IConventionSkipNavigation?)((IReadOnlySkipNavigation)this).Inverse;
		}
	}

	public static class Test
	{
		public static void Go() => Rock.Create<IConventionSkipNavigation>();
	}
	""";

This leads to the following error:

// Rocks\Rocks.RockCreateGenerator\IConventionSkipNavigation_Rock_Create.g.cs(119,12): error CS8603: Possible null reference return.

The problem is here in the generated code:

IReadOnlySkipNavigation IReadOnlySkipNavigation.Inverse
{
	get => ((IConventionSkipNavigation)this.mock).Inverse;
}

I think this is because the return for IConventionSkipNavigation.Inverse is nullable, but the one for IReadOnlySkipNavigation.Inverse is not. Adding a ! should fix the return.

This was found on Microsoft.EntityFrameworkCore.Metadata.IConventionSkipNavigation.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions