Skip to content

Warnings for intrinsics that throw or are unreachable #106886

@sbomer

Description

@sbomer

dotnet/linker#2652 added tests that showed a few cases where intrinsics produce warnings for null inputs even when they throw at runtime. The intrinsic handling falls back on the annotated return values, which warn if they flow into methods with requirements that aren't satisfied by the return annotation.

// Warns about the return value of GetType, even though this throws at runtime.
Type.GetType (null).RequiresAll ();
TestType nullInstance = null;
// Warns about the return value of GetType, even though this throws at runtime
nullInstance.GetType ().RequiresAll ();
Type type = null;
// Warns about the return value of BaseType, even though this throws at runtime.
type.BaseType.RequiresPublicMethods ();
// Technically this shouldn't warn because CreateInstanceFrom throws if the assembly file path is null.
// However, our implementation is the same for CreateInstance and CreateInstanceFrom.
Activator.CreateInstanceFrom (null, "Namespace.SomeType");

A similar issue exists for "empty" inputs flowing to intrinsics (the empty inputs represent unimplemented intrinsics in the analyzer, which should not warn, or values produced from intrinsics that are known to throw at runtime). In a few intrinsics, we propagate the "empty" input, while in others, we fall back on annotations.

Type t = null;
string noValue = t.AssemblyQualifiedName;
// Warns about the return value of GetType, even though AssemblyQualifiedName throws at runtime.
Type.GetType (noValue).RequiresAll ();
Type t = null;
Type noValue = Type.GetTypeFromHandle (t.TypeHandle);
// Warns about the return value of GetType, even though the above throws at runtime.
noValue.GetType ().RequiresAll ();
Type t = null;
Type noValue = Type.GetTypeFromHandle (t.TypeHandle);
// Warns about the base type even though the above throws an exception at runtime.
noValue.BaseType.RequiresPublicMethods ();
Type t = null;
Type noValue = Type.GetTypeFromHandle (t.TypeHandle);
// Warns about the base type even though the above throws an exception at runtime.
noValue.BaseType.RequiresPublicMethods ();

My suggestion would be to consistently avoid producing warnings for these cases, and return "empty" from intrinsics that we know would throw, or which receive an "empty" value.

Metadata

Metadata

Assignees

Labels

area-Tools-ILLink.NET linker development as well as trimming analyzers

Type

No type

Projects

Status

No status

Relationships

None yet

Development

No branches or pull requests

Issue actions