Skip to content

[Sema] Add param indicate diagnose or not in lookupPrecedenceGroupForInfixOperator #61066

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 1 commit into from
Sep 14, 2022
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
7 changes: 4 additions & 3 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8110,7 +8110,8 @@ bool swift::exprNeedsParensInsideFollowingOperator(
DeclContext *DC, Expr *expr,
PrecedenceGroupDecl *followingPG) {
if (expr->isInfixOperator()) {
auto exprPG = TypeChecker::lookupPrecedenceGroupForInfixOperator(DC, expr);
auto exprPG = TypeChecker::lookupPrecedenceGroupForInfixOperator(
DC, expr, /*diagnose=*/false);
if (!exprPG) return true;

return DC->getASTContext().associateInfixOperators(exprPG, followingPG)
Expand Down Expand Up @@ -8160,8 +8161,8 @@ bool swift::exprNeedsParensOutsideFollowingOperator(
return false;

if (parent->isInfixOperator()) {
auto parentPG = TypeChecker::lookupPrecedenceGroupForInfixOperator(DC,
parent);
auto parentPG = TypeChecker::lookupPrecedenceGroupForInfixOperator(
DC, parent, /*diagnose=*/false);
if (!parentPG) return true;

// If the index is 0, this is on the LHS of the parent.
Expand Down
49 changes: 31 additions & 18 deletions lib/Sema/TypeCheckExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,73 +107,84 @@ Expr *TypeChecker::substituteInputSugarTypeForResult(ApplyExpr *E) {
static PrecedenceGroupDecl *lookupPrecedenceGroupForOperator(DeclContext *DC,
Identifier name,
SourceLoc loc) {
auto *op = DC->lookupInfixOperator(name).getSingleOrDiagnose(loc);
auto result = DC->lookupInfixOperator(name);
auto *op =
loc.isValid() ? result.getSingleOrDiagnose(loc) : result.getSingle();
return op ? op->getPrecedenceGroup() : nullptr;
}

PrecedenceGroupDecl *
TypeChecker::lookupPrecedenceGroupForInfixOperator(DeclContext *DC, Expr *E) {
TypeChecker::lookupPrecedenceGroupForInfixOperator(DeclContext *DC, Expr *E,
bool diagnose) {
/// Look up the builtin precedence group with the given name.

auto getBuiltinPrecedenceGroup = [&](DeclContext *DC, Identifier name,
SourceLoc loc) -> PrecedenceGroupDecl * {
auto groups = TypeChecker::lookupPrecedenceGroup(DC, name, loc);
return groups.getSingleOrDiagnose(loc, /*forBuiltin*/ true);
return loc.isValid() ? groups.getSingleOrDiagnose(loc, /*forBuiltin*/ true)
: groups.getSingle();
};

auto &Context = DC->getASTContext();
if (auto ifExpr = dyn_cast<IfExpr>(E)) {
// Ternary has fixed precedence.
return getBuiltinPrecedenceGroup(DC, Context.Id_TernaryPrecedence,
ifExpr->getQuestionLoc());
diagnose ? ifExpr->getQuestionLoc()
: SourceLoc());
}

if (auto assignExpr = dyn_cast<AssignExpr>(E)) {
// Assignment has fixed precedence.
return getBuiltinPrecedenceGroup(DC, Context.Id_AssignmentPrecedence,
assignExpr->getEqualLoc());
diagnose ? assignExpr->getEqualLoc()
: SourceLoc());
}

if (auto castExpr = dyn_cast<ExplicitCastExpr>(E)) {
// 'as' and 'is' casts have fixed precedence.
return getBuiltinPrecedenceGroup(DC, Context.Id_CastingPrecedence,
castExpr->getAsLoc());
diagnose ? castExpr->getAsLoc()
: SourceLoc());
}

if (auto *DRE = dyn_cast<DeclRefExpr>(E)) {
Identifier name = DRE->getDecl()->getBaseIdentifier();
return lookupPrecedenceGroupForOperator(DC, name, DRE->getLoc());
return lookupPrecedenceGroupForOperator(
DC, name, diagnose ? DRE->getLoc() : SourceLoc());
}

if (auto *OO = dyn_cast<OverloadedDeclRefExpr>(E)) {
Identifier name = OO->getDecls()[0]->getBaseIdentifier();
return lookupPrecedenceGroupForOperator(DC, name, OO->getLoc());
return lookupPrecedenceGroupForOperator(
DC, name, diagnose ? OO->getLoc() : SourceLoc());
}

if (auto arrowExpr = dyn_cast<ArrowExpr>(E)) {
return getBuiltinPrecedenceGroup(DC,
Context.Id_FunctionArrowPrecedence,
arrowExpr->getArrowLoc());
return getBuiltinPrecedenceGroup(DC, Context.Id_FunctionArrowPrecedence,
diagnose ? arrowExpr->getArrowLoc()
: SourceLoc());
}

// An already-folded binary operator comes up for non-primary use cases
// of this function.
if (auto binaryExpr = dyn_cast<BinaryExpr>(E)) {
return lookupPrecedenceGroupForInfixOperator(DC, binaryExpr->getFn());
return lookupPrecedenceGroupForInfixOperator(DC, binaryExpr->getFn(),
diagnose);
}

if (auto *DSCE = dyn_cast<DotSyntaxCallExpr>(E)) {
return lookupPrecedenceGroupForInfixOperator(DC, DSCE->getFn());
return lookupPrecedenceGroupForInfixOperator(DC, DSCE->getFn(), diagnose);
}

if (auto *MRE = dyn_cast<MemberRefExpr>(E)) {
Identifier name = MRE->getDecl().getDecl()->getBaseIdentifier();
return lookupPrecedenceGroupForOperator(DC, name, MRE->getLoc());
return lookupPrecedenceGroupForOperator(
DC, name, diagnose ? MRE->getLoc() : SourceLoc());
}

// If E is already an ErrorExpr, then we've diagnosed it as invalid already,
// otherwise emit an error.
if (!isa<ErrorExpr>(E))
if (diagnose && !isa<ErrorExpr>(E))
Context.Diags.diagnose(E->getLoc(), diag::unknown_binop);

return nullptr;
Expand Down Expand Up @@ -203,7 +214,7 @@ Expr *TypeChecker::findLHS(DeclContext *DC, Expr *E, Identifier name) {
continue;
}

auto left = lookupPrecedenceGroupForInfixOperator(DC, E);
auto left = lookupPrecedenceGroupForInfixOperator(DC, E, /*diagnose=*/true);
if (!left)
// LHS is not binary expression.
return E;
Expand Down Expand Up @@ -425,7 +436,8 @@ static Expr *foldSequence(DeclContext *DC,
Expr *op = S[0];

// If the operator's precedence is lower than the minimum, stop here.
auto opPrecedence = TypeChecker::lookupPrecedenceGroupForInfixOperator(DC, op);
auto opPrecedence = TypeChecker::lookupPrecedenceGroupForInfixOperator(
DC, op, /*diagnose=*/true);
if (!precedenceBound.shouldConsider(opPrecedence))
return {nullptr, nullptr};
return {op, opPrecedence};
Expand Down Expand Up @@ -457,7 +469,8 @@ static Expr *foldSequence(DeclContext *DC,
}

// Pull out the next binary operator.
Op op2{ S[0], TypeChecker::lookupPrecedenceGroupForInfixOperator(DC, S[0]) };
Op op2{S[0], TypeChecker::lookupPrecedenceGroupForInfixOperator(
DC, S[0], /*diagnose=*/true)};

// If the second operator's precedence is lower than the
// precedence bound, break out of the loop.
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,8 @@ lookupMemberType(DeclContext *dc, Type type, DeclNameRef name,

/// Given an expression that's known to be an infix operator,
/// look up its precedence group.
PrecedenceGroupDecl *lookupPrecedenceGroupForInfixOperator(DeclContext *dc,
Expr *op);
PrecedenceGroupDecl *
lookupPrecedenceGroupForInfixOperator(DeclContext *dc, Expr *op, bool diagnose);

PrecedenceGroupLookupResult
lookupPrecedenceGroup(DeclContext *dc, Identifier name, SourceLoc nameLoc);
Expand Down
13 changes: 13 additions & 0 deletions test/Constraints/operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,16 @@ do {
let number = 1
let test = true || number == .First.rawValue // expected-error {{type 'Int' has no member 'First'}}
}

// https://github.com/apple/swift/issues/60954
enum I60954 {
// expected-error@+1{{operator implementation without matching operator declaration}}
func ?= (pattern: I60954?, version: Self) { // expected-error{{operator '?=' declared in type 'I60954' must be 'static'}}
// expected-error@+2{{operator is not a known binary operator}}
// expected-error@+1{{initializer 'init(_:)' requires that 'I60954' conform to 'StringProtocol'}}
pattern ?= .init(version) // expected-error{{value of optional type 'I60954?' must be unwrapped to a value of type 'I60954'}}
// expected-note@-1{{coalesce using '??' to provide a default when the optional value contains 'nil'}}
// expected-note@-2{{force-unwrap using '!' to abort execution if the optional value contains 'nil'}}
}
init?<S>(_ string: S) where S: StringProtocol {} // expected-note{{where 'S' = 'I60954'}}
}