Skip to content

Commit 5c3f670

Browse files
committed
Remove diag::type_of_expression_is_ambiguous
This is a diagnostic that is only really emitted as a fallback when the constraint system isn't able to better diagnose the expression. It's not particulary helpful for the user, and can be often be misleading since the underlying issue might not actually be an ambiguity, and the user may well already have a type annotation. Let's instead just emit the fallback diagnostic that we emit in all other cases, asking the user to file a bug.
1 parent a554080 commit 5c3f670

21 files changed

+51
-59
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,15 +4749,10 @@ ERROR(could_not_infer_pack_element,none,
47494749
NOTE(note_in_opening_pack_element,none,
47504750
"in inferring pack element #%0 of '%1'", (unsigned,StringRef))
47514751

4752-
4753-
ERROR(type_of_expression_is_ambiguous,none,
4754-
"type of expression is ambiguous without a type annotation", ())
4755-
47564752
ERROR(failed_to_produce_diagnostic,none,
47574753
"failed to produce diagnostic for expression; "
47584754
SWIFT_BUG_REPORT_MESSAGE, ())
47594755

4760-
47614756
ERROR(missing_protocol,none,
47624757
"missing protocol %0", (Identifier))
47634758
ERROR(nil_literal_broken_proto,none,

lib/Sema/ConstraintSystem.cpp

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4768,6 +4768,21 @@ void ConstraintSystem::diagnoseFailureFor(SyntacticElementTarget target) {
47684768
return;
47694769
}
47704770

4771+
if (auto *wrappedVar = target.getAsUninitializedWrappedVar()) {
4772+
auto *outerWrapper = wrappedVar->getOutermostAttachedPropertyWrapper();
4773+
Type propertyType = wrappedVar->getInterfaceType();
4774+
Type wrapperType = outerWrapper->getType();
4775+
4776+
// Emit the property wrapper fallback diagnostic
4777+
wrappedVar->diagnose(diag::property_wrapper_incompatible_property,
4778+
propertyType, wrapperType);
4779+
if (auto nominal = wrapperType->getAnyNominal()) {
4780+
nominal->diagnose(diag::property_wrapper_declared_here,
4781+
nominal->getName());
4782+
}
4783+
return;
4784+
}
4785+
47714786
if (auto expr = target.getAsExpr()) {
47724787
if (auto *assignment = dyn_cast<AssignExpr>(expr)) {
47734788
if (isa<DiscardAssignmentExpr>(assignment->getDest()))
@@ -4785,33 +4800,12 @@ void ConstraintSystem::diagnoseFailureFor(SyntacticElementTarget target) {
47854800
.highlight(closure->getSourceRange());
47864801
return;
47874802
}
4788-
4789-
// If no one could find a problem with this expression or constraint system,
4790-
// then it must be well-formed... but is ambiguous. Handle this by
4791-
// diagnostic various cases that come up.
4792-
DE.diagnose(expr->getLoc(), diag::type_of_expression_is_ambiguous)
4793-
.highlight(expr->getSourceRange());
4794-
} else if (auto *wrappedVar = target.getAsUninitializedWrappedVar()) {
4795-
auto *outerWrapper = wrappedVar->getOutermostAttachedPropertyWrapper();
4796-
Type propertyType = wrappedVar->getInterfaceType();
4797-
Type wrapperType = outerWrapper->getType();
4798-
4799-
// Emit the property wrapper fallback diagnostic
4800-
wrappedVar->diagnose(diag::property_wrapper_incompatible_property,
4801-
propertyType, wrapperType);
4802-
if (auto nominal = wrapperType->getAnyNominal()) {
4803-
nominal->diagnose(diag::property_wrapper_declared_here,
4804-
nominal->getName());
4805-
}
4806-
} else if (target.getAsUninitializedVar()) {
4807-
DE.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
4808-
} else if (target.isForEachPreamble()) {
4809-
DE.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
4810-
} else {
4811-
// Emit a poor fallback message.
4812-
DE.diagnose(target.getAsFunction()->getLoc(),
4813-
diag::failed_to_produce_diagnostic);
48144803
}
4804+
4805+
// Emit a poor fallback message.
4806+
auto diag = DE.diagnose(target.getLoc(), diag::failed_to_produce_diagnostic);
4807+
if (auto *expr = target.getAsExpr())
4808+
diag.highlight(expr->getSourceRange());
48154809
}
48164810

48174811
bool ConstraintSystem::isDeclUnavailable(const Decl *D,

test/Concurrency/sendable_keypaths.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ do {
224224

225225
// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
226226
func test() -> KeyPath<String, Int> {
227-
true ? kp() : kp() // expected-error {{type of expression is ambiguous without a type annotation}}
227+
true ? kp() : kp() // expected-error {{failed to produce diagnostic for expression}}
228228
}
229229

230230
func forward<T>(_ v: T) -> T { v }
@@ -249,7 +249,7 @@ do {
249249

250250
// TODO(rdar://125948508): This shouldn't be ambiguous (@Sendable version should be preferred)
251251
func fnRet(cond: Bool) -> () -> Void {
252-
cond ? Test.fn : Test.otherFn // expected-error {{type of expression is ambiguous without a type annotation}}
252+
cond ? Test.fn : Test.otherFn // expected-error {{failed to produce diagnostic for expression}}
253253
}
254254

255255
func forward<T>(_: T) -> T {

test/Constraints/keypath.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func test_invalid_argument_to_keypath_subscript() {
290290
// The diagnostic should point out that `ambiguous` is indeed ambiguous and that `5` is not a valid argument
291291
// for a key path subscript.
292292
ambiguous {
293-
// expected-error@-1 {{type of expression is ambiguous without a type annotation}}
293+
// expected-error@-1 {{failed to produce diagnostic for expression}}
294294
$0[keyPath: 5]
295295
}
296296

test/Constraints/operator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func rdar60727310() {
285285
// FIXME: Bad diagnostic.
286286
func f_54877(_ e: Error) {
287287
func foo<T>(_ a: T, _ op: ((T, T) -> Bool)) {}
288-
foo(e, ==) // expected-error {{type of expression is ambiguous without a type annotation}}
288+
foo(e, ==) // expected-error {{failed to produce diagnostic for expression}}
289289
}
290290

291291
// rdar://problem/62054241 - Swift compiler crashes when passing < as the sort function in sorted(by:) and the type of the array is not comparable

test/Constraints/parameterized_existentials.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ protocol P<A> {
88

99
func f1(x: any P) -> any P<Int> {
1010
// FIXME: Bad diagnostic
11-
return x // expected-error {{type of expression is ambiguous without a type annotation}}
11+
return x // expected-error {{failed to produce diagnostic for expression}}
1212
}
1313

1414
func f2(x: any P<Int>) -> any P {
@@ -52,4 +52,4 @@ func h3(x: (any P<Int>)?) -> (any P<String>)? {
5252

5353
func generic1<T>(x: any P<T>) -> T {
5454
return x.f()
55-
}
55+
}

test/Constraints/rdar85263844_swift6.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ extension S4 where T == (outer: Int, y: Int) {
2929

3030
public func rdar85263844_2(_ x: [Int]) -> S4<(outer: Int, y: Int)> {
3131
// FIXME: Bad error message.
32-
S4(x.map { (inner: $0, y: $0) }) // expected-error {{type of expression is ambiguous without a type annotation}}
32+
S4(x.map { (inner: $0, y: $0) }) // expected-error {{failed to produce diagnostic for expression}}
3333
}

test/Constraints/variadic_generic_constraints.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ takesAnyObject()
4141
takesAnyObject(C(), C(), C())
4242

4343
// FIXME: Bad diagnostic
44-
takesAnyObject(C(), S(), C()) // expected-error {{type of expression is ambiguous without a type annotation}}
44+
takesAnyObject(C(), S(), C()) // expected-error {{failed to produce diagnostic for expression}}
4545

4646
// Same-type requirements
4747

test/Interop/Cxx/foreign-reference/not-any-object.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ import Test;
2222
public func test(_ _: AnyObject) {}
2323

2424
// TODO: make this a better error.
25-
test(Empty.create()) // expected-error {{type of expression is ambiguous without a type annotation}}
25+
test(Empty.create()) // expected-error {{failed to produce diagnostic for expression}}
2626
test([Empty.create()][0]) // expected-error {{argument type 'Any' expected to be an instance of a class or class-constrained type}}

test/Interop/Cxx/namespace/import-as-member-typechecker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ func takesDeepNestedStruct(_ s: MyNS.MyDeepNS.DeepNestedStruct) {
2121
}
2222

2323
MyNS.method() // expected-error {{type 'MyNS' has no member 'method'}}
24-
MyNS.nestedMethod() // expected-error {{type of expression is ambiguous without a type annotation}}
24+
MyNS.nestedMethod() // expected-error {{failed to produce diagnostic for expression}}

0 commit comments

Comments
 (0)