Skip to content

Commit d07e73e

Browse files
committed
[TypeChecker] SE-0347: Perform syntactic diagnostics after inference
Implementation of type inference from default expressions (SE-0347) omitted running syntactic diagnostics on the type-checked default expression which leads to invalid code being accepted by the type-checker. Resolves: #62025
1 parent b526205 commit d07e73e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,8 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
732732
cs.applySolution(solution);
733733

734734
if (auto result = cs.applySolution(solution, defaultExprTarget)) {
735+
// Perform syntactic diagnostics on the type-checked target.
736+
performSyntacticDiagnosticsForTarget(*result, /*isExprStmt=*/false);
735737
defaultValue = result->getAsExpr();
736738
return defaultValue->getType();
737739
}

test/Constraints/type_inference_from_default_exprs.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ protocol StorageType {
208208
var identifier: String { get }
209209
}
210210

211-
class Storage {
211+
class Storage { // expected-note {{class 'Storage' is not public}}
212212
}
213213

214214
extension Storage {
@@ -253,3 +253,16 @@ struct S61061_2<T> where T:Hashable {
253253
struct S61061_3<T> where T:Hashable {
254254
init(x:[(T, T)] = [(1, 1)]) {} // OK
255255
}
256+
257+
// https://github.com/apple/swift/issues/62025
258+
// Syntactic checks are not run on the default argument expressions
259+
public struct MyStruct {} // expected-note {{initializer 'init()' is not public}}
260+
public func issue62025_with_init<T>(_: T = MyStruct()) {}
261+
// expected-error@-1 {{initializer 'init()' is internal and cannot be referenced from a default argument value}}
262+
public func issue62025_with_type<T>(_: T = Storage.self) {}
263+
// expected-error@-1 {{class 'Storage' is internal and cannot be referenced from a default argument value}}
264+
do {
265+
func default_with_dup_keys<T>(_: T = ["a": 1, "a": 2]) {}
266+
// expected-warning@-1 {{dictionary literal of type '[String : Int]' has duplicate entries for string literal key 'a'}}
267+
// expected-note@-2 2 {{duplicate key declared here}}
268+
}

0 commit comments

Comments
 (0)