Skip to content

Commit a6040bc

Browse files
committed
Specify type kind of constant that can't be used in patterns
``` error: trait object `dyn Send` cannot be used in patterns --> $DIR/issue-70972-dyn-trait.rs:6:9 | LL | const F: &'static dyn Send = &7u32; | -------------------------- constant defined here ... LL | F => panic!(), | ^ trait object can't be used in patterns ```
1 parent 253eb95 commit a6040bc

File tree

9 files changed

+28
-20
lines changed

9 files changed

+28
-20
lines changed

compiler/rustc_mir_build/messages.ftl

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ mir_build_inline_assembly_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
152152
153153
mir_build_interpreted_as_const = introduce a variable instead
154154
155-
mir_build_invalid_pattern = `{$non_sm_ty}` cannot be used in patterns
155+
mir_build_invalid_pattern = {$prefix} `{$non_sm_ty}` cannot be used in patterns
156+
.label = {$prefix} can't be used in patterns
156157
157158
mir_build_irrefutable_let_patterns_if_let = irrefutable `if let` {$count ->
158159
[one] pattern

compiler/rustc_mir_build/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,10 @@ pub(crate) struct TypeNotPartialEq<'tcx> {
903903
#[diag(mir_build_invalid_pattern)]
904904
pub(crate) struct InvalidPattern<'tcx> {
905905
#[primary_span]
906+
#[label]
906907
pub(crate) span: Span,
907908
pub(crate) non_sm_ty: Ty<'tcx>,
909+
pub(crate) prefix: String,
908910
}
909911

910912
#[derive(Diagnostic)]

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ impl<'tcx> ConstToPat<'tcx> {
171171
ty::FnPtr(..) | ty::RawPtr(..) => {
172172
self.tcx.dcx().create_err(PointerPattern { span: self.span })
173173
}
174-
_ => self
175-
.tcx
176-
.dcx()
177-
.create_err(InvalidPattern { span: self.span, non_sm_ty: bad_ty }),
174+
_ => self.tcx.dcx().create_err(InvalidPattern {
175+
span: self.span,
176+
non_sm_ty: bad_ty,
177+
prefix: bad_ty.prefix_string(self.tcx).to_string(),
178+
}),
178179
};
179180
return self.mk_err(e, ty);
180181
}
@@ -373,7 +374,11 @@ impl<'tcx> ConstToPat<'tcx> {
373374
)
374375
}
375376
_ => {
376-
let err = InvalidPattern { span, non_sm_ty: ty };
377+
let err = InvalidPattern {
378+
span,
379+
non_sm_ty: ty,
380+
prefix: ty.prefix_string(self.tcx).to_string(),
381+
};
377382
return Err(tcx.dcx().create_err(err));
378383
}
379384
};
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: `fn() {uwu}` cannot be used in patterns
1+
error: fn item `fn() {uwu}` cannot be used in patterns
22
--> $DIR/pat-match-fndef.rs:8:9
33
|
44
LL | const { uwu } => {}
5-
| ^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^ fn item can't be used in patterns
66

77
error: aborting due to 1 previous error
88

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error: `dyn Send` cannot be used in patterns
1+
error: trait object `dyn Send` cannot be used in patterns
22
--> $DIR/issue-70972-dyn-trait.rs:6:9
33
|
44
LL | const F: &'static dyn Send = &7u32;
55
| -------------------------- constant defined here
66
...
77
LL | F => panic!(),
8-
| ^
8+
| ^ trait object can't be used in patterns
99

1010
error: aborting due to 1 previous error
1111

tests/ui/pattern/issue-72565.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error: `dyn PartialEq<u32>` cannot be used in patterns
1+
error: trait object `dyn PartialEq<u32>` cannot be used in patterns
22
--> $DIR/issue-72565.rs:6:9
33
|
44
LL | const F: &'static dyn PartialEq<u32> = &7u32;
55
| ------------------------------------ constant defined here
66
...
77
LL | F => panic!(),
8-
| ^
8+
| ^ trait object can't be used in patterns
99

1010
error: aborting due to 1 previous error
1111

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
error: `{closure@$DIR/non-structural-match-types.rs:9:17: 9:19}` cannot be used in patterns
1+
error: closure `{closure@$DIR/non-structural-match-types.rs:9:17: 9:19}` cannot be used in patterns
22
--> $DIR/non-structural-match-types.rs:9:9
33
|
44
LL | const { || {} } => {}
5-
| ^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^ closure can't be used in patterns
66

7-
error: `{async block@$DIR/non-structural-match-types.rs:12:17: 12:22}` cannot be used in patterns
7+
error: `async` block `{async block@$DIR/non-structural-match-types.rs:12:17: 12:22}` cannot be used in patterns
88
--> $DIR/non-structural-match-types.rs:12:9
99
|
1010
LL | const { async {} } => {}
11-
| ^^^^^^^^^^^^^^^^^^
11+
| ^^^^^^^^^^^^^^^^^^ `async` block can't be used in patterns
1212

1313
error: aborting due to 2 previous errors
1414

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error: `Bar` cannot be used in patterns
1+
error: opaque type `Bar` cannot be used in patterns
22
--> $DIR/structural-match-no-leak.rs:16:9
33
|
44
LL | const LEAK_FREE: bar::Bar = bar::leak_free();
55
| ------------------------- constant defined here
66
...
77
LL | LEAK_FREE => (),
8-
| ^^^^^^^^^
8+
| ^^^^^^^^^ opaque type can't be used in patterns
99

1010
error: aborting due to 1 previous error
1111

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error: `foo::Foo` cannot be used in patterns
1+
error: opaque type `foo::Foo` cannot be used in patterns
22
--> $DIR/structural-match.rs:18:9
33
|
44
LL | const VALUE: Foo = value();
55
| ---------------- constant defined here
66
...
77
LL | VALUE => (),
8-
| ^^^^^
8+
| ^^^^^ opaque type can't be used in patterns
99

1010
error: aborting due to 1 previous error
1111

0 commit comments

Comments
 (0)