Skip to content

Commit e7cb6bb

Browse files
committed
[Sema] Diagnose cross-file operator redeclarations
Previously we permitted operator redeclarations across files as the user could shadow imported operator decls on a per-file basis, and we would prefer an imported operator decl over one in another file. However with the new operator lookup implementation, operators in the current module always shadow imported decls. As such, we should start diagnosing cross-file redeclarations when the new lookup logic is enabled. Resolves rdar://48731166
1 parent cc062ee commit e7cb6bb

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,18 +436,23 @@ static void checkOperatorOrPrecedenceGroupRedeclaration(
436436
if (other == decl || other->isInvalid())
437437
continue;
438438

439-
// Emit a redeclaration error if the two declarations occur in the same
440-
// source file. We currently allow redeclarations across source files to
441-
// allow the user to shadow operator decls from imports, as we currently
442-
// favor those decls over ones from other files.
443-
// FIXME: Once we prefer operator decls from the same module, start
444-
// diagnosing redeclarations across files.
439+
bool shouldDiagnose = false;
445440
if (currentFile == other->getDeclContext()->getParentSourceFile()) {
446-
// Make sure we get the diagnostic ordering to be sensible.
441+
// For a same-file redeclaration, make sure we get the diagnostic ordering
442+
// to be sensible.
447443
if (decl->getLoc().isValid() && other->getLoc().isValid() &&
448444
ctx.SourceMgr.isBeforeInBuffer(decl->getLoc(), other->getLoc())) {
449445
std::swap(decl, other);
450446
}
447+
shouldDiagnose = true;
448+
} else {
449+
// If the declarations are in different files, only diagnose if we've
450+
// enabled the new operator lookup behaviour where decls in the current
451+
// module are now favored over imports.
452+
shouldDiagnose = ctx.LangOpts.EnableNewOperatorLookup;
453+
}
454+
455+
if (shouldDiagnose) {
451456
ctx.Diags.diagnose(decl, diagID);
452457
ctx.Diags.diagnose(other, noteID);
453458
decl->setInvalid();

0 commit comments

Comments
 (0)