Skip to content

Commit

Permalink
avoid creating an Instance only to immediately disassemble it again
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Jul 13, 2024
1 parent 11637c9 commit 414b6f7
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 98 deletions.
2 changes: 0 additions & 2 deletions compiler/rustc_mir_build/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ mir_build_already_borrowed = cannot borrow value as mutable because it is also b
mir_build_already_mut_borrowed = cannot borrow value as immutable because it is also borrowed as mutable
mir_build_assoc_const_in_pattern = associated consts cannot be referenced in patterns
mir_build_bindings_with_variant_name =
pattern binding `{$name}` is named the same as one of the variants of the type `{$ty_path}`
.suggestion = to match on the variant, qualify the path
Expand Down
7 changes: 0 additions & 7 deletions compiler/rustc_mir_build/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,13 +566,6 @@ pub(crate) struct StaticInPattern {
pub(crate) span: Span,
}

#[derive(Diagnostic)]
#[diag(mir_build_assoc_const_in_pattern, code = E0158)]
pub(crate) struct AssocConstInPattern {
#[primary_span]
pub(crate) span: Span,
}

#[derive(Diagnostic)]
#[diag(mir_build_const_param_in_pattern, code = E0158)]
pub(crate) struct ConstParamInPattern {
Expand Down
33 changes: 2 additions & 31 deletions compiler/rustc_mir_build/src/thir/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,37 +548,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
_ => return pat_from_kind(self.lower_variant_or_leaf(res, id, span, ty, vec![])),
};

// Use `Reveal::All` here because patterns are always monomorphic even if their function
// isn't.
let param_env_reveal_all = self.param_env.with_reveal_all_normalized(self.tcx);
// N.B. There is no guarantee that args collected in typeck results are fully normalized,
// so they need to be normalized in order to pass to `Instance::resolve`, which will ICE
// if given unnormalized types.
let args = self
.tcx
.normalize_erasing_regions(param_env_reveal_all, self.typeck_results.node_args(id));
let instance = match ty::Instance::try_resolve(self.tcx, param_env_reveal_all, def_id, args)
{
Ok(Some(i)) => i,
Ok(None) => {
// It should be assoc consts if there's no error but we cannot resolve it.
debug_assert!(is_associated_const);

let e = self.tcx.dcx().emit_err(AssocConstInPattern { span });
return pat_from_kind(PatKind::Error(e));
}

Err(_) => {
let e = self.tcx.dcx().emit_err(CouldNotEvalConstPattern { span });
return pat_from_kind(PatKind::Error(e));
}
};

let c = ty::Const::new_unevaluated(
self.tcx,
ty::UnevaluatedConst { def: instance.def_id(), args: instance.args },
);

let args = self.typeck_results.node_args(id);
let c = ty::Const::new_unevaluated(self.tcx, ty::UnevaluatedConst { def: def_id, args });
let pattern = self.const_to_pat(c, ty, id, span);

if !is_associated_const {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ impl Foo for Def {
pub fn test<A: Foo, B: Foo>(arg: EFoo) {
match arg {
A::X => println!("A::X"),
//~^ error: associated consts cannot be referenced in patterns [E0158]
//~^ error: constant pattern depends on a generic parameter
B::X => println!("B::X"),
//~^ error: associated consts cannot be referenced in patterns [E0158]
//~^ error: constant pattern depends on a generic parameter
_ => (),
}
}

pub fn test_let_pat<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
//~^ ERROR constant pattern depends on a generic parameter
let A::X = arg;
//~^ ERROR constant pattern depends on a generic parameter
}

fn main() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: constant pattern depends on a generic parameter
--> $DIR/associated-const-type-parameter-pattern.rs:20:9
|
LL | A::X => println!("A::X"),
| ^^^^

error: constant pattern depends on a generic parameter
--> $DIR/associated-const-type-parameter-pattern.rs:22:9
|
LL | B::X => println!("B::X"),
| ^^^^

error: constant pattern depends on a generic parameter
--> $DIR/associated-const-type-parameter-pattern.rs:30:9
|
LL | let A::X = arg;
| ^^^^

error: constant pattern depends on a generic parameter
--> $DIR/associated-const-type-parameter-pattern.rs:28:48
|
LL | pub fn test_let_pat<A: Foo, B: Foo>(arg: EFoo, A::X: EFoo) {
| ^^^^

error: aborting due to 4 previous errors

26 changes: 0 additions & 26 deletions tests/ui/pattern/issue-68393-let-pat-assoc-constant.rs

This file was deleted.

15 changes: 0 additions & 15 deletions tests/ui/pattern/issue-68393-let-pat-assoc-constant.stderr

This file was deleted.

0 comments on commit 414b6f7

Please sign in to comment.