Skip to content

Commit 80fb5c1

Browse files
committed
Remove usage of getMetatypeLoc()
1 parent 4f46522 commit 80fb5c1

File tree

10 files changed

+71
-60
lines changed

10 files changed

+71
-60
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,8 +2271,6 @@ WARNING(optional_pattern_match_promotion,none,
22712271
(Type, Type))
22722272

22732273

2274-
ERROR(type_of_metatype,none,
2275-
"'.dynamicType' is not allowed after a type name", ())
22762274
ERROR(invalid_noescape_use,none,
22772275
"non-escaping %select{value|parameter}1 %0 may only be called",
22782276
(Identifier, bool))

include/swift/AST/Expr.h

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3519,28 +3519,34 @@ class AutoClosureExpr : public AbstractClosureExpr {
35193519

35203520
/// DynamicTypeExpr - "type(of: base)" - Produces a metatype value.
35213521
///
3522-
/// The metatype value can comes from evaluating an expression and then
3523-
/// getting its metatype.
3522+
/// The metatype value comes from evaluating an expression then retrieving the
3523+
/// metatype of the result.
35243524
class DynamicTypeExpr : public Expr {
3525+
SourceLoc KeywordLoc;
3526+
SourceLoc LParenLoc;
35253527
Expr *Base;
3526-
SourceLoc MetatypeLoc;
3528+
SourceLoc RParenLoc;
35273529

35283530
public:
3529-
explicit DynamicTypeExpr(Expr *Base, SourceLoc MetatypeLoc, Type Ty)
3531+
explicit DynamicTypeExpr(SourceLoc KeywordLoc, SourceLoc LParenLoc,
3532+
Expr *Base, SourceLoc RParenLoc, Type Ty)
35303533
: Expr(ExprKind::DynamicType, /*Implicit=*/false, Ty),
3531-
Base(Base), MetatypeLoc(MetatypeLoc) { }
3534+
KeywordLoc(KeywordLoc), LParenLoc(LParenLoc), Base(Base),
3535+
RParenLoc(RParenLoc) { }
35323536

35333537
Expr *getBase() const { return Base; }
35343538
void setBase(Expr *base) { Base = base; }
35353539

3536-
SourceLoc getLoc() const { return MetatypeLoc; }
3537-
SourceLoc getMetatypeLoc() const { return MetatypeLoc; }
3538-
3540+
SourceLoc getLoc() const { return KeywordLoc; }
3541+
SourceRange getSourceRange() const {
3542+
return SourceRange(KeywordLoc, RParenLoc);
3543+
}
3544+
35393545
SourceLoc getStartLoc() const {
3540-
return getBase()->getStartLoc();
3546+
return KeywordLoc;
35413547
}
35423548
SourceLoc getEndLoc() const {
3543-
return (MetatypeLoc.isValid() ? MetatypeLoc : getBase()->getEndLoc());
3549+
return RParenLoc;
35443550
}
35453551

35463552
static bool classof(const Expr *E) {

include/swift/Parse/Tokens.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ STMT_KEYWORD(catch)
130130
// Expression keywords.
131131
KEYWORD(as)
132132
KEYWORD(Any)
133-
KEYWORD(dynamicType)
134133
KEYWORD(false)
135134
KEYWORD(is)
136135
KEYWORD(nil)
@@ -148,6 +147,9 @@ KEYWORD(__COLUMN__)
148147
KEYWORD(__FUNCTION__)
149148
KEYWORD(__DSO_HANDLE__)
150149

150+
// FIXME(SE-0096): REMOVE ME
151+
KEYWORD(dynamicType)
152+
151153
// Pattern keywords.
152154
KEYWORD(_)
153155

lib/FrontendTool/FrontendTool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ class JSONFixitWriter : public DiagnosticConsumer {
585585
Info.ID == diag::invalid_ibinspectable.ID ||
586586
Info.ID == diag::invalid_ibaction_decl.ID)
587587
return false;
588-
// Adding .dynamicType interacts poorly with the swift migrator by
588+
// Adding type(of:) interacts poorly with the swift migrator by
589589
// invalidating some inits with type errors.
590590
if (Info.ID == diag::init_not_instance_member.ID)
591591
return false;

lib/Parse/ParseExpr.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,8 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
14201420

14211421
// Handle "x.dynamicType" - A metatype expr.
14221422
// Deprecated in SE-0096: `x.dynamicType` becomes `type(of: x)`
1423+
//
1424+
// FIXME(SE-0096): This will go away along with the keyword soon.
14231425
if (Tok.is(tok::kw_dynamicType)) {
14241426
// Fix-it
14251427
auto range = Result.get()->getSourceRange();
@@ -1429,8 +1431,11 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
14291431
.fixItReplace(dynamicTypeExprRange, ")")
14301432
.fixItInsert(range.Start, "type(of: ");
14311433

1432-
Result = makeParserResult(
1433-
new (Context) DynamicTypeExpr(Result.get(), dynamicTypeExprRange.End, Type()));
1434+
// HACK: Arbitrary.
1435+
auto loc = range.Start;
1436+
auto dt = new (Context) DynamicTypeExpr(loc, loc, Result.get(), loc, Type());
1437+
dt->setImplicit();
1438+
Result = makeParserResult(dt);
14341439
continue;
14351440
}
14361441

@@ -3018,23 +3023,33 @@ Parser::parseVersionConstraintSpec() {
30183023
/// 'type' '(' 'of:' expr ')'
30193024
///
30203025
ParserResult<Expr> Parser::parseExprTypeOf() {
3021-
3022-
assert(Tok.getText() == "type" && "only 'type' should be handled here");
3023-
30243026
// Consume 'type'
30253027
SourceLoc keywordLoc = consumeToken();
3026-
// Consume '('
3028+
3029+
// Parse the leading '('.
30273030
SourceLoc lParenLoc = consumeToken(tok::l_paren);
30283031

3029-
// Parse `of` label
3030-
// If we see a potential argument label followed by a ':', consume
3031-
// it.
3032-
if (Tok.canBeArgumentLabel() && Tok.getText() == "of" && peekToken().is(tok::colon)) {
3032+
// Parse `of` label.
3033+
auto ofRange = Tok.getRange();
3034+
if (Tok.canBeArgumentLabel() && peekToken().is(tok::colon)) {
3035+
bool hasOf = Tok.getText() == "of";
3036+
if (!hasOf) {
3037+
// User mis-spelled the 'of' label.
3038+
diagnose(Tok, diag::expr_typeof_expected_label_of)
3039+
.fixItReplace({ ofRange.getStart(), ofRange.getEnd() }, "of");
3040+
}
3041+
3042+
// Consume either 'of' or the misspelling.
30333043
consumeToken();
30343044
consumeToken(tok::colon);
3045+
3046+
if (!hasOf) {
3047+
return makeParserError();
3048+
}
30353049
} else {
3036-
diagnose(Tok, diag::expr_typeof_expected_label_of);
3037-
return makeParserError();
3050+
// No label at all; insert it.
3051+
diagnose(Tok, diag::expr_typeof_expected_label_of)
3052+
.fixItInsert(ofRange.getStart(), "of: ");
30383053
}
30393054

30403055
// Parse the subexpression.
@@ -3059,15 +3074,18 @@ ParserResult<Expr> Parser::parseExprTypeOf() {
30593074
if (subExpr.isParseError()) {
30603075
if (subExpr.hasCodeCompletion()) {
30613076
auto res = makeParserResult(
3062-
new (Context) DynamicTypeExpr(subExpr.get(), consumeToken(), Type()));
3077+
new (Context) DynamicTypeExpr(keywordLoc, lParenLoc,
3078+
subExpr.get(), rParenLoc,
3079+
Type()));
30633080
res.setHasCodeCompletion();
30643081
return res;
30653082
} else {
30663083
return makeParserResult<Expr>(
3067-
new (Context) ErrorExpr(SourceRange(keywordLoc, rParenLoc)));
3084+
new (Context) ErrorExpr(SourceRange(keywordLoc, rParenLoc)));
30683085
}
30693086
}
30703087

30713088
return makeParserResult(
3072-
new (Context) DynamicTypeExpr(subExpr.get(), rParenLoc, Type()));
3089+
new (Context) DynamicTypeExpr(keywordLoc, lParenLoc,
3090+
subExpr.get(), rParenLoc, Type()));
30733091
}

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,17 +486,9 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
486486
return;
487487

488488
// Allow references to types as a part of:
489-
// - member references T.foo, T.Type, T.self, etc. (but *not* T.type)
489+
// - member references T.foo, T.Type, T.self, etc.
490490
// - constructor calls T()
491491
if (auto *ParentExpr = Parent.getAsExpr()) {
492-
// Reject use of "T.dynamicType", it should be written as "T.self".
493-
if (auto metaExpr = dyn_cast<DynamicTypeExpr>(ParentExpr)) {
494-
// Add a fixit to replace '.dynamicType' with '.self'.
495-
TC.diagnose(E->getStartLoc(), diag::type_of_metatype)
496-
.fixItReplace(metaExpr->getMetatypeLoc(), "self");
497-
return;
498-
}
499-
500492
// This is the white-list of accepted syntactic forms.
501493
if (isa<ErrorExpr>(ParentExpr) ||
502494
isa<DotSelfExpr>(ParentExpr) || // T.self

test/Parse/type_expr.swift

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func unqualifiedType() {
5353
_ = Foo.instMeth
5454

5555
_ = Foo // expected-error{{expected member name or constructor call after type name}} expected-note{{add arguments}} {{10-10=()}} expected-note{{use '.self'}} {{10-10=.self}}
56-
_ = type(of: Foo) // expected-error{{'.dynamicType' is not allowed after a type name}} {{11-22=self}}
5756

5857
_ = Bad // expected-error{{expected member name or constructor call after type name}}
5958
// expected-note@-1{{use '.self' to reference the type object}}{{10-10=.self}}
@@ -70,7 +69,6 @@ func qualifiedType() {
7069
_ = Foo.Bar.instMeth
7170

7271
_ = Foo.Bar // expected-error{{expected member name or constructor call after type name}} expected-note{{add arguments}} {{14-14=()}} expected-note{{use '.self'}} {{14-14=.self}}
73-
_ = type(of: Foo.Bar) // expected-error{{'.dynamicType' is not allowed after a type name}} {{15-26=self}}
7472
}
7573

7674
/* TODO allow '.Type' in expr context
@@ -90,11 +88,9 @@ func genType() {
9088
_ = Gen<Foo>.meth
9189
let _ : () = Gen<Foo>.meth()
9290
_ = Gen<Foo>.instMeth
93-
94-
// Misparses because generic parameter disambiguation rejects '>' not
95-
// followed by '.' or '('
96-
_ = Gen<Foo> // expected-error{{not a postfix unary operator}}
97-
_ = Gen<Foo>.dynamicType // expected-error{{'.dynamicType' is not allowed after a type name}} {{16-27=self}}
91+
_ = Gen<Foo> // expected-error{{expected member name or constructor call after type name}}
92+
// expected-note@-1{{use '.self' to reference the type object}}
93+
// expected-note@-2{{add arguments after the type to construct a value of the type}}
9894
}
9995

10096
func genQualifiedType() {
@@ -106,7 +102,9 @@ func genQualifiedType() {
106102
_ = Gen<Foo>.Bar.instMeth
107103

108104
_ = Gen<Foo>.Bar
109-
_ = type(of: Gen<Foo>.Bar)
105+
_ = type(of: Gen<Foo>.Bar) // No error here.
106+
_ = type(Gen<Foo>.Bar) // expected-error {{expected argument label 'of:' within 'type(...)'}} {{12-12=of: }}
107+
_ = type(fo: Gen<Foo>.Bar) // expected-error {{expected argument label 'of:' within 'type(...)'}}
110108
}
111109

112110
func archetype<T: Zim>(_: T) {
@@ -117,7 +115,6 @@ func archetype<T: Zim>(_: T) {
117115
let _ : () = T.meth()
118116

119117
_ = T // expected-error{{expected member name or constructor call after type name}} expected-note{{add arguments}} {{8-8=()}} expected-note{{use '.self'}} {{8-8=.self}}
120-
_ = type(of: T) // expected-error{{'.dynamicType' is not allowed after a type name}} {{9-20=self}}
121118
}
122119

123120
func assocType<T: Zim>(_: T) where T.Zang: Zim {
@@ -128,7 +125,6 @@ func assocType<T: Zim>(_: T) where T.Zang: Zim {
128125
let _ : () = T.Zang.meth()
129126

130127
_ = T.Zang // expected-error{{expected member name or constructor call after type name}} expected-note{{add arguments}} {{13-13=()}} expected-note{{use '.self'}} {{13-13=.self}}
131-
_ = type(of: T.Zang) // expected-error{{'.dynamicType' is not allowed after a type name}} {{14-25=self}}
132128
}
133129

134130
class B {
@@ -149,7 +145,6 @@ func derivedType() {
149145

150146
let _: B.Type = D // expected-error{{expected member name or constructor call after type name}} expected-note{{add arguments}} {{20-20=()}} expected-note{{use '.self'}} {{20-20=.self}}
151147
let _: D.Type = D // expected-error{{expected member name or constructor call after type name}} expected-note{{add arguments}} {{20-20=()}} expected-note{{use '.self'}} {{20-20=.self}}
152-
let _: D.Type.Type = type(of: D) // expected-error{{'.dynamicType' is not allowed after a type name}} {{26-37=self}}
153148
}
154149

155150
// Referencing a nonexistent member or constructor should not trigger errors

test/SILGen/optional_chain_addressor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
func foo(x: UnsafeMutablePointer<UnsafeMutablePointer<()>?>) {
44
_ = x.pointee?.pointee
5-
_ = type(of: x.pointee?)
5+
_ = type(of: x.pointee)
66
}

test/SILOptimizer/cast_folding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func test18_2() -> Bool {
509509
// CHECK-NEXT: return %1
510510
@inline(never)
511511
func test19() -> Bool {
512-
let t: Any.Type = (1 as Any).dynamicType
512+
let t: Any.Type = type(of: 1 as Any)
513513
return t is Int.Type
514514
}
515515

test/expr/postfix/dot/init_ref_delegation.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ enum Z2 {
101101
// Ill-formed initialization: wrong context.
102102
class Z3 {
103103
func f() {
104-
self.init() // expected-error{{'init' is a member of the type; insert '.dynamicType' to initialize a new object of the same dynamic type}} {{9-9=.dynamicType}}
104+
self.init() // expected-error{{'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type}} {{10-10=type(of: }} {{14-14=)}}
105105
}
106106

107107
init() { }
@@ -112,7 +112,7 @@ class Z4 {
112112
init() {} // expected-note{{selected non-required initializer}}
113113

114114
convenience init(other: Z4) {
115-
other.init() // expected-error{{'init' is a member of the type; insert '.dynamicType' to initialize a new object of the same dynamic type}} {{10-10=.dynamicType}}
115+
other.init() // expected-error{{'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type}} {{11-11=type(of: }} {{15-15=)}}
116116
type(of: other).init() // expected-error{{must use a 'required' initializer}} expected-warning{{unused}}
117117
}
118118
}
@@ -121,7 +121,7 @@ class Z5 : Z4 {
121121
override init() { }
122122

123123
convenience init(other: Z5) {
124-
other.init() // expected-error{{'init' is a member of the type; insert '.dynamicType' to initialize a new object of the same dynamic type}} {{10-10=.dynamicType}}
124+
other.init() // expected-error{{'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type}} {{11-11=type(of: }} {{15-15=)}}
125125
}
126126
}
127127

@@ -150,7 +150,7 @@ struct RDar16603812 {
150150
var i = 42
151151
init() {}
152152
func foo() {
153-
self.init() // expected-error {{'init' is a member of the type; insert '.dynamicType' to initialize a new object of the same dynamic type}} {{11-11=.dynamicType}}
153+
self.init() // expected-error {{'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type}} {{12-12=type(of: }} {{16-16=)}}
154154
type(of: self).init() // expected-warning{{result of 'RDar16603812' initializer is unused}}
155155
}
156156
}
@@ -199,7 +199,7 @@ class D: C {
199199
}
200200

201201
func foo() {
202-
self.init(x: 0) // expected-error{{'init' is a member of the type; insert '.dynamicType' to initialize a new object of the same dynamic type}} {{9-9=.dynamicType}}
202+
self.init(x: 0) // expected-error{{'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type}} {{10-10=type(of: }} {{14-14=)}}
203203
}
204204
func bar() {
205205
super.init(x: 0) // expected-error{{'super.init' cannot be called outside of an initializer}}
@@ -270,10 +270,10 @@ func foo<T: C>(_ x: T, y: T.Type) where T: P {
270270
var c3a = type(of: x).init() // expected-error{{'required' initializer}}
271271
var c4a = type(of: x).init(proto: "")
272272

273-
var ci1 = x.init(required: 0) // expected-error{{'init' is a member of the type; insert '.dynamicType' to initialize a new object of the same dynamic type}} {{14-14=.dynamicType}}
274-
var ci2 = x.init(x: 0) // expected-error{{'init' is a member of the type; insert '.dynamicType' to initialize a new object of the same dynamic type}} {{14-14=.dynamicType}}
275-
var ci3 = x.init() // expected-error{{'init' is a member of the type; insert '.dynamicType' to initialize a new object of the same dynamic type}} {{14-14=.dynamicType}}
276-
var ci4 = x.init(proto: "") // expected-error{{'init' is a member of the type; insert '.dynamicType' to initialize a new object of the same dynamic type}} {{14-14=.dynamicType}}
273+
var ci1 = x.init(required: 0) // expected-error{{'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type}} {{15-15=type(of: }} {{19-19=)}}
274+
var ci2 = x.init(x: 0) // expected-error{{'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type}} {{15-15=type(of: }} {{19-19=)}}
275+
var ci3 = x.init() // expected-error{{'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type}} {{15-15=type(of: }} {{19-19=)}}
276+
var ci4 = x.init(proto: "") // expected-error{{'init' is a member of the type; use 'type(of: ...)' to initialize a new object of the same dynamic type}} {{15-15=type(of: }} {{19-19=)}}
277277

278278
var ci1a = x(required: 0) // expected-error{{cannot call value of non-function type 'T'}}
279279
var ci2a = x(x: 0) // expected-error{{cannot call value of non-function type 'T'}}

0 commit comments

Comments
 (0)