Skip to content

Commit

Permalink
Rollup merge of rust-lang#114668 - compiler-errors:match-fn-def, r=pe…
Browse files Browse the repository at this point in the history
…trochenkov

Deny `FnDef` in patterns

We can only see these via `const { .. }` patterns, which are unstable.

cc rust-lang#76001 (tracking issue for inline const pats)

Fixes rust-lang#114658
Fixes rust-lang#114659
  • Loading branch information
matthiaskrgr authored Aug 15, 2023
2 parents 072e134 + 1f42be6 commit 2d64446
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
7 changes: 6 additions & 1 deletion compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ impl<'tcx> ConstToPat<'tcx> {
// `PartialEq::eq` on it.
return Err(FallbackToConstRef);
}
ty::FnDef(..) => {
self.saw_const_match_error.set(true);
tcx.sess.emit_err(InvalidPattern { span, non_sm_ty: ty });
PatKind::Wild
}
ty::Adt(adt_def, _) if !self.type_marked_structural(ty) => {
debug!("adt_def {:?} has !type_marked_structural for cv.ty: {:?}", adt_def, ty,);
self.saw_const_match_error.set(true);
Expand Down Expand Up @@ -440,7 +445,7 @@ impl<'tcx> ConstToPat<'tcx> {
}
}
},
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::FnDef(..) => PatKind::Constant {
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) => PatKind::Constant {
value: mir::ConstantKind::Ty(ty::Const::new_value(tcx, cv, ty)),
},
ty::FnPtr(..) | ty::RawPtr(..) => unreachable!(),
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/inline-const/pat-match-fndef.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(inline_const_pat)]
//~^ WARN the feature `inline_const_pat` is incomplete

fn uwu() {}

fn main() {
let x = [];
match x[123] {
const { uwu } => {}
//~^ ERROR `fn() {uwu}` cannot be used in patterns
_ => {}
}
}
17 changes: 17 additions & 0 deletions tests/ui/inline-const/pat-match-fndef.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
warning: the feature `inline_const_pat` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/pat-match-fndef.rs:1:12
|
LL | #![feature(inline_const_pat)]
| ^^^^^^^^^^^^^^^^
|
= note: see issue #76001 <https://github.com/rust-lang/rust/issues/76001> for more information
= note: `#[warn(incomplete_features)]` on by default

error: `fn() {uwu}` cannot be used in patterns
--> $DIR/pat-match-fndef.rs:9:9
|
LL | const { uwu } => {}
| ^^^^^^^^^^^^^

error: aborting due to previous error; 1 warning emitted

0 comments on commit 2d64446

Please sign in to comment.