Skip to content

Commit 8e8d1bd

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 f7edbcb commit 8e8d1bd

File tree

9 files changed

+28
-20
lines changed

9 files changed

+28
-20
lines changed

compiler/rustc_mir_build/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ mir_build_inline_assembly_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
142142
143143
mir_build_interpreted_as_const = introduce a variable instead
144144
145-
mir_build_invalid_pattern = `{$non_sm_ty}` cannot be used in patterns
145+
mir_build_invalid_pattern = {$prefix} `{$non_sm_ty}` cannot be used in patterns
146+
.label = {$prefix} can't be used in patterns
146147
147148
mir_build_irrefutable_let_patterns_if_let = irrefutable `if let` {$count ->
148149
[one] pattern

compiler/rustc_mir_build/src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,8 +814,10 @@ pub(crate) struct TypeNotPartialEq<'tcx> {
814814
#[diag(mir_build_invalid_pattern)]
815815
pub(crate) struct InvalidPattern<'tcx> {
816816
#[primary_span]
817+
#[label]
817818
pub(crate) span: Span,
818819
pub(crate) non_sm_ty: Ty<'tcx>,
820+
pub(crate) prefix: String,
819821
}
820822

821823
#[derive(Diagnostic)]

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

Lines changed: 10 additions & 5 deletions
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
};
Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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)