Skip to content

[PropertyWrappers] Mark the property as invalid when there is a mismatch with wrappedValue type #27995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2980,6 +2980,16 @@ bool ConstraintSystem::repairFailures(
locator);
}

// Let's not complain about argument type mismatch if we have a synthesized
// wrappedValue initializer, because we already emit a diagnostic for a
// type mismatch between the property's type and the wrappedValue type.
if (auto CE = dyn_cast<CallExpr>(loc->getAnchor())) {
if (CE->isImplicit() && !CE->getArgumentLabels().empty() &&
CE->getArgumentLabels().front() == getASTContext().Id_wrappedValue) {
break;
}
}

conversionsOrFixes.push_back(
AllowArgumentMismatch::create(*this, lhs, rhs, loc));
break;
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckPropertyWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ PropertyWrapperBackingPropertyTypeRequest::evaluate(
if (cs.solve(nullptr, solutions) || solutions.size() != 1) {
var->diagnose(diag::property_wrapper_incompatible_property,
propertyType, rawType);
var->setInvalid();
if (auto nominalWrapper = rawType->getAnyNominal()) {
nominalWrapper->diagnose(diag::property_wrapper_declared_here,
nominalWrapper->getFullName());
Expand Down
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,7 @@ PropertyWrapperBackingPropertyInfoRequest::evaluate(Evaluator &evaluator,
!propertyType->isEqual(expectedPropertyType)) {
var->diagnose(diag::property_wrapper_incompatible_property,
propertyType, wrapperType);
var->setInvalid();
if (auto nominalWrapper = wrapperType->getAnyNominal()) {
nominalWrapper->diagnose(diag::property_wrapper_declared_here,
nominalWrapper->getFullName());
Expand Down
6 changes: 1 addition & 5 deletions test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,8 @@ struct Initialization {
@WrapperWithInitialValue
var y = true

// FIXME: For some reason this is type-checked twice, second time around solver complains about <<error type>> argument
@WrapperWithInitialValue<Int>
var y2 = true // expected-error{{cannot convert value of type 'Bool' to expected argument type 'Int'}}
// expected-error@-1 {{cannot convert value of type '<<error type>>' to expected argument type 'Int'}}
var y2 = true // expected-error{{Bool' is not convertible to 'Int}}

mutating func checkTypes(s: String) {
x2 = s // expected-error{{cannot assign value of type 'String' to type 'Double'}}
Expand Down Expand Up @@ -1584,9 +1582,7 @@ extension SR_11288_P4 where Self: AnyObject { // expected-note 2 {{where 'Self'
}

struct SR_11288_S4: SR_11288_P4 {
// FIXME: We shouldn't diagnose the arg-to-param mismatch (rdar://problem/56345248)
@SR_11288_Wrapper4 var answer = 42 // expected-error 2 {{referencing type alias 'SR_11288_Wrapper4' on 'SR_11288_P4' requires that 'SR_11288_S4' be a class type}}
// expected-error @-1 {{cannot convert value of type '<<error type>>' to expected argument type 'Int'}}
}

class SR_11288_C0: SR_11288_P4 {
Expand Down
13 changes: 13 additions & 0 deletions validation-test/compiler_crashers_2_fixed/sr11600.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: not %target-swift-frontend -typecheck %s

@propertyWrapper struct A {
var wrappedValue: Int
}

@propertyWrapper struct B {
var wrappedValue: Int
}

struct C {
@A @B var foo: Int?
}
21 changes: 21 additions & 0 deletions validation-test/compiler_crashers_2_fixed/sr11684.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-swift-frontend %s -typecheck -verify

@propertyWrapper
struct Wrapper1 { // expected-note {{property wrapper type 'Wrapper1' declared here}}
var wrappedValue: Int?
}

class Test1 {
@Wrapper1 var user: Int
// expected-error@-1 {{property type 'Int' does not match that of the 'wrappedValue' property of its wrapper type 'Wrapper1'}}
}

@propertyWrapper
struct Wrapper2 { // expected-note {{property wrapper type 'Wrapper2' declared here}}
var wrappedValue: Int??
}

class Test2 {
@Wrapper2 var user: Int?
// expected-error@-1 {{property type 'Int?' does not match that of the 'wrappedValue' property of its wrapper type 'Wrapper2'}}
}