Skip to content

[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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,18 @@ ERROR(expr_selector_expected_property_expr,PointsToFirstBadToken,
ERROR(expr_selector_expected_rparen,PointsToFirstBadToken,
"expected ')' to complete '#selector' expression", ())

// Type-of expressions.
ERROR(expr_typeof_expected_lparen,PointsToFirstBadToken,
"expected '(' following 'type'", ())
ERROR(expr_typeof_expected_label_of,PointsToFirstBadToken,
"expected argument label 'of:' within 'type(...)'", ())
ERROR(expr_typeof_expected_expr,PointsToFirstBadToken,
"expected an expression within 'type(of: ...)'", ())
ERROR(expr_typeof_expected_rparen,PointsToFirstBadToken,
"expected ')' to complete 'type(of: ...)' expression", ())
WARNING(expr_dynamictype_deprecated,PointsToFirstBadToken,
"'.dynamicType' is deprecated. Use 'type(of: ...)' instead", ())

//------------------------------------------------------------------------------
// Attribute-parsing diagnostics
//------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ ERROR(did_not_call_method,none,
(Identifier))

ERROR(init_not_instance_member,none,
"'init' is a member of the type; insert '.dynamicType' to initialize "
"'init' is a member of the type; use 'type(of: ...)' to initialize "
"a new object of the same dynamic type", ())
ERROR(super_initializer_not_in_initializer,none,
"'super.init' cannot be called outside of an initializer", ())
Expand Down
1 change: 1 addition & 0 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,7 @@ class Parser {
ParserResult<Expr> parseExprSuper(bool isExprBasic);
ParserResult<Expr> parseExprConfiguration();
ParserResult<Expr> parseExprStringLiteral();
ParserResult<Expr> parseExprTypeOf();

/// If the token is an escaped identifier being used as an argument
/// label, but doesn't need to be, diagnose it.
Expand Down
78 changes: 77 additions & 1 deletion lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)`
Copy link
Contributor

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.

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(
Expand Down Expand Up @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

The 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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outdent this to be aligned two spaces after makeParserResult and break it across multiple lines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to consumeToken() is incorrect. It should be rParenLoc.

res.setHasCodeCompletion();
return res;
} else {
return makeParserResult<Expr>(
new (Context) ErrorExpr(SourceRange(keywordLoc, rParenLoc)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

}
}

return makeParserResult(
new (Context) DynamicTypeExpr(subExpr.get(), rParenLoc, Type()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

}