Skip to content

Commit 1fa166b

Browse files
committed
NCGenerics: implicit casts require 'consume'
It's better if this is an error from the start. (cherry picked from commit 77e8fb0)
1 parent aab6469 commit 1fa166b

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7774,10 +7774,10 @@ ERROR(noncopyable_objc_enum, none,
77747774
"noncopyable enums cannot be marked '@objc'", ())
77757775
ERROR(moveOnly_not_allowed_here,none,
77767776
"'@_moveOnly' attribute is only valid on structs or enums", ())
7777-
WARNING(consume_expression_needed_for_cast,none,
7777+
ERROR(consume_expression_needed_for_cast,none,
77787778
"implicit conversion to %0 is consuming", (Type))
7779-
NOTE(add_consume_to_silence_warning,none,
7780-
"add 'consume' to silence this warning", ())
7779+
NOTE(add_consume_to_silence,none,
7780+
"add 'consume' to make consumption explicit", ())
77817781
ERROR(consume_expression_not_passed_lvalue,none,
77827782
"'consume' can only be applied to a local binding ('let', 'var', or parameter)", ())
77837783
ERROR(consume_expression_partial_copyable,none,

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5549,7 +5549,7 @@ namespace {
55495549
cs.getType(coercion));
55505550
ctx.Diags
55515551
.diagnose(coercion->getLoc(),
5552-
diag::add_consume_to_silence_warning)
5552+
diag::add_consume_to_silence)
55535553
.fixItInsert(coercion->getStartLoc(), "consume ");
55545554
}
55555555
}

test/Sema/moveonly_casts.swift

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ func testExplicitCasts() {
1919
func testCalls() {
2020
let method = MethodSir()
2121
let foo = NC()
22-
testBorrowing(foo) // expected-warning {{implicit conversion to 'NC?' is consuming}}
23-
// expected-note@-1 {{add 'consume' to silence this warning}} {{17-17=consume }}
22+
testBorrowing(foo) // expected-error {{implicit conversion to 'NC?' is consuming}}
23+
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{17-17=consume }}
2424
testBorrowing(consume foo)
2525
testBorrowing(foo as NC?)
2626

27-
method.borrow(foo) // expected-warning {{implicit conversion to 'NC?' is consuming}}
28-
// expected-note@-1 {{add 'consume' to silence this warning}} {{17-17=consume }}
27+
method.borrow(foo) // expected-error {{implicit conversion to 'NC?' is consuming}}
28+
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{17-17=consume }}
2929
method.borrow(consume foo)
3030

3131
testConsuming(foo)
@@ -59,8 +59,8 @@ func delay(_ f: @autoclosure () -> NC?) -> NC? { f() }
5959

6060
func testDelay() {
6161
let nc = NC()
62-
let _ = delay(nc) // expected-warning {{implicit conversion to 'NC?' is consuming}}
63-
// expected-note@-1 {{add 'consume' to silence this warning}} {{17-17=consume }}
62+
let _ = delay(nc) // expected-error {{implicit conversion to 'NC?' is consuming}}
63+
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{17-17=consume }}
6464
}
6565

6666
struct PropCity {
@@ -74,14 +74,14 @@ struct PropCity {
7474
func chk(_ t: borrowing NC!) {}
7575
func chkWithDefaultArg(_ oath: borrowing NC? = NC()) {}
7676
func test(_ nc: consuming NC) {
77-
chk(nc) // expected-warning {{implicit conversion to 'NC?' is consuming}}
78-
// expected-note@-1 {{add 'consume' to silence this warning}} {{9-9=consume }}
77+
chk(nc) // expected-error {{implicit conversion to 'NC?' is consuming}}
78+
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{9-9=consume }}
7979

8080
chk(consume nc)
8181

8282
chkWithDefaultArg()
83-
chkWithDefaultArg(nc) // expected-warning {{implicit conversion to 'NC?' is consuming}}
84-
// expected-note@-1 {{add 'consume' to silence this warning}} {{23-23=consume }}
83+
chkWithDefaultArg(nc) // expected-error {{implicit conversion to 'NC?' is consuming}}
84+
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{23-23=consume }}
8585
}
8686
}
8787

@@ -93,16 +93,16 @@ func restockConsume(_ x: consuming any Veggie & ~Copyable) {}
9393

9494
func checkExistential() {
9595
let carrot = Carrot()
96-
restockBorrow(carrot) // expected-warning {{implicit conversion to 'any Veggie & ~Copyable' is consuming}}
97-
// expected-note@-1 {{add 'consume' to silence this warning}} {{17-17=consume }}
96+
restockBorrow(carrot) // expected-error {{implicit conversion to 'any Veggie & ~Copyable' is consuming}}
97+
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{17-17=consume }}
9898
restockBorrow(consume carrot)
9999

100100
restockConsume(carrot)
101101
}
102102

103103
func genericErasure<T: Veggie & ~Copyable>(_ veg: consuming T) {
104-
restockBorrow(veg) // expected-warning {{implicit conversion to 'any Veggie & ~Copyable' is consuming}}
105-
// expected-note@-1 {{add 'consume' to silence this warning}} {{17-17=consume }}
104+
restockBorrow(veg) // expected-error {{implicit conversion to 'any Veggie & ~Copyable' is consuming}}
105+
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{17-17=consume }}
106106
restockBorrow(consume veg)
107107
restockBorrow(veg as any Veggie & ~Copyable)
108108
restockConsume(veg)
@@ -115,12 +115,11 @@ extension Veggie where Self: ~Copyable {
115115
}
116116
extension Carrot {
117117
consuming func check() {
118-
inspect(self) // expected-warning {{implicit conversion to 'any Veggie & ~Copyable' is consuming}}
119-
// expected-note@-1 {{add 'consume' to silence this warning}} {{13-13=consume }}
118+
inspect(self) // expected-error {{implicit conversion to 'any Veggie & ~Copyable' is consuming}}
119+
// expected-note@-1 {{add 'consume' to make consumption explicit}} {{13-13=consume }}
120120
inspect(consume self)
121121
inspect(self as any Veggie & ~Copyable)
122122

123123
let _: any Veggie & ~Copyable = self
124124
}
125125
}
126-

0 commit comments

Comments
 (0)