-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Refine parameter type 'inout' diagnostics #9147
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -573,3 +573,38 @@ func f(a : FooClass, b : LetStructMembers) { | |
a.baz = 1 // expected-error {{cannot assign to property: 'baz' is a method}} | ||
b.f = 42 // expected-error {{cannot assign to property: 'f' is a method}} | ||
} | ||
|
||
// SR-2354: Reject subscript declarations with mutable parameters. | ||
class MutableSubscripts { | ||
var x : Int = 0 | ||
|
||
subscript(x: inout Int) -> () { x += 1 } // expected-error {{'inout' may not be used on subscript parameters}} | ||
subscript<T>(x: inout T) -> () { // expected-error {{'inout' may not be used on subscript parameters}} | ||
fatalError() | ||
} | ||
|
||
static func initialize(from state: inout MutableSubscripts) -> MutableSubscripts { | ||
return state | ||
} | ||
} | ||
|
||
|
||
// SR-4214: Misleading location-less diagnostic when closure parameter type | ||
// is inferred to be inout. | ||
func sr4214() { | ||
func sequence<T>(_ x : T, _ f : (T) -> T) -> T { | ||
return f(x) | ||
} | ||
|
||
// expected-error@+1 {{expression type '(inout MutableSubscripts) -> ()' is ambiguous without more context}} | ||
let closure = { val in val.x = 7 } as (inout MutableSubscripts) -> () | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this an error? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the parameter is assumed immutable unless the user actually writes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, we infer it inconsistently now, which is terrible, and then give a bad diagnostic in the non-inferred cases, which @CodaFi has improved. There are a handful of bugs about this. |
||
var v = MutableSubscripts() | ||
closure(&v) | ||
// FIXME: This diagnostic isn't really all that much better | ||
// expected-error@+1 {{cannot convert value of type '(inout MutableSubscripts) -> ()' to expected argument type '(_) -> _'}} | ||
sequence(v) { (state : inout MutableSubscripts) -> () in | ||
_ = MutableSubscripts.initialize(from: &state) | ||
return () | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use this to resolve property types also, and remove the non-materializable check there.