Skip to content
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

[SE-0031] Adjusting 'inout' Declarations for Type Decoration #1333

Merged
merged 4 commits into from
Feb 26, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Parser] adjust inout declaration for type decoration
This commit implements [SE-0031](//github.com/apple/swift-evolution/blob/master/proposals/0031-adjusting-inout-declarations.md).
When `inout` appears before parameter name, the parser issues a warning and
suggests the correct alternate location for it.

`inout` prefixing the paramter type is now valid.
  • Loading branch information
dduan committed Feb 26, 2016
commit f48487d3fbea6d05efa69881c9577f0beb7e971a
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ WARNING(lex_editor_placeholder_in_playground,none,

NOTE(note_in_decl_extension,none,
"in %select{declaration|extension}0 of %1", (bool, Identifier))
WARNING(inout_as_attr_deprecated,none,
"'inout' as parameter attribute is deprecated, prefix type with it instead.",
Copy link
Contributor

Choose a reason for hiding this comment

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

This reads awkwardly, and diagnostic messages should not end with a period. I'd suggest something like:

'inout' before a parameter name is deprecated, place it before the parameter type instead

())

ERROR(declaration_same_line_without_semi,none,
"consecutive declarations on a line must be separated by ';'", ())
Expand Down
35 changes: 23 additions & 12 deletions lib/Parse/ParsePattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
status |= makeParserCodeCompletionStatus();
}
}

//SourceLoc deprecatedInoutLoc;
// ('inout' | 'let' | 'var')?
if (Tok.is(tok::kw_inout)) {
//deprecatedInoutLoc = Tok.getLoc();
param.LetVarInOutLoc = consumeToken();
param.SpecifierKind = ParsedParameter::InOut;
} else if (Tok.is(tok::kw_let)) {
Expand Down Expand Up @@ -242,8 +243,28 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
param.SecondNameLoc = SourceLoc();
}

// (':' type)?
// (':' ('inout')? type)?
if (consumeIf(tok::colon)) {

SourceLoc postColonLoc = Tok.getLoc();

bool hasDeprecatedInOut =
param.SpecifierKind == ParsedParameter::InOut;

if (Tok.is(tok::kw_inout)) {
SourceLoc deprecatedInOutLoc = param.LetVarInOutLoc;
param.LetVarInOutLoc = consumeToken();
param.SpecifierKind = ParsedParameter::InOut;
if (hasDeprecatedInOut) {
diagnose(deprecatedInOutLoc, diag::inout_as_attr_deprecated)
.fixItRemove(deprecatedInOutLoc);
}
} else if (hasDeprecatedInOut) {
diagnose(param.LetVarInOutLoc, diag::inout_as_attr_deprecated)
.fixItRemove(param.LetVarInOutLoc)
.fixItInsert(postColonLoc, "inout ");
}

auto type = parseType(diag::expected_parameter_type);
status |= type;
param.Type = type.getPtrOrNull();
Expand All @@ -252,16 +273,6 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
// was invalid. Remember that.
if (type.isParseError() && !type.hasCodeCompletion())
param.isInvalid = true;

// Only allow 'inout' before the parameter name.
if (auto InOutTy = dyn_cast_or_null<InOutTypeRepr>(param.Type)) {
SourceLoc InOutLoc = InOutTy->getInOutLoc();
SourceLoc NameLoc = param.FirstNameLoc;
diagnose(InOutLoc, diag::inout_must_appear_before_param)
.fixItRemove(InOutLoc)
.fixItInsert(NameLoc, "inout ");
param.Type = InOutTy->getBase();
}
}
} else {
// Otherwise, we have invalid code. Check to see if this looks like a
Expand Down