Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid some unnecessary query invocations. #121387

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions compiler/rustc_hir_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_middle::middle;
use rustc_middle::mir::interpret::GlobalId;
use rustc_middle::query::Providers;
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::util;
use rustc_session::parse::feature_err;
use rustc_span::{symbol::sym, Span};
Expand Down Expand Up @@ -187,7 +188,12 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
let def_kind = tcx.def_kind(item_def_id);
match def_kind {
DefKind::Static { .. } => tcx.ensure().eval_static_initializer(item_def_id),
DefKind::Const => tcx.ensure().const_eval_poly(item_def_id.into()),
DefKind::Const if tcx.generics_of(item_def_id).params.is_empty() => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this impacts behavior for consts with where-bounds, do we get a similar perf improvement if we continue using const_eval_poly but change it to use self.tcx.param_env_with_reveal_all(def_id)?

Copy link
Contributor Author

@oli-obk oli-obk Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only an issue with trivial_bounds, which are checked on MIR in mir_drops_elaborated_and_const_checked. If they don't hold, we destroy the MIR body which will fail const eval even if the constant is never called. Not ideal I guess. I'll ponder on how to get perf and no issues at the same time 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I guess we already fail it today:

#![feature(trivial_bounds)]
#![feature(generic_const_items)]
const FOO: () = () where str: Sized;

with exactly the error I assumed

error[E0080]: evaluation of constant value failed
 --> src/lib.rs:3:1
  |
3 | const FOO: () = () where str: Sized;
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entering unreachable code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this work with

trait Trait {
    const ASSOC: u32;
}

impl Trait for ()
where
    for<'a> &'a (): Trait,
{
    const ASSOC: u32 = <&'static ()>::ASSOC;
}

Copy link
Contributor Author

@oli-obk oli-obk Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not a DefKind::Const, but a DefKind::AssocConst.

I can't come up with any problematic example using generic const items, but I think your example can be rewritten with them

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does

#![feature(generic_const_items)]
trait Trait {
    const ASSOC: u32;
}

const ASSOC: u32 = <&'static ()>::ASSOC where for<'a> &'a (): Trait;

go from pass to error with this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, it still passes

let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty());
let cid = GlobalId { instance, promoted: None };
let param_env = ty::ParamEnv::reveal_all();
tcx.ensure().eval_to_const_value_raw(param_env.and(cid));
}
_ => (),
}
});
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/generic-const-items/hkl_where_bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Test that nonsense bounds prevent consts from being evaluated at all.
//@ check-pass

#![feature(generic_const_items)]
#![allow(incomplete_features)]
trait Trait {
const ASSOC: u32;
}

// rustfmt eats the where bound
#[rustfmt::skip]
const ASSOC: u32 = <&'static ()>::ASSOC where for<'a> &'a (): Trait;

fn main() {}
Loading