Skip to content

Commit 4155ed3

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

File tree

5 files changed

+6
-45
lines changed

5 files changed

+6
-45
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/pattern/issue-68393-let-pat-assoc-constant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ impl Foo for Def {
1818
}
1919

2020
pub fn test<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
21-
//~^ ERROR associated consts cannot be referenced in patterns
21+
//~^ ERROR constant pattern depends on a generic parameter
2222
let A::X = arg;
23-
//~^ ERROR associated consts cannot be referenced in patterns
23+
//~^ ERROR constant pattern depends on a generic parameter
2424
}
2525

2626
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
error[E0158]: associated consts cannot be referenced in patterns
1+
error: constant pattern depends on a generic parameter
22
--> $DIR/issue-68393-let-pat-assoc-constant.rs:22:9
33
|
44
LL | let A::X = arg;
55
| ^^^^
66

7-
error[E0158]: associated consts cannot be referenced in patterns
7+
error: constant pattern depends on a generic parameter
88
--> $DIR/issue-68393-let-pat-assoc-constant.rs:20:40
99
|
1010
LL | pub fn test<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
1111
| ^^^^
1212

1313
error: aborting due to 2 previous errors
1414

15-
For more information about this error, try `rustc --explain E0158`.

0 commit comments

Comments
 (0)