Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bbf07c0

Browse files
committed
Auto merge of rust-lang#83333 - Dylan-DPC:rollup-0rdt6sz, r=Dylan-DPC
Rollup of 6 pull requests Successful merges: - rust-lang#82707 (const_evaluatable_checked: Stop eagerly erroring in `is_const_evaluatable`) - rust-lang#83040 (extract `ConstKind::Unevaluated` into a struct) - rust-lang#83280 (Fix pluralization in keyword docs) - rust-lang#83289 (Move some tests to more reasonable directories - 5) - rust-lang#83306 (Extend `proc_macro_back_compat` lint to `js-sys`) - rust-lang#83327 (Extend comment in `UsedLocals::visit_lhs`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents ed4005d + 69f6a19 commit bbf07c0

File tree

94 files changed

+436
-295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+436
-295
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
4545
};
4646
match const_.val {
4747
ConstKind::Value(_) => {}
48-
ConstKind::Unevaluated(def, ref substs, promoted) => {
48+
ConstKind::Unevaluated(unevaluated) => {
4949
if let Err(err) =
50-
fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None)
50+
fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None)
5151
{
5252
all_constants_ok = false;
5353
match err {
@@ -122,14 +122,14 @@ pub(crate) fn codegen_constant<'tcx>(
122122
};
123123
let const_val = match const_.val {
124124
ConstKind::Value(const_val) => const_val,
125-
ConstKind::Unevaluated(def, ref substs, promoted) if fx.tcx.is_static(def.did) => {
125+
ConstKind::Unevaluated(ty::Unevaluated { def, substs, promoted }) if fx.tcx.is_static(def.did) => {
126126
assert!(substs.is_empty());
127127
assert!(promoted.is_none());
128128

129129
return codegen_static_ref(fx, def.did, fx.layout_of(const_.ty)).to_cvalue(fx);
130130
}
131-
ConstKind::Unevaluated(def, ref substs, promoted) => {
132-
match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None) {
131+
ConstKind::Unevaluated(unevaluated) => {
132+
match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), unevaluated, None) {
133133
Ok(const_val) => const_val,
134134
Err(_) => {
135135
span_bug!(constant.span, "erroneous constant not captured by required_consts");

compiler/rustc_codegen_ssa/src/mir/constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3030
mir::ConstantKind::Val(val, _) => return Ok(val),
3131
};
3232
match ct.val {
33-
ty::ConstKind::Unevaluated(def, substs, promoted) => self
33+
ty::ConstKind::Unevaluated(ct) => self
3434
.cx
3535
.tcx()
36-
.const_eval_resolve(ty::ParamEnv::reveal_all(), def, substs, promoted, None)
36+
.const_eval_resolve(ty::ParamEnv::reveal_all(), ct, None)
3737
.map_err(|err| {
3838
self.cx.tcx().sess.span_err(constant.span, "erroneous constant encountered");
3939
err

compiler/rustc_expand/src/proc_macro_server.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,8 @@ fn ident_name_compatibility_hack(
739739

740740
let time_macros_impl =
741741
macro_name == sym::impl_macros && matches_prefix("time-macros-impl", "lib.rs");
742-
if time_macros_impl
743-
|| (macro_name == sym::arrays && matches_prefix("js-sys", "lib.rs"))
744-
{
742+
let js_sys = macro_name == sym::arrays && matches_prefix("js-sys", "lib.rs");
743+
if time_macros_impl || js_sys {
745744
let snippet = source_map.span_to_snippet(orig_span);
746745
if snippet.as_deref() == Ok("$name") {
747746
if time_macros_impl {
@@ -754,8 +753,35 @@ fn ident_name_compatibility_hack(
754753
"the `time-macros-impl` crate will stop compiling in futures version of Rust. \
755754
Please update to the latest version of the `time` crate to avoid breakage".to_string())
756755
);
756+
return Some((*ident, *is_raw));
757+
}
758+
if js_sys {
759+
if let Some(c) = path
760+
.components()
761+
.flat_map(|c| c.as_os_str().to_str())
762+
.find(|c| c.starts_with("js-sys"))
763+
{
764+
let mut version = c.trim_start_matches("js-sys-").split(".");
765+
if version.next() == Some("0")
766+
&& version.next() == Some("3")
767+
&& version
768+
.next()
769+
.and_then(|c| c.parse::<u32>().ok())
770+
.map_or(false, |v| v < 40)
771+
{
772+
rustc.sess.buffer_lint_with_diagnostic(
773+
&PROC_MACRO_BACK_COMPAT,
774+
orig_span,
775+
ast::CRATE_NODE_ID,
776+
"using an old version of `js-sys`",
777+
BuiltinLintDiagnostics::ProcMacroBackCompat(
778+
"older versions of the `js-sys` crate will stop compiling in future versions of Rust; \
779+
please update to `js-sys` v0.3.40 or above".to_string())
780+
);
781+
return Some((*ident, *is_raw));
782+
}
783+
}
757784
}
758-
return Some((*ident, *is_raw));
759785
}
760786
}
761787

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_hir::def_id::{DefId, LocalDefId};
1818
use rustc_middle::infer::canonical::{Canonical, CanonicalVarValues};
1919
use rustc_middle::infer::unify_key::{ConstVarValue, ConstVariableValue};
2020
use rustc_middle::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
21-
use rustc_middle::mir;
2221
use rustc_middle::mir::interpret::EvalToConstValueResult;
2322
use rustc_middle::traits::select;
2423
use rustc_middle::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
@@ -1499,9 +1498,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14991498
pub fn const_eval_resolve(
15001499
&self,
15011500
param_env: ty::ParamEnv<'tcx>,
1502-
def: ty::WithOptConstParam<DefId>,
1503-
substs: SubstsRef<'tcx>,
1504-
promoted: Option<mir::Promoted>,
1501+
ty::Unevaluated { def, substs, promoted }: ty::Unevaluated<'tcx>,
15051502
span: Option<Span>,
15061503
) -> EvalToConstValueResult<'tcx> {
15071504
let mut original_values = OriginalQueryValues::default();
@@ -1510,7 +1507,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15101507
let (param_env, substs) = canonical.value;
15111508
// The return value is the evaluated value which doesn't contain any reference to inference
15121509
// variables, thus we don't need to substitute back the original values.
1513-
self.tcx.const_eval_resolve(param_env, def, substs, promoted, span)
1510+
self.tcx.const_eval_resolve(param_env, ty::Unevaluated { def, substs, promoted }, span)
15141511
}
15151512

15161513
/// If `typ` is a type variable of some kind, resolve it one level

compiler/rustc_middle/src/mir/abstract_const.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,20 @@ pub enum Node<'tcx> {
1818
UnaryOp(mir::UnOp, NodeId),
1919
FunctionCall(NodeId, &'tcx [NodeId]),
2020
}
21+
22+
#[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable, TyEncodable, TyDecodable)]
23+
pub enum NotConstEvaluatable {
24+
Error(rustc_errors::ErrorReported),
25+
MentionsInfer,
26+
MentionsParam,
27+
}
28+
29+
impl From<rustc_errors::ErrorReported> for NotConstEvaluatable {
30+
fn from(e: rustc_errors::ErrorReported) -> NotConstEvaluatable {
31+
NotConstEvaluatable::Error(e)
32+
}
33+
}
34+
35+
TrivialTypeFoldableAndLiftImpls! {
36+
NotConstEvaluatable,
37+
}

compiler/rustc_middle/src/mir/interpret/queries.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::{ErrorHandled, EvalToConstValueResult, GlobalId};
22

33
use crate::mir;
4-
use crate::ty::subst::{InternalSubsts, SubstsRef};
4+
use crate::ty::subst::InternalSubsts;
55
use crate::ty::{self, TyCtxt};
66
use rustc_hir::def_id::DefId;
77
use rustc_span::Span;
@@ -35,14 +35,12 @@ impl<'tcx> TyCtxt<'tcx> {
3535
pub fn const_eval_resolve(
3636
self,
3737
param_env: ty::ParamEnv<'tcx>,
38-
def: ty::WithOptConstParam<DefId>,
39-
substs: SubstsRef<'tcx>,
40-
promoted: Option<mir::Promoted>,
38+
ct: ty::Unevaluated<'tcx>,
4139
span: Option<Span>,
4240
) -> EvalToConstValueResult<'tcx> {
43-
match ty::Instance::resolve_opt_const_arg(self, param_env, def, substs) {
41+
match ty::Instance::resolve_opt_const_arg(self, param_env, ct.def, ct.substs) {
4442
Ok(Some(instance)) => {
45-
let cid = GlobalId { instance, promoted };
43+
let cid = GlobalId { instance, promoted: ct.promoted };
4644
self.const_eval_global_id(param_env, cid, span)
4745
}
4846
Ok(None) => Err(ErrorHandled::TooGeneric),

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub mod specialization_graph;
99
mod structural_impls;
1010

1111
use crate::infer::canonical::Canonical;
12-
use crate::mir::interpret::ErrorHandled;
12+
use crate::mir::abstract_const::NotConstEvaluatable;
1313
use crate::ty::subst::SubstsRef;
1414
use crate::ty::{self, AdtKind, Ty, TyCtxt};
1515

@@ -398,7 +398,7 @@ pub enum SelectionError<'tcx> {
398398
ty::error::TypeError<'tcx>,
399399
),
400400
TraitNotObjectSafe(DefId),
401-
ConstEvalFailure(ErrorHandled),
401+
NotConstEvaluatable(NotConstEvaluatable),
402402
Overflow,
403403
}
404404

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,18 @@ impl<'tcx> Const<'tcx> {
9898
let name = tcx.hir().name(hir_id);
9999
ty::ConstKind::Param(ty::ParamConst::new(index, name))
100100
}
101-
_ => ty::ConstKind::Unevaluated(
102-
def.to_global(),
103-
InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
104-
None,
105-
),
101+
_ => ty::ConstKind::Unevaluated(ty::Unevaluated {
102+
def: def.to_global(),
103+
substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
104+
promoted: None,
105+
}),
106106
};
107107

108108
tcx.mk_const(ty::Const { val, ty })
109109
}
110110

111-
#[inline]
112111
/// Interns the given value as a constant.
112+
#[inline]
113113
pub fn from_value(tcx: TyCtxt<'tcx>, val: ConstValue<'tcx>, ty: Ty<'tcx>) -> &'tcx Self {
114114
tcx.mk_const(Self { val: ConstKind::Value(val), ty })
115115
}

compiler/rustc_middle/src/ty/consts/kind.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ use rustc_macros::HashStable;
1212
use rustc_target::abi::Size;
1313

1414
use super::ScalarInt;
15+
/// An unevaluated, potentially generic, constant.
16+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
17+
#[derive(Hash, HashStable)]
18+
pub struct Unevaluated<'tcx> {
19+
pub def: ty::WithOptConstParam<DefId>,
20+
pub substs: SubstsRef<'tcx>,
21+
pub promoted: Option<Promoted>,
22+
}
1523

1624
/// Represents a constant in Rust.
17-
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Hash)]
18-
#[derive(HashStable)]
25+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable)]
26+
#[derive(Hash, HashStable)]
1927
pub enum ConstKind<'tcx> {
2028
/// A const generic parameter.
2129
Param(ty::ParamConst),
@@ -31,7 +39,7 @@ pub enum ConstKind<'tcx> {
3139

3240
/// Used in the HIR by using `Unevaluated` everywhere and later normalizing to one of the other
3341
/// variants when the code is monomorphic enough for that.
34-
Unevaluated(ty::WithOptConstParam<DefId>, SubstsRef<'tcx>, Option<Promoted>),
42+
Unevaluated(Unevaluated<'tcx>),
3543

3644
/// Used to hold computed value.
3745
Value(ConstValue<'tcx>),
@@ -102,7 +110,7 @@ impl<'tcx> ConstKind<'tcx> {
102110
tcx: TyCtxt<'tcx>,
103111
param_env: ParamEnv<'tcx>,
104112
) -> Option<Result<ConstValue<'tcx>, ErrorReported>> {
105-
if let ConstKind::Unevaluated(def, substs, promoted) = self {
113+
if let ConstKind::Unevaluated(Unevaluated { def, substs, promoted }) = self {
106114
use crate::mir::interpret::ErrorHandled;
107115

108116
// HACK(eddyb) this erases lifetimes even though `const_eval_resolve`
@@ -132,7 +140,8 @@ impl<'tcx> ConstKind<'tcx> {
132140
let (param_env, substs) = param_env_and_substs.into_parts();
133141
// try to resolve e.g. associated constants to their definition on an impl, and then
134142
// evaluate the const.
135-
match tcx.const_eval_resolve(param_env, def, substs, promoted, None) {
143+
match tcx.const_eval_resolve(param_env, ty::Unevaluated { def, substs, promoted }, None)
144+
{
136145
// NOTE(eddyb) `val` contains no lifetimes/types/consts,
137146
// and we use the original type, so nothing from `substs`
138147
// (which may be identity substs, see above),

compiler/rustc_middle/src/ty/flags.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,7 @@ impl FlagComputation {
270270
fn add_const(&mut self, c: &ty::Const<'_>) {
271271
self.add_ty(c.ty);
272272
match c.val {
273-
ty::ConstKind::Unevaluated(_, substs, _) => {
274-
self.add_substs(substs);
275-
self.add_flags(TypeFlags::HAS_CT_PROJECTION);
276-
}
273+
ty::ConstKind::Unevaluated(unevaluated) => self.add_unevaluated_const(unevaluated),
277274
ty::ConstKind::Infer(infer) => {
278275
self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE);
279276
match infer {
@@ -297,6 +294,11 @@ impl FlagComputation {
297294
}
298295
}
299296

297+
fn add_unevaluated_const(&mut self, ct: ty::Unevaluated<'tcx>) {
298+
self.add_substs(ct.substs);
299+
self.add_flags(TypeFlags::HAS_CT_PROJECTION);
300+
}
301+
300302
fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) {
301303
self.add_substs(projection.substs);
302304
self.add_ty(projection.ty);

0 commit comments

Comments
 (0)