Skip to content

Commit be1f82e

Browse files
committed
[CS] Avoid emitting duplicate note for generic argument diagnostic
Since we can run CSGen multiple times, we need to guard the emission of the note on whether the TypeRepr was already marked invalid (i.e whether we already emitted a diagnostic for it).
1 parent b7519fa commit be1f82e

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,8 @@ namespace {
19571957
void addSpecializationConstraint(ConstraintLocator *locator, Type boundType,
19581958
SourceLoc lAngleLoc,
19591959
ArrayRef<TypeRepr *> specializationArgs) {
1960+
auto &ctx = CS.getASTContext();
1961+
19601962
// Resolve each type.
19611963
SmallVector<Type, 2> specializationArgTypes;
19621964
auto options =
@@ -1971,6 +1973,7 @@ namespace {
19711973
options |= TypeResolutionFlags::AllowPackReferences;
19721974
elementEnv = OuterExpansions.back();
19731975
}
1976+
auto alreadyInvalid = specializationArg->isInvalid();
19741977
auto result = TypeResolution::resolveContextualType(
19751978
specializationArg, CurDC, options,
19761979
// Introduce type variables for unbound generics.
@@ -1980,19 +1983,20 @@ namespace {
19801983
OpenGenericTypeRequirements(CS, locator,
19811984
/*preparedOverload*/ nullptr));
19821985
if (result->hasError()) {
1983-
auto &ctxt = CS.getASTContext();
1986+
if (!alreadyInvalid) {
1987+
ctx.Diags.diagnose(lAngleLoc,
1988+
diag::while_parsing_as_left_angle_bracket);
1989+
}
19841990
CS.recordFix(IgnoreInvalidASTNode::create(
19851991
CS, CS.getConstraintLocator(argLocator)));
1986-
result = PlaceholderType::get(ctxt, specializationArg);
1987-
ctxt.Diags.diagnose(lAngleLoc,
1988-
diag::while_parsing_as_left_angle_bracket);
1992+
result = PlaceholderType::get(ctx, specializationArg);
19891993
}
19901994
specializationArgTypes.push_back(result);
19911995
}
19921996

19931997
auto constraint = Constraint::create(
19941998
CS, ConstraintKind::ExplicitGenericArguments, boundType,
1995-
PackType::get(CS.getASTContext(), specializationArgTypes), locator);
1999+
PackType::get(ctx, specializationArgTypes), locator);
19962000
CS.addUnsolvedConstraint(constraint);
19972001
CS.activateConstraint(constraint);
19982002
}

test/Constraints/generics.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,3 +1113,18 @@ func testHolePropagation() {
11131113
_ = { () -> (S<R>, Int) in return (makeT(), 0) } // expected-error {{type 'R' does not conform to protocol 'P'}}
11141114
_ = { () -> (S<R>, Int) in (); return (makeT(), 0) } // expected-error {{type 'R' does not conform to protocol 'P'}}
11151115
}
1116+
1117+
@freestanding(expression) macro overloadedMacro<T>() -> String = #file
1118+
@freestanding(expression) macro overloadedMacro<T>(_ x: T = 0) -> String = #file
1119+
1120+
do {
1121+
func foo(_ fn: () -> Int) {}
1122+
func foo(_ fn: () -> String) {}
1123+
1124+
// Make sure we only emit a single note here.
1125+
foo {
1126+
#overloadedMacro < Undefined >
1127+
// expected-error@-1 {{cannot find type 'Undefined' in scope}}
1128+
// expected-note@-2 {{while parsing this '<' as a type parameter bracket}}
1129+
}
1130+
}

0 commit comments

Comments
 (0)