Skip to content

Commit 125c1fb

Browse files
author
Nathan Hawes
committed
[Sema] Avoid needing the typechecker for the IsStaticRequest request
It was being used purely to get the name of the type context for a diagnostic message. SourceKit's syntactic-only requests were hitting an assertion when this diagnostic was triggered because they don't set up a type checker.
1 parent 0a3a130 commit 125c1fb

File tree

7 files changed

+215
-10
lines changed

7 files changed

+215
-10
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,12 @@ ERROR(invalid_arg_count_for_operator,none,
911911
"operators must have one or two arguments", ())
912912
ERROR(operator_in_local_scope,none,
913913
"operator functions can only be declared at global or in type scope", ())
914-
ERROR(nonstatic_operator_in_type,none,
915-
"operator %0 declared in type %1 must be 'static'", (Identifier, Type))
914+
ERROR(nonstatic_operator_in_nominal,none,
915+
"operator %0 declared in type %1 must be 'static'",
916+
(Identifier, DeclName))
917+
ERROR(nonstatic_operator_in_extension,none,
918+
"operator %0 declared in extension of %1 must be 'static'",
919+
(Identifier, TypeRepr*))
916920
ERROR(nonfinal_operator_in_class,none,
917921
"operator %0 declared in non-final class %1 must be 'final'",
918922
(Identifier, Type))

lib/Sema/TypeCheckDecl.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,10 +1258,18 @@ IsStaticRequest::evaluate(Evaluator &evaluator, FuncDecl *decl) const {
12581258
decl->isOperator() &&
12591259
dc->isTypeContext()) {
12601260
auto operatorName = decl->getFullName().getBaseIdentifier();
1261-
decl->diagnose(diag::nonstatic_operator_in_type,
1262-
operatorName, dc->getDeclaredInterfaceType())
1263-
.fixItInsert(decl->getAttributeInsertionLoc(/*forModifier=*/true),
1264-
"static ");
1261+
if (auto ED = dyn_cast<ExtensionDecl>(dc->getAsDecl())) {
1262+
decl->diagnose(diag::nonstatic_operator_in_extension,
1263+
operatorName, ED->getExtendedTypeRepr())
1264+
.fixItInsert(decl->getAttributeInsertionLoc(/*forModifier=*/true),
1265+
"static ");
1266+
} else {
1267+
auto *NTD = cast<NominalTypeDecl>(dc->getAsDecl());
1268+
decl->diagnose(diag::nonstatic_operator_in_nominal, operatorName,
1269+
NTD->getName())
1270+
.fixItInsert(decl->getAttributeInsertionLoc(/*forModifier=*/true),
1271+
"static ");
1272+
}
12651273
result = true;
12661274
}
12671275

test/SourceKit/DocumentStructure/Inputs/main.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,7 @@ class OneMore {
135135
fatalError()
136136
}
137137
}
138+
139+
class Chain<A> {
140+
func + (lhs: Chain<A>, rhs: Chain<A>) -> Chain<A> { fatalError() }
141+
}

test/SourceKit/DocumentStructure/structure.swift.empty.response

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
key.offset: 0,
3-
key.length: 2259,
3+
key.length: 2348,
44
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
55
key.substructure: [
66
{
@@ -1403,6 +1403,69 @@
14031403
]
14041404
}
14051405
]
1406+
},
1407+
{
1408+
key.kind: source.lang.swift.decl.class,
1409+
key.accessibility: source.lang.swift.accessibility.internal,
1410+
key.name: "Chain",
1411+
key.offset: 2260,
1412+
key.length: 87,
1413+
key.nameoffset: 2266,
1414+
key.namelength: 5,
1415+
key.bodyoffset: 2276,
1416+
key.bodylength: 70,
1417+
key.substructure: [
1418+
{
1419+
key.kind: source.lang.swift.decl.generic_type_param,
1420+
key.name: "A",
1421+
key.offset: 2272,
1422+
key.length: 1,
1423+
key.nameoffset: 2272,
1424+
key.namelength: 1
1425+
},
1426+
{
1427+
key.kind: source.lang.swift.decl.function.method.static,
1428+
key.accessibility: source.lang.swift.accessibility.internal,
1429+
key.name: "+(_:_:)",
1430+
key.offset: 2279,
1431+
key.length: 66,
1432+
key.typename: "Chain<A>",
1433+
key.nameoffset: 2284,
1434+
key.namelength: 32,
1435+
key.bodyoffset: 2330,
1436+
key.bodylength: 14,
1437+
key.substructure: [
1438+
{
1439+
key.kind: source.lang.swift.decl.var.parameter,
1440+
key.name: "lhs",
1441+
key.offset: 2287,
1442+
key.length: 13,
1443+
key.typename: "Chain<A>",
1444+
key.nameoffset: 0,
1445+
key.namelength: 0
1446+
},
1447+
{
1448+
key.kind: source.lang.swift.decl.var.parameter,
1449+
key.name: "rhs",
1450+
key.offset: 2302,
1451+
key.length: 13,
1452+
key.typename: "Chain<A>",
1453+
key.nameoffset: 0,
1454+
key.namelength: 0
1455+
},
1456+
{
1457+
key.kind: source.lang.swift.expr.call,
1458+
key.name: "fatalError",
1459+
key.offset: 2331,
1460+
key.length: 12,
1461+
key.nameoffset: 2331,
1462+
key.namelength: 10,
1463+
key.bodyoffset: 2342,
1464+
key.bodylength: 0
1465+
}
1466+
]
1467+
}
1468+
]
14061469
}
14071470
],
14081471
key.diagnostics: [

test/SourceKit/DocumentStructure/structure.swift.foobar.response

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
key.offset: 0,
3-
key.length: 2259,
3+
key.length: 2348,
44
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
55
key.substructure: [
66
{
@@ -1403,6 +1403,69 @@
14031403
]
14041404
}
14051405
]
1406+
},
1407+
{
1408+
key.kind: source.lang.swift.decl.class,
1409+
key.accessibility: source.lang.swift.accessibility.internal,
1410+
key.name: "Chain",
1411+
key.offset: 2260,
1412+
key.length: 87,
1413+
key.nameoffset: 2266,
1414+
key.namelength: 5,
1415+
key.bodyoffset: 2276,
1416+
key.bodylength: 70,
1417+
key.substructure: [
1418+
{
1419+
key.kind: source.lang.swift.decl.generic_type_param,
1420+
key.name: "A",
1421+
key.offset: 2272,
1422+
key.length: 1,
1423+
key.nameoffset: 2272,
1424+
key.namelength: 1
1425+
},
1426+
{
1427+
key.kind: source.lang.swift.decl.function.method.static,
1428+
key.accessibility: source.lang.swift.accessibility.internal,
1429+
key.name: "+(_:_:)",
1430+
key.offset: 2279,
1431+
key.length: 66,
1432+
key.typename: "Chain<A>",
1433+
key.nameoffset: 2284,
1434+
key.namelength: 32,
1435+
key.bodyoffset: 2330,
1436+
key.bodylength: 14,
1437+
key.substructure: [
1438+
{
1439+
key.kind: source.lang.swift.decl.var.parameter,
1440+
key.name: "lhs",
1441+
key.offset: 2287,
1442+
key.length: 13,
1443+
key.typename: "Chain<A>",
1444+
key.nameoffset: 0,
1445+
key.namelength: 0
1446+
},
1447+
{
1448+
key.kind: source.lang.swift.decl.var.parameter,
1449+
key.name: "rhs",
1450+
key.offset: 2302,
1451+
key.length: 13,
1452+
key.typename: "Chain<A>",
1453+
key.nameoffset: 0,
1454+
key.namelength: 0
1455+
},
1456+
{
1457+
key.kind: source.lang.swift.expr.call,
1458+
key.name: "fatalError",
1459+
key.offset: 2331,
1460+
key.length: 12,
1461+
key.nameoffset: 2331,
1462+
key.namelength: 10,
1463+
key.bodyoffset: 2342,
1464+
key.bodylength: 0
1465+
}
1466+
]
1467+
}
1468+
]
14061469
}
14071470
],
14081471
key.diagnostics: [

test/SourceKit/DocumentStructure/structure.swift.response

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
key.offset: 0,
3-
key.length: 2259,
3+
key.length: 2348,
44
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
55
key.substructure: [
66
{
@@ -1403,6 +1403,69 @@
14031403
]
14041404
}
14051405
]
1406+
},
1407+
{
1408+
key.kind: source.lang.swift.decl.class,
1409+
key.accessibility: source.lang.swift.accessibility.internal,
1410+
key.name: "Chain",
1411+
key.offset: 2260,
1412+
key.length: 87,
1413+
key.nameoffset: 2266,
1414+
key.namelength: 5,
1415+
key.bodyoffset: 2276,
1416+
key.bodylength: 70,
1417+
key.substructure: [
1418+
{
1419+
key.kind: source.lang.swift.decl.generic_type_param,
1420+
key.name: "A",
1421+
key.offset: 2272,
1422+
key.length: 1,
1423+
key.nameoffset: 2272,
1424+
key.namelength: 1
1425+
},
1426+
{
1427+
key.kind: source.lang.swift.decl.function.method.static,
1428+
key.accessibility: source.lang.swift.accessibility.internal,
1429+
key.name: "+(_:_:)",
1430+
key.offset: 2279,
1431+
key.length: 66,
1432+
key.typename: "Chain<A>",
1433+
key.nameoffset: 2284,
1434+
key.namelength: 32,
1435+
key.bodyoffset: 2330,
1436+
key.bodylength: 14,
1437+
key.substructure: [
1438+
{
1439+
key.kind: source.lang.swift.decl.var.parameter,
1440+
key.name: "lhs",
1441+
key.offset: 2287,
1442+
key.length: 13,
1443+
key.typename: "Chain<A>",
1444+
key.nameoffset: 0,
1445+
key.namelength: 0
1446+
},
1447+
{
1448+
key.kind: source.lang.swift.decl.var.parameter,
1449+
key.name: "rhs",
1450+
key.offset: 2302,
1451+
key.length: 13,
1452+
key.typename: "Chain<A>",
1453+
key.nameoffset: 0,
1454+
key.namelength: 0
1455+
},
1456+
{
1457+
key.kind: source.lang.swift.expr.call,
1458+
key.name: "fatalError",
1459+
key.offset: 2331,
1460+
key.length: 12,
1461+
key.nameoffset: 2331,
1462+
key.namelength: 10,
1463+
key.bodyoffset: 2342,
1464+
key.bodylength: 0
1465+
}
1466+
]
1467+
}
1468+
]
14061469
}
14071470
],
14081471
key.diagnostics: [

test/decl/func/operator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ struct S1 {
243243
}
244244

245245
extension S1 {
246-
func %%%%(lhs: S1, rhs: S1) -> S1 { return lhs } // expected-error{{operator '%%%%' declared in type 'S1' must be 'static'}}{{3-3=static }}
246+
func %%%%(lhs: S1, rhs: S1) -> S1 { return lhs } // expected-error{{operator '%%%%' declared in extension of 'S1' must be 'static'}}{{3-3=static }}
247247
}
248248

249249
class C0 {

0 commit comments

Comments
 (0)