Skip to content

Commit 66815f9

Browse files
committed
[MiscDiagnostics] Downgrade plain type use error to a warning for certain positions
In Swift < 6 warn about plain type name passed as an argument to a subscript, dynamic subscript, or ObjC literal since it used to be accepted. Resolves: rdar://80270126
1 parent 333fdbc commit 66815f9

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,11 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
615615
if (!AlreadyDiagnosedMetatypes.insert(E).second)
616616
return;
617617

618+
// In Swift < 6 warn about plain type name passed as an
619+
// argument to a subscript, dynamic subscript, or ObjC
620+
// literal since it used to be accepted.
621+
DiagnosticBehavior behavior = DiagnosticBehavior::Error;
622+
618623
// Allow references to types as a part of:
619624
// - member references T.foo, T.Type, T.self, etc.
620625
// - constructor calls T()
@@ -634,11 +639,22 @@ static void diagSyntacticUseRestrictions(const Expr *E, const DeclContext *DC,
634639
isa<SubscriptExpr>(ParentExpr)) {
635640
return;
636641
}
642+
643+
if (!Ctx.LangOpts.isSwiftVersionAtLeast(6)) {
644+
auto argument = CallArgs.find(ParentExpr);
645+
if (argument != CallArgs.end()) {
646+
auto *callExpr = argument->second;
647+
if (isa<SubscriptExpr>(callExpr) ||
648+
isa<DynamicSubscriptExpr>(callExpr) ||
649+
isa<ObjectLiteralExpr>(callExpr))
650+
behavior = DiagnosticBehavior::Warning;
651+
}
652+
}
637653
}
638654

639655
// Is this a protocol metatype?
640-
641-
Ctx.Diags.diagnose(E->getStartLoc(), diag::value_of_metatype_type);
656+
Ctx.Diags.diagnose(E->getStartLoc(), diag::value_of_metatype_type)
657+
.limitBehavior(behavior);
642658

643659
// Add fix-it to insert '()', only if this is a metatype of
644660
// non-existential type and has any initializers.

test/Constraints/dynamic_lookup.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,20 @@ func testCallAsFunctionAnyObject(_ x: AnyObject) {
433433
x() // expected-error {{cannot call value of non-function type 'AnyObject'}}
434434
x.callAsFunction() // Okay.
435435
}
436+
437+
// Note: In Swift >= 6 mode this would become an error.
438+
func test_dynamic_subscript_accepts_type_name_argument() {
439+
@objc class A {
440+
@objc subscript(a: A.Type) -> Int { get { 42 } }
441+
}
442+
443+
func test(a: AnyObject, optA: AnyObject?) {
444+
let _ = a[A] // expected-warning {{expected member name or constructor call after type name}}
445+
// expected-note@-1 {{add arguments after the type to construct a value of the type}} {{16-16=()}}
446+
// expected-note@-2 {{use '.self' to reference the type object}} {{16-16=.self}}
447+
448+
let _ = optA?[A] // expected-warning {{expected member name or constructor call after type name}}
449+
// expected-note@-1 {{add arguments after the type to construct a value of the type}} {{20-20=()}}
450+
// expected-note@-2 {{use '.self' to reference the type object}} {{20-20=.self}}
451+
}
452+
}

test/Constraints/subscript.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,19 @@ func rdar61084565() {
217217
a[] // expected-error {{value of type 'Foo' has no subscripts}}
218218
}
219219

220+
// Note: In Swift >= 6 mode this would become an error.
220221
func test_subscript_accepts_type_name_argument() {
221222
struct A {
222223
subscript(a: A.Type) -> Int { get { 42 } }
223224
}
224225

225-
func test(a: A?) {
226-
let _ = a?[A] // expected-error {{expected member name or constructor call after type name}}
227-
// expected-note@-1 {{add arguments after the type to construct a value of the type}} {{17-17=()}}
228-
// expected-note@-2 {{use '.self' to reference the type object}} {{17-17=.self}}
226+
func test(a: A, optA: A?) {
227+
let _ = a[A] // expected-warning {{expected member name or constructor call after type name}}
228+
// expected-note@-1 {{add arguments after the type to construct a value of the type}} {{16-16=()}}
229+
// expected-note@-2 {{use '.self' to reference the type object}} {{16-16=.self}}
230+
231+
let _ = optA?[A] // expected-warning {{expected member name or constructor call after type name}}
232+
// expected-note@-1 {{add arguments after the type to construct a value of the type}} {{20-20=()}}
233+
// expected-note@-2 {{use '.self' to reference the type object}} {{20-20=.self}}
229234
}
230235
}

0 commit comments

Comments
 (0)