Skip to content

Commit 4a462c0

Browse files
committed
avoid creating an Instance only to immediately disassemble it again
1 parent 37ad6b2 commit 4a462c0

File tree

11 files changed

+49
-111
lines changed

11 files changed

+49
-111
lines changed

compiler/rustc_error_codes/src/error_codes/E0158.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
An associated `const`, `const` parameter or `static` has been referenced
2-
in a pattern.
1+
A generic parameter or `static` has been referenced in a pattern.
32

43
Erroneous code example:
54

@@ -15,25 +14,25 @@ trait Bar {
1514
1615
fn test<A: Bar>(arg: Foo) {
1716
match arg {
18-
A::X => println!("A::X"), // error: E0158: associated consts cannot be
19-
// referenced in patterns
17+
A::X => println!("A::X"), // error: E0158: constant pattern depends
18+
// on a generic parameter
2019
Foo::Two => println!("Two")
2120
}
2221
}
2322
```
2423

25-
Associated `const`s cannot be referenced in patterns because it is impossible
24+
Generic parameters cannot be referenced in patterns because it is impossible
2625
for the compiler to prove exhaustiveness (that some pattern will always match).
2726
Take the above example, because Rust does type checking in the *generic*
2827
method, not the *monomorphized* specific instance. So because `Bar` could have
29-
theoretically infinite implementations, there's no way to always be sure that
28+
theoretically arbitrary implementations, there's no way to always be sure that
3029
`A::X` is `Foo::One`. So this code must be rejected. Even if code can be
3130
proven exhaustive by a programmer, the compiler cannot currently prove this.
3231

33-
The same holds true of `const` parameters and `static`s.
32+
The same holds true of `static`s.
3433

35-
If you want to match against an associated `const`, `const` parameter or
36-
`static` consider using a guard instead:
34+
If you want to match against a `const` that depends on a generic parameter or a
35+
`static`, consider using a guard instead:
3736

3837
```
3938
trait Trait {

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

+1-8
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 {
@@ -597,7 +590,7 @@ pub(crate) struct UnreachablePattern {
597590
}
598591

599592
#[derive(Diagnostic)]
600-
#[diag(mir_build_const_pattern_depends_on_generic_parameter)]
593+
#[diag(mir_build_const_pattern_depends_on_generic_parameter, code = E0158)]
601594
pub(crate) struct ConstPatternDependsOnGenericParameter {
602595
#[primary_span]
603596
pub(crate) span: Span,

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 {

src/tools/tidy/src/issues.txt

-1
Original file line numberDiff line numberDiff line change
@@ -3449,7 +3449,6 @@ ui/pattern/issue-6449.rs
34493449
ui/pattern/issue-66270-pat-struct-parser-recovery.rs
34503450
ui/pattern/issue-67037-pat-tup-scrut-ty-diff-less-fields.rs
34513451
ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs
3452-
ui/pattern/issue-68393-let-pat-assoc-constant.rs
34533452
ui/pattern/issue-72565.rs
34543453
ui/pattern/issue-72574-1.rs
34553454
ui/pattern/issue-72574-2.rs

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,27 @@
1+
error[E0158]: 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[E0158]: 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[E0158]: 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[E0158]: 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+
27+
For more information about this error, try `rustc --explain E0158`.
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
error: constant pattern depends on a generic parameter
1+
error[E0158]: constant pattern depends on a generic parameter
22
--> $DIR/const-match-pat-generic.rs:7:9
33
|
44
LL | const { V } => {},
55
| ^^^^^^^^^^^
66

7-
error: constant pattern depends on a generic parameter
7+
error[E0158]: constant pattern depends on a generic parameter
88
--> $DIR/const-match-pat-generic.rs:19:9
99
|
1010
LL | const { f(V) } => {},
1111
| ^^^^^^^^^^^^^^
1212

1313
error: aborting due to 2 previous errors
1414

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

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)