Skip to content

[Swift 3.0] diagnostics improvements #4642

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

Merged
merged 2 commits into from
Sep 7, 2016
Merged
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ ERROR(operator_decl_no_fixity,none,
"operator must be declared as 'prefix', 'postfix', or 'infix'", ())

// PrecedenceGroup
ERROR(precedencegroup_not_infix,none,
"only infix operators may declare a precedence", ())
ERROR(expected_precedencegroup_name,none,
"expected identifier after 'precedencegroup'", ())
ERROR(expected_precedencegroup_lbrace,none,
Expand Down
8 changes: 6 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,18 @@ NOTE(any_as_anyobject_fixit, none,
"cast 'Any' to 'AnyObject' or use 'as!' to force downcast to a more specific type to access members", ())

ERROR(expected_argument_in_contextual_member,none,
"contextual member %0 expects argument of type %1", (DeclName, Type))
"member %0 expects argument of type %1", (DeclName, Type))
ERROR(expected_parens_in_contextual_member,none,
"member %0 is a function; did you mean to call it?", (DeclName))

ERROR(expected_result_in_contextual_member,none,
"member %0 in %2 produces result of type %1, but context expects %2",
(DeclName, Type, Type))

ERROR(unexpected_argument_in_contextual_member,none,
"contextual member %0 has no associated value", (DeclName))
"member %0 takes no arguments", (DeclName))
ERROR(unexpected_parens_in_contextual_member,none,
"member %0 is not a function", (DeclName))

ERROR(could_not_use_value_member,none,
"member %1 cannot be used on value of type %0", (Type, DeclName))
Expand Down
16 changes: 4 additions & 12 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -825,18 +825,10 @@ class Parser {

ParserResult<OperatorDecl> parseDeclOperator(ParseDeclOptions Flags,
DeclAttributes &Attributes);
ParserResult<OperatorDecl> parseDeclPrefixOperator(SourceLoc OperatorLoc,
Identifier Name,
SourceLoc NameLoc,
DeclAttributes &Attrs);
ParserResult<OperatorDecl> parseDeclPostfixOperator(SourceLoc OperatorLoc,
Identifier Name,
SourceLoc NameLoc,
DeclAttributes &Attrs);
ParserResult<OperatorDecl> parseDeclInfixOperator(SourceLoc OperatorLoc,
Identifier Name,
SourceLoc NameLoc,
DeclAttributes &Attrs);
ParserResult<OperatorDecl> parseDeclOperatorImpl(SourceLoc OperatorLoc,
Identifier Name,
SourceLoc NameLoc,
DeclAttributes &Attrs);

ParserResult<PrecedenceGroupDecl>
parseDeclPrecedenceGroup(ParseDeclOptions flags, DeclAttributes &attributes);
Expand Down
120 changes: 45 additions & 75 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5574,17 +5574,8 @@ Parser::parseDeclOperator(ParseDeclOptions Flags, DeclAttributes &Attributes) {

Identifier Name = Context.getIdentifier(Tok.getText());
SourceLoc NameLoc = consumeToken();

ParserResult<OperatorDecl> Result;
if (Attributes.hasAttribute<PrefixAttr>())
Result = parseDeclPrefixOperator(OperatorLoc, Name, NameLoc, Attributes);
else if (Attributes.hasAttribute<PostfixAttr>())
Result = parseDeclPostfixOperator(OperatorLoc, Name, NameLoc, Attributes);
else {
if (!Attributes.hasAttribute<InfixAttr>())
diagnose(OperatorLoc, diag::operator_decl_no_fixity);
Result = parseDeclInfixOperator(OperatorLoc, Name, NameLoc, Attributes);
}

auto Result = parseDeclOperatorImpl(OperatorLoc, Name, NameLoc, Attributes);

if (!DCC.movedToTopLevel() && !AllowTopLevel) {
diagnose(OperatorLoc, diag::operator_decl_inner_scope);
Expand All @@ -5595,89 +5586,68 @@ Parser::parseDeclOperator(ParseDeclOptions Flags, DeclAttributes &Attributes) {
}

ParserResult<OperatorDecl>
Parser::parseDeclPrefixOperator(SourceLoc OperatorLoc, Identifier Name,
Parser::parseDeclOperatorImpl(SourceLoc OperatorLoc, Identifier Name,
SourceLoc NameLoc, DeclAttributes &Attributes) {
SourceLoc lBraceLoc;
if (consumeIf(tok::l_brace, lBraceLoc)) {
auto Diag = diagnose(lBraceLoc, diag::deprecated_operator_body);
if (Tok.is(tok::r_brace)) {
SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken(SourceMgr,
NameLoc);
SourceLoc rBraceEnd = Lexer::getLocForEndOfToken(SourceMgr, Tok.getLoc());
Diag.fixItRemoveChars(lastGoodLocEnd, rBraceEnd);
}

skipUntilDeclRBrace();
(void) consumeIf(tok::r_brace);
}
bool isPrefix = Attributes.hasAttribute<PrefixAttr>();
bool isInfix = Attributes.hasAttribute<InfixAttr>();
bool isPostfix = Attributes.hasAttribute<PostfixAttr>();

auto *Res = new (Context) PrefixOperatorDecl(CurDeclContext, OperatorLoc,
Name, NameLoc);
Res->getAttrs() = Attributes;
return makeParserResult(Res);
}

ParserResult<OperatorDecl>
Parser::parseDeclPostfixOperator(SourceLoc OperatorLoc,
Identifier Name, SourceLoc NameLoc,
DeclAttributes &Attributes) {
SourceLoc lBraceLoc;
if (consumeIf(tok::l_brace, lBraceLoc)) {
auto Diag = diagnose(lBraceLoc, diag::deprecated_operator_body);
if (Tok.is(tok::r_brace)) {
SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken(SourceMgr,
NameLoc);
SourceLoc rBraceEnd = Lexer::getLocForEndOfToken(SourceMgr, Tok.getLoc());
Diag.fixItRemoveChars(lastGoodLocEnd, rBraceEnd);
}

skipUntilDeclRBrace();
(void) consumeIf(tok::r_brace);
}

auto Res = new (Context) PostfixOperatorDecl(CurDeclContext, OperatorLoc,
Name, NameLoc);
Res->getAttrs() = Attributes;
return makeParserResult(Res);
}

ParserResult<OperatorDecl>
Parser::parseDeclInfixOperator(SourceLoc operatorLoc, Identifier name,
SourceLoc nameLoc, DeclAttributes &attributes) {
// Parse (or diagnose) a specified precedence group.
SourceLoc colonLoc;
Identifier precedenceGroupName;
SourceLoc precedenceGroupNameLoc;
if (consumeIf(tok::colon, colonLoc)) {
if (Tok.is(tok::identifier)) {
precedenceGroupName = Context.getIdentifier(Tok.getText());
precedenceGroupNameLoc = consumeToken(tok::identifier);

if (isPrefix || isPostfix)
diagnose(colonLoc, diag::precedencegroup_not_infix)
.fixItRemove({colonLoc, precedenceGroupNameLoc});
}
}


// Diagnose deprecated operator body syntax `operator + { ... }`.
SourceLoc lBraceLoc;
if (consumeIf(tok::l_brace, lBraceLoc)) {
if (Tok.is(tok::r_brace)) {
SourceLoc lastGoodLoc = precedenceGroupNameLoc;
if (lastGoodLoc.isInvalid())
lastGoodLoc = nameLoc;
SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken(SourceMgr,
lastGoodLoc);
SourceLoc rBraceEnd = Lexer::getLocForEndOfToken(SourceMgr, Tok.getLoc());
diagnose(lBraceLoc, diag::deprecated_operator_body)
.fixItRemoveChars(lastGoodLocEnd, rBraceEnd);
} else {
if (isInfix && !Tok.is(tok::r_brace)) {
diagnose(lBraceLoc, diag::deprecated_operator_body_use_group);
} else {
auto Diag = diagnose(lBraceLoc, diag::deprecated_operator_body);
if (Tok.is(tok::r_brace)) {
SourceLoc lastGoodLoc = precedenceGroupNameLoc;
if (lastGoodLoc.isInvalid())
lastGoodLoc = NameLoc;
SourceLoc lastGoodLocEnd = Lexer::getLocForEndOfToken(SourceMgr,
lastGoodLoc);
SourceLoc rBraceEnd = Lexer::getLocForEndOfToken(SourceMgr, Tok.getLoc());
Diag.fixItRemoveChars(lastGoodLocEnd, rBraceEnd);
}
}

skipUntilDeclRBrace();
(void) consumeIf(tok::r_brace);
}

auto res = new (Context) InfixOperatorDecl(CurDeclContext, operatorLoc,
name, nameLoc, colonLoc,
precedenceGroupName,
precedenceGroupNameLoc);
res->getAttrs() = attributes;


OperatorDecl *res;
if (Attributes.hasAttribute<PrefixAttr>())
res = new (Context) PrefixOperatorDecl(CurDeclContext, OperatorLoc,
Name, NameLoc);
else if (Attributes.hasAttribute<PostfixAttr>())
res = new (Context) PostfixOperatorDecl(CurDeclContext, OperatorLoc,
Name, NameLoc);
else {
if (!Attributes.hasAttribute<InfixAttr>())
diagnose(OperatorLoc, diag::operator_decl_no_fixity);

res = new (Context) InfixOperatorDecl(CurDeclContext, OperatorLoc,
Name, NameLoc, colonLoc,
precedenceGroupName,
precedenceGroupNameLoc);
}

res->getAttrs() = Attributes;
return makeParserResult(res);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,7 @@ namespace {
Expr *Guard = nullptr;
};

/// Contexts in which a guarded pattern can appears.
/// Contexts in which a guarded pattern can appear.
enum class GuardedPatternContext {
Case,
Catch,
Expand Down
48 changes: 31 additions & 17 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6049,7 +6049,7 @@ bool FailureDiagnosis::visitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
return true;
}

auto argumentTy = candidateInfo[0].getArgumentType();
auto candidateArgTy = candidateInfo[0].getArgumentType();

// Depending on how we matched, produce tailored diagnostics.
switch (candidateInfo.closeness) {
Expand All @@ -6068,9 +6068,9 @@ bool FailureDiagnosis::visitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
case CC_ExactMatch: { // This is a perfect match for the arguments.

// If we have an exact match, then we must have an argument list, check it.
if (argumentTy) {
if (candidateArgTy) {
assert(E->getArgument() && "Exact match without argument?");
if (!typeCheckArgumentChildIndependently(E->getArgument(), argumentTy,
if (!typeCheckArgumentChildIndependently(E->getArgument(), candidateArgTy,
candidateInfo))
return true;
}
Expand Down Expand Up @@ -6104,16 +6104,16 @@ bool FailureDiagnosis::visitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {

case CC_ArgumentLabelMismatch: { // Argument labels are not correct.
auto argExpr = typeCheckArgumentChildIndependently(E->getArgument(),
argumentTy,
candidateArgTy,
candidateInfo);
if (!argExpr) return true;

// Construct the actual expected argument labels that our candidate
// expected.
assert(argumentTy &&
assert(candidateArgTy &&
"Candidate must expect an argument to have a label mismatch");
SmallVector<Identifier, 2> argLabelsScratch;
auto arguments = decomposeArgType(argumentTy,
auto arguments = decomposeArgType(candidateArgTy,
candidateInfo[0].getArgumentLabels(
argLabelsScratch));

Expand All @@ -6131,26 +6131,40 @@ bool FailureDiagnosis::visitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
case CC_ArgumentCountMismatch: // This candidate has wrong # arguments.
// If we have no argument, the candidates must have expected one.
if (!E->getArgument()) {
if (!argumentTy)
if (!candidateArgTy)
return false; // Candidate must be incorrect for some other reason.

// Pick one of the arguments that are expected as an exemplar.
diagnose(E->getNameLoc(), diag::expected_argument_in_contextual_member,
E->getName(), argumentTy);
if (candidateArgTy->isVoid()) {
// If this member is () -> T, suggest adding parentheses.
diagnose(E->getNameLoc(), diag::expected_parens_in_contextual_member,
E->getName())
.fixItInsertAfter(E->getEndLoc(), "()");
} else {
diagnose(E->getNameLoc(), diag::expected_argument_in_contextual_member,
E->getName(), candidateArgTy);
}
return true;
}

// If an argument value was specified, but this is a simple enumerator, then
// we fail with a nice error message.
auto argTy = candidateInfo[0].getArgumentType();
if (!argTy) {
diagnose(E->getNameLoc(), diag::unexpected_argument_in_contextual_member,
E->getName());
// If an argument value was specified, but this member expects no arguments,
// then we fail with a nice error message.
if (!candidateArgTy) {
if (E->getArgument()->getType()->isVoid()) {
diagnose(E->getNameLoc(), diag::unexpected_parens_in_contextual_member,
E->getName())
.fixItRemove(E->getArgument()->getSourceRange());
} else {
diagnose(E->getNameLoc(), diag::unexpected_argument_in_contextual_member,
E->getName())
.highlight(E->getArgument()->getSourceRange());
}
return true;
}

assert(E->getArgument() && argTy && "Exact match without an argument?");
return !typeCheckArgumentChildIndependently(E->getArgument(), argTy,
assert(E->getArgument() && candidateArgTy &&
"Exact match without an argument?");
return !typeCheckArgumentChildIndependently(E->getArgument(), candidateArgTy,
candidateInfo);
}

Expand Down
4 changes: 2 additions & 2 deletions test/Constraints/diagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,13 @@ let _: [Color] = [1,2].map { _ in .Unknown("") }// expected-error {{missing argu

let _: (Int) -> (Int, Color) = { ($0, .Unknown("")) } // expected-error {{missing argument label 'description:' in call}} {{48-48=description: }}
let _: Color = .Unknown("") // expected-error {{missing argument label 'description:' in call}} {{25-25=description: }}
let _: Color = .Unknown // expected-error {{contextual member 'Unknown' expects argument of type '(description: String)'}}
let _: Color = .Unknown // expected-error {{member 'Unknown' expects argument of type '(description: String)'}}
let _: Color = .Unknown(42) // expected-error {{cannot convert value of type 'Int' to expected argument type 'String'}}
let _ : Color = .rainbow(42) // expected-error {{argument passed to call that takes no arguments}}

let _ : (Int, Float) = (42.0, 12) // expected-error {{cannot convert value of type 'Double' to specified type 'Int'}}

let _ : Color = .rainbow // expected-error {{contextual member 'rainbow' expects argument of type '()'}}
let _ : Color = .rainbow // expected-error {{member 'rainbow' is a function; did you mean to call it?}} {{25-25=()}}

let _: Color = .overload(a : 1.0) // expected-error {{cannot convert value of type 'Double' to expected argument type 'Int'}}
let _: Color = .overload(1.0) // expected-error {{ambiguous reference to member 'overload'}}
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ enum SomeErrorType {

static func someErrorFromString(_ str: String) -> SomeErrorType? {
if str == "standalone" { return .StandaloneError }
if str == "underlying" { return .UnderlyingError } // expected-error {{contextual member 'UnderlyingError' expects argument of type 'String'}}
if str == "underlying" { return .UnderlyingError } // expected-error {{member 'UnderlyingError' expects argument of type 'String'}}
return nil
}
}
Expand Down
28 changes: 27 additions & 1 deletion test/Parse/operator_decl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,33 @@ infix operator +++ {} // expected-warning {{operator should no longer be declare
infix operator +++* { // expected-warning {{operator should no longer be declared with body; use a precedence group instead}} {{none}}
associativity right
}
infix operator +++** : A { } // expected-warning {{operator should no longer be declared with body}} {{25-29=}}
infix operator +++*+ : A { } // expected-warning {{operator should no longer be declared with body}} {{25-29=}}


prefix operator +++** : A { }
// expected-error@-1 {{only infix operators may declare a precedence}} {{23-27=}}
// expected-warning@-2 {{operator should no longer be declared with body}} {{26-30=}}

prefix operator ++*++ : A
// expected-error@-1 {{only infix operators may declare a precedence}} {{23-26=}}

postfix operator ++*+* : A { }
// expected-error@-1 {{only infix operators may declare a precedence}} {{24-28=}}
// expected-warning@-2 {{operator should no longer be declared with body}} {{27-31=}}

postfix operator ++**+ : A
// expected-error@-1 {{only infix operators may declare a precedence}} {{24-27=}}

operator ++*** : A
// expected-error@-1 {{operator must be declared as 'prefix', 'postfix', or 'infix'}}

operator +*+++ { }
// expected-error@-1 {{operator must be declared as 'prefix', 'postfix', or 'infix'}}
// expected-warning@-2 {{operator should no longer be declared with body}} {{15-19=}}

operator +*++* : A { }
// expected-error@-1 {{operator must be declared as 'prefix', 'postfix', or 'infix'}}
// expected-warning@-2 {{operator should no longer be declared with body}} {{19-23=}}

prefix operator // expected-error {{expected operator name in operator declaration}}

Expand Down
5 changes: 3 additions & 2 deletions test/decl/enum/enumtest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ func f() {
}

func union_error(_ a: ZeroOneTwoThree) {
var _ : ZeroOneTwoThree = .Zero(1) // expected-error {{contextual member 'Zero' has no associated value}}
var _ : ZeroOneTwoThree = .One // expected-error {{contextual member 'One' expects argument of type 'Int'}}
var _ : ZeroOneTwoThree = .Zero(1) // expected-error {{member 'Zero' takes no arguments}}
var _ : ZeroOneTwoThree = .Zero() // expected-error {{member 'Zero' is not a function}} {{34-36=}}
var _ : ZeroOneTwoThree = .One // expected-error {{member 'One' expects argument of type 'Int'}}
var _ : ZeroOneTwoThree = .foo // expected-error {{type 'ZeroOneTwoThree' has no member 'foo'}}
var _ : ZeroOneTwoThree = .foo() // expected-error {{type 'ZeroOneTwoThree' has no member 'foo'}}
}
Expand Down