Skip to content

Emit a warning when type parameters are shadowed. #62767

@hborla

Description

@hborla

This code does not produce any diagnostics:

struct S<T> {
  func f<T>(_: T) {}
}

Then, confusing errors are produced when the above code is called incorrectly:

S().f(1) // error: Generic parameter 'T' could not be inferred

This is fundamentally because the type parameter T is shadowed in func f. Usually, programmers do this by accident - they meant for the inner T to match the outer T. The compiler should emit a warning when type parameters are shadowed like this, with two notes for possible fixes:

  • A note to remove the shadowed type parameter with a corresponding fix-it.
  • A note to rename the shadowed type parameter.

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of ImplementationgenericsFeature: generic declarations and typestype checkerArea → compiler: Semantic analysis

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions