Skip to content

Commit 414b6f7

Browse files
committed
avoid creating an Instance only to immediately disassemble it again
1 parent 11637c9 commit 414b6f7

8 files changed

+36
-98
lines changed

compiler/rustc_mir_build/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ mir_build_already_borrowed = cannot borrow value as mutable because it is also b
44
55
mir_build_already_mut_borrowed = cannot borrow value as immutable because it is also borrowed as mutable
66
7-
mir_build_assoc_const_in_pattern = associated consts cannot be referenced in patterns
8-
97
mir_build_bindings_with_variant_name =
108
pattern binding `{$name}` is named the same as one of the variants of the type `{$ty_path}`
119
.suggestion = to match on the variant, qualify the path

compiler/rustc_mir_build/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,6 @@ pub(crate) struct StaticInPattern {
566566
pub(crate) span: Span,
567567
}
568568

569-
#[derive(Diagnostic)]
570-
#[diag(mir_build_assoc_const_in_pattern, code = E0158)]
571-
pub(crate) struct AssocConstInPattern {
572-
#[primary_span]
573-
pub(crate) span: Span,
574-
}
575-
576569
#[derive(Diagnostic)]
577570
#[diag(mir_build_const_param_in_pattern, code = E0158)]
578571
pub(crate) struct ConstParamInPattern {

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

+2-31
Original file line numberDiff line numberDiff line change
@@ -548,37 +548,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
548548
_ => return pat_from_kind(self.lower_variant_or_leaf(res, id, span, ty, vec![])),
549549
};
550550

551-
// Use `Reveal::All` here because patterns are always monomorphic even if their function
552-
// isn't.
553-
let param_env_reveal_all = self.param_env.with_reveal_all_normalized(self.tcx);
554-
// N.B. There is no guarantee that args collected in typeck results are fully normalized,
555-
// so they need to be normalized in order to pass to `Instance::resolve`, which will ICE
556-
// if given unnormalized types.
557-
let args = self
558-
.tcx
559-
.normalize_erasing_regions(param_env_reveal_all, self.typeck_results.node_args(id));
560-
let instance = match ty::Instance::try_resolve(self.tcx, param_env_reveal_all, def_id, args)
561-
{
562-
Ok(Some(i)) => i,
563-
Ok(None) => {
564-
// It should be assoc consts if there's no error but we cannot resolve it.
565-
debug_assert!(is_associated_const);
566-
567-
let e = self.tcx.dcx().emit_err(AssocConstInPattern { span });
568-
return pat_from_kind(PatKind::Error(e));
569-
}
570-
571-
Err(_) => {
572-
let e = self.tcx.dcx().emit_err(CouldNotEvalConstPattern { span });
573-
return pat_from_kind(PatKind::Error(e));
574-
}
575-
};
576-
577-
let c = ty::Const::new_unevaluated(
578-
self.tcx,
579-
ty::UnevaluatedConst { def: instance.def_id(), args: instance.args },
580-
);
581-
551+
let args = self.typeck_results.node_args(id);
552+
let c = ty::Const::new_unevaluated(self.tcx, ty::UnevaluatedConst { def: def_id, args });
582553
let pattern = self.const_to_pat(c, ty, id, span);
583554

584555
if !is_associated_const {

tests/ui/associated-consts/associated-const-type-parameter-arms.stderr

-15
This file was deleted.

tests/ui/associated-consts/associated-const-type-parameter-arms.rs renamed to tests/ui/associated-consts/associated-const-type-parameter-pattern.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ impl Foo for Def {
1818
pub fn test<A: Foo, B: Foo>(arg: EFoo) {
1919
match arg {
2020
A::X => println!("A::X"),
21-
//~^ error: associated consts cannot be referenced in patterns [E0158]
21+
//~^ error: constant pattern depends on a generic parameter
2222
B::X => println!("B::X"),
23-
//~^ error: associated consts cannot be referenced in patterns [E0158]
23+
//~^ error: constant pattern depends on a generic parameter
2424
_ => (),
2525
}
2626
}
2727

28+
pub fn test_let_pat<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
29+
//~^ ERROR constant pattern depends on a generic parameter
30+
let A::X = arg;
31+
//~^ ERROR constant pattern depends on a generic parameter
32+
}
33+
2834
fn main() {
2935
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: constant pattern depends on a generic parameter
2+
--> $DIR/associated-const-type-parameter-pattern.rs:20:9
3+
|
4+
LL | A::X => println!("A::X"),
5+
| ^^^^
6+
7+
error: constant pattern depends on a generic parameter
8+
--> $DIR/associated-const-type-parameter-pattern.rs:22:9
9+
|
10+
LL | B::X => println!("B::X"),
11+
| ^^^^
12+
13+
error: constant pattern depends on a generic parameter
14+
--> $DIR/associated-const-type-parameter-pattern.rs:30:9
15+
|
16+
LL | let A::X = arg;
17+
| ^^^^
18+
19+
error: constant pattern depends on a generic parameter
20+
--> $DIR/associated-const-type-parameter-pattern.rs:28:48
21+
|
22+
LL | pub fn test_let_pat<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
23+
| ^^^^
24+
25+
error: aborting due to 4 previous errors
26+

tests/ui/pattern/issue-68393-let-pat-assoc-constant.rs

-26
This file was deleted.

tests/ui/pattern/issue-68393-let-pat-assoc-constant.stderr

-15
This file was deleted.

0 commit comments

Comments
 (0)