Skip to content

Commit 5bda9bd

Browse files
authored
Merge pull request #36939 from hborla/wrapped-parameter-diagnostics
[ConstraintSystem] Fix a few diagnostic bugs for inferred property wrapper types.
2 parents c475105 + b11f465 commit 5bda9bd

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8369,32 +8369,41 @@ ConstraintSystem::simplifyPropertyWrapperConstraint(
83698369
return SolutionKind::Unsolved;
83708370
}
83718371

8372-
ConstraintFix *fix = nullptr;
8372+
// If the wrapper type is a hole or a dependent member with no type variables,
8373+
// don't record a fix, because this indicates that there was an error
8374+
// elsewhere in the constraint system.
8375+
if (wrapperType->isPlaceholder() || wrapperType->is<DependentMemberType>())
8376+
return SolutionKind::Solved;
8377+
83738378
auto *wrappedVar = getAsDecl<VarDecl>(locator.getAnchor());
83748379
assert(wrappedVar && wrappedVar->hasAttachedPropertyWrapper());
83758380

83768381
// The wrapper type must be a property wrapper.
83778382
auto *nominal = wrapperType->getDesugaredType()->getAnyNominal();
83788383
if (!(nominal && nominal->getAttrs().hasAttribute<PropertyWrapperAttr>())) {
8379-
fix = AllowInvalidPropertyWrapperType::create(*this, wrapperType,
8380-
getConstraintLocator(locator));
8384+
if (shouldAttemptFixes()) {
8385+
auto *fix = AllowInvalidPropertyWrapperType::create(
8386+
*this, wrapperType, getConstraintLocator(locator));
8387+
if (!recordFix(fix))
8388+
return SolutionKind::Solved;
8389+
}
8390+
8391+
return SolutionKind::Error;
83818392
}
83828393

83838394
auto typeInfo = nominal->getPropertyWrapperTypeInfo();
83848395

83858396
// Implicit property wrappers must support projected-value initialization.
8386-
if (!fix && wrappedVar->hasImplicitPropertyWrapper()) {
8387-
if (!(typeInfo.projectedValueVar && typeInfo.hasProjectedValueInit)) {
8388-
fix = RemoveProjectedValueArgument::create(*this, wrapperType, cast<ParamDecl>(wrappedVar),
8389-
getConstraintLocator(locator));
8397+
if (wrappedVar->hasImplicitPropertyWrapper() &&
8398+
!(typeInfo.projectedValueVar && typeInfo.hasProjectedValueInit)) {
8399+
if (shouldAttemptFixes()) {
8400+
auto *fix = RemoveProjectedValueArgument::create(
8401+
*this, wrapperType, cast<ParamDecl>(wrappedVar), getConstraintLocator(locator));
8402+
if (!recordFix(fix))
8403+
return SolutionKind::Solved;
83908404
}
8391-
}
83928405

8393-
if (fix) {
8394-
if (!shouldAttemptFixes() || recordFix(fix))
8395-
return SolutionKind::Error;
8396-
8397-
return SolutionKind::Solved;
8406+
return SolutionKind::Error;
83988407
}
83998408

84008409
auto resolvedType = wrapperType->getTypeOfMember(DC->getParentModule(), typeInfo.valueVar);

test/Sema/property_wrapper_parameter_invalid.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,26 @@ public func testComposition2(@InternalWrapper @PublicWrapper value: Int) {}
164164
// expected-error@+2 {{generic struct 'InternalWrapper' is internal and cannot be referenced from an '@inlinable' function}}
165165
// expected-error@+1 {{initializer 'init(wrappedValue:)' is internal and cannot be referenced from an '@inlinable' function}}
166166
@inlinable func testComposition6(@InternalWrapper @PublicWrapper value: Int) {}
167+
168+
protocol Q {
169+
associatedtype A
170+
}
171+
172+
// expected-note@+1 {{where 'T' = 'Int'}}
173+
func takesClosure<T: Q>(type: T.Type, _ closure: (T.A) -> Void) {}
174+
175+
func testMissingWrapperType() {
176+
// expected-error@+1 {{global function 'takesClosure(type:_:)' requires that 'Int' conform to 'Q'}}
177+
takesClosure(type: Int.self) { $value in
178+
return
179+
}
180+
181+
struct S: Q {
182+
typealias A = (Int, Int)
183+
}
184+
185+
// expected-error@+1 {{inferred projection type 'S.A' (aka '(Int, Int)') is not a property wrapper}}
186+
takesClosure(type: S.self) { $value in
187+
return
188+
}
189+
}

0 commit comments

Comments
 (0)