-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SE-0096] [Parse] Implement type(of:)
expression
#3871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1095,6 +1095,12 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) { | |
} | ||
|
||
case tok::identifier: // foo | ||
// If starts with 'type(', parse the 'type(of: ...)' metatype expression | ||
if (Tok.getText() == "type" && peekToken().is(tok::l_paren)) { | ||
Result = parseExprTypeOf(); | ||
break; | ||
} | ||
|
||
// If we are parsing a refutable pattern and are inside a let/var pattern, | ||
// the identifiers change to be value bindings instead of decl references. | ||
// Parse and return this as an UnresolvedPatternExpr around a binding. This | ||
|
@@ -1413,12 +1419,22 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) { | |
} | ||
|
||
// Handle "x.dynamicType" - A metatype expr. | ||
// Deprecated in SE-0096: `x.dynamicType` becomes `type(of: x)` | ||
if (Tok.is(tok::kw_dynamicType)) { | ||
// Fix-it | ||
auto range = Result.get()->getSourceRange(); | ||
auto dynamicTypeExprRange = SourceRange(TokLoc, consumeToken()); | ||
diagnose(TokLoc, diag::expr_dynamictype_deprecated) | ||
.highlight(dynamicTypeExprRange) | ||
.fixItReplace(dynamicTypeExprRange, ")") | ||
.fixItInsert(range.Start, "type(of: "); | ||
|
||
Result = makeParserResult( | ||
new (Context) DynamicTypeExpr(Result.get(), consumeToken(), Type())); | ||
new (Context) DynamicTypeExpr(Result.get(), dynamicTypeExprRange.End, Type())); | ||
continue; | ||
} | ||
|
||
|
||
// Handle "x.self" expr. | ||
if (Tok.is(tok::kw_self)) { | ||
Result = makeParserResult( | ||
|
@@ -2995,3 +3011,63 @@ Parser::parseVersionConstraintSpec() { | |
return makeParserResult(new (Context) VersionConstraintAvailabilitySpec( | ||
Platform.getValue(), PlatformLoc, Version, VersionRange)); | ||
} | ||
|
||
/// parseExprTypeOf | ||
/// | ||
/// expr-dynamictype: | ||
/// 'type' '(' 'of:' expr ')' | ||
/// | ||
ParserResult<Expr> Parser::parseExprTypeOf() { | ||
|
||
assert(Tok.getText() == "type" && "only 'type' should be handled here"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assert is nice to have but isn't needed. |
||
|
||
// Consume 'type' | ||
SourceLoc keywordLoc = consumeToken(); | ||
// Consume '(' | ||
SourceLoc lParenLoc = consumeToken(tok::l_paren); | ||
|
||
// Parse `of` label | ||
// If we see a potential argument label followed by a ':', consume | ||
// it. | ||
if (Tok.canBeArgumentLabel() && Tok.getText() == "of" && peekToken().is(tok::colon)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 80 column limit here. |
||
consumeToken(); | ||
consumeToken(tok::colon); | ||
} else { | ||
diagnose(Tok, diag::expr_typeof_expected_label_of); | ||
return makeParserError(); | ||
} | ||
|
||
// Parse the subexpression. | ||
ParserResult<Expr> subExpr = parseExpr(diag::expr_typeof_expected_expr); | ||
if (subExpr.hasCodeCompletion()) | ||
return makeParserCodeCompletionResult<Expr>(); | ||
|
||
// Parse the closing ')' | ||
SourceLoc rParenLoc; | ||
if (subExpr.isParseError()) { | ||
skipUntilDeclStmtRBrace(tok::r_paren); | ||
if (Tok.is(tok::r_paren)) | ||
rParenLoc = consumeToken(); | ||
else | ||
rParenLoc = Tok.getLoc(); | ||
} else { | ||
parseMatchingToken(tok::r_paren, rParenLoc, | ||
diag::expr_typeof_expected_rparen, lParenLoc); | ||
} | ||
|
||
// If the subexpression was in error, just propagate the error. | ||
if (subExpr.isParseError()) { | ||
if (subExpr.hasCodeCompletion()) { | ||
auto res = makeParserResult( | ||
new (Context) DynamicTypeExpr(subExpr.get(), consumeToken(), Type())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Outdent this to be aligned two spaces after There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The call to |
||
res.setHasCodeCompletion(); | ||
return res; | ||
} else { | ||
return makeParserResult<Expr>( | ||
new (Context) ErrorExpr(SourceRange(keywordLoc, rParenLoc))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
} | ||
} | ||
|
||
return makeParserResult( | ||
new (Context) DynamicTypeExpr(subExpr.get(), rParenLoc, Type())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thx for pointing it out. Must be some misformatting when I had the patch applied. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add
FIXME(SE-0096)
here so we know to come around and remove this.