Skip to content

Commit 9b98af8

Browse files
committed
Auto merge of #67532 - Centril:rollup-3duj42d, r=Centril
Rollup of 8 pull requests Successful merges: - #66877 (Add simpler entry points to const eval for common usages.) - #67299 (Add `ImmTy::try_from_(u)int` methods) - #67487 (Rustdoc mutability removal) - #67499 (Misc MIR building cleanups) - #67506 (Remove iter_private.rs) - #67508 (Fix typo in path parser name) - #67519 (Document why Any is not an unsafe trait) - #67525 (Utilize rust-lang/rust commit hashes in toolstate) Failed merges: r? @ghost
2 parents 0d2817a + 9c5b73e commit 9b98af8

File tree

41 files changed

+320
-428
lines changed

Some content is hidden

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

41 files changed

+320
-428
lines changed

src/ci/publish_toolstate.sh

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ printf 'https://%s:[email protected]\n' "$TOOLSTATE_REPO_ACCESS_TOKEN" \
1414
> "$HOME/.git-credentials"
1515
git clone --depth=1 $TOOLSTATE_REPO
1616

17+
GIT_COMMIT="$(git rev-parse HEAD)"
18+
GIT_COMMIT_MSG="$(git log --format=%s -n1 HEAD)"
19+
1720
cd rust-toolstate
1821
FAILURE=1
1922
for RETRY_COUNT in 1 2 3 4 5; do
2023
# The purpose is to publish the new "current" toolstate in the toolstate repo.
21-
"$BUILD_SOURCESDIRECTORY/src/tools/publish_toolstate.py" "$(git rev-parse HEAD)" \
22-
"$(git log --format=%s -n1 HEAD)" \
24+
"$BUILD_SOURCESDIRECTORY/src/tools/publish_toolstate.py" "$GIT_COMMIT" \
25+
"$GIT_COMMIT_MSG" \
2326
"$MESSAGE_FILE" \
2427
"$TOOLSTATE_REPO_ACCESS_TOKEN"
2528
# `git commit` failing means nothing to commit.

src/libcore/any.rs

+10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ use crate::intrinsics;
7474
/// See the [module-level documentation][mod] for more details.
7575
///
7676
/// [mod]: index.html
77+
// This trait is not unsafe, though we rely on the specifics of it's sole impl's
78+
// `type_id` function in unsafe code (e.g., `downcast`). Normally, that would be
79+
// a problem, but because the only impl of `Any` is a blanket implementation, no
80+
// other code can implement `Any`.
81+
//
82+
// We could plausibly make this trait unsafe -- it would not cause breakage,
83+
// since we control all the implementations -- but we choose not to as that's
84+
// both not really necessary and may confuse users about the distinction of
85+
// unsafe traits and unsafe methods (i.e., `type_id` would still be safe to call,
86+
// but we would likely want to indicate as such in documentation).
7787
#[stable(feature = "rust1", since = "1.0.0")]
7888
pub trait Any: 'static {
7989
/// Gets the `TypeId` of `self`.

src/libcore/iter_private.rs

-17
This file was deleted.

src/librustc/mir/interpret/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ mod error;
101101
mod value;
102102
mod allocation;
103103
mod pointer;
104+
mod queries;
104105

105106
pub use self::error::{
106107
InterpErrorInfo, InterpResult, InterpError, AssertMessage, ConstEvalErr, struct_error,
@@ -116,9 +117,10 @@ pub use self::pointer::{Pointer, PointerArithmetic, CheckInAllocMsg};
116117

117118
use crate::mir;
118119
use crate::hir::def_id::DefId;
119-
use crate::ty::{self, TyCtxt, Instance, subst::GenericArgKind};
120+
use crate::ty::{self, TyCtxt, Instance};
120121
use crate::ty::codec::TyDecoder;
121122
use crate::ty::layout::{self, Size};
123+
use crate::ty::subst::GenericArgKind;
122124
use std::io;
123125
use std::fmt;
124126
use std::num::NonZeroU32;

src/librustc/mir/interpret/queries.rs

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
use super::{ConstEvalResult, ErrorHandled, GlobalId};
2+
3+
use crate::mir;
4+
use crate::hir::def_id::DefId;
5+
use crate::ty::{self, TyCtxt};
6+
use crate::ty::subst::{InternalSubsts, SubstsRef};
7+
use syntax_pos::Span;
8+
9+
10+
impl<'tcx> TyCtxt<'tcx> {
11+
12+
/// Evaluates a constant without providing any substitutions. This is useful to evaluate consts
13+
/// that can't take any generic arguments like statics, const items or enum discriminants. If a
14+
/// generic parameter is used within the constant `ErrorHandled::ToGeneric` will be returned.
15+
pub fn const_eval_poly(self, def_id: DefId) -> ConstEvalResult<'tcx> {
16+
// In some situations def_id will have substitutions within scope, but they aren't allowed
17+
// to be used. So we can't use `Instance::mono`, instead we feed unresolved substitutions
18+
// into `const_eval` which will return `ErrorHandled::ToGeneric` if any og them are
19+
// encountered.
20+
let substs = InternalSubsts::identity_for_item(self, def_id);
21+
let instance = ty::Instance::new(def_id, substs);
22+
let cid = GlobalId {
23+
instance,
24+
promoted: None,
25+
};
26+
let param_env = self.param_env(def_id);
27+
self.const_eval_validated(param_env.and(cid))
28+
}
29+
30+
/// Resolves and evaluates a constant.
31+
///
32+
/// The constant can be located on a trait like `<A as B>::C`, in which case the given
33+
/// substitutions and environment are used to resolve the constant. Alternatively if the
34+
/// constant has generic parameters in scope the substitutions are used to evaluate the value of
35+
/// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
36+
/// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still
37+
/// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
38+
/// returned.
39+
pub fn const_eval_resolve(
40+
self,
41+
param_env: ty::ParamEnv<'tcx>,
42+
def_id: DefId,
43+
substs: SubstsRef<'tcx>,
44+
span: Option<Span>
45+
) -> ConstEvalResult<'tcx> {
46+
let instance = ty::Instance::resolve(
47+
self,
48+
param_env,
49+
def_id,
50+
substs,
51+
);
52+
if let Some(instance) = instance {
53+
self.const_eval_instance(param_env, instance, span)
54+
} else {
55+
Err(ErrorHandled::TooGeneric)
56+
}
57+
}
58+
59+
pub fn const_eval_instance(
60+
self,
61+
param_env: ty::ParamEnv<'tcx>,
62+
instance: ty::Instance<'tcx>,
63+
span: Option<Span>
64+
) -> ConstEvalResult<'tcx> {
65+
let cid = GlobalId {
66+
instance,
67+
promoted: None,
68+
};
69+
if let Some(span) = span {
70+
self.at(span).const_eval_validated(param_env.and(cid))
71+
} else {
72+
self.const_eval_validated(param_env.and(cid))
73+
}
74+
}
75+
76+
/// Evaluate a promoted constant.
77+
pub fn const_eval_promoted(
78+
self,
79+
instance: ty::Instance<'tcx>,
80+
promoted: mir::Promoted
81+
) -> ConstEvalResult<'tcx> {
82+
let cid = GlobalId {
83+
instance,
84+
promoted: Some(promoted),
85+
};
86+
let param_env = ty::ParamEnv::reveal_all();
87+
self.const_eval_validated(param_env.and(cid))
88+
}
89+
}

src/librustc/mir/interpret/value.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,22 @@ impl<'tcx, Tag> Scalar<Tag> {
236236
Scalar::Raw { data: c as u128, size: 4 }
237237
}
238238

239+
#[inline]
240+
pub fn try_from_uint(i: impl Into<u128>, size: Size) -> Option<Self> {
241+
let i = i.into();
242+
if truncate(i, size) == i {
243+
Some(Scalar::Raw { data: i, size: size.bytes() as u8 })
244+
} else {
245+
None
246+
}
247+
}
248+
239249
#[inline]
240250
pub fn from_uint(i: impl Into<u128>, size: Size) -> Self {
241251
let i = i.into();
242-
assert_eq!(
243-
truncate(i, size), i,
244-
"Unsigned value {:#x} does not fit in {} bits", i, size.bits()
245-
);
246-
Scalar::Raw { data: i, size: size.bytes() as u8 }
252+
Self::try_from_uint(i, size).unwrap_or_else(|| {
253+
bug!("Unsigned value {:#x} does not fit in {} bits", i, size.bits())
254+
})
247255
}
248256

249257
#[inline]
@@ -267,15 +275,23 @@ impl<'tcx, Tag> Scalar<Tag> {
267275
}
268276

269277
#[inline]
270-
pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
278+
pub fn try_from_int(i: impl Into<i128>, size: Size) -> Option<Self> {
271279
let i = i.into();
272280
// `into` performed sign extension, we have to truncate
273281
let truncated = truncate(i as u128, size);
274-
assert_eq!(
275-
sign_extend(truncated, size) as i128, i,
276-
"Signed value {:#x} does not fit in {} bits", i, size.bits()
277-
);
278-
Scalar::Raw { data: truncated, size: size.bytes() as u8 }
282+
if sign_extend(truncated, size) as i128 == i {
283+
Some(Scalar::Raw { data: truncated, size: size.bytes() as u8 })
284+
} else {
285+
None
286+
}
287+
}
288+
289+
#[inline]
290+
pub fn from_int(i: impl Into<i128>, size: Size) -> Self {
291+
let i = i.into();
292+
Self::try_from_int(i, size).unwrap_or_else(|| {
293+
bug!("Signed value {:#x} does not fit in {} bits", i, size.bits())
294+
})
279295
}
280296

281297
#[inline]

src/librustc/query/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ rustc_queries! {
448448
///
449449
/// **Do not use this** outside const eval. Const eval uses this to break query cycles
450450
/// during validation. Please add a comment to every use site explaining why using
451-
/// `const_eval` isn't sufficient.
451+
/// `const_eval_validated` isn't sufficient. The returned constant also isn't in a suitable
452+
/// form to be used outside of const eval.
452453
query const_eval_raw(key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>)
453454
-> ConstEvalRawResult<'tcx> {
454455
no_force
@@ -460,7 +461,13 @@ rustc_queries! {
460461

461462
/// Results of evaluating const items or constants embedded in
462463
/// other items (such as enum variant explicit discriminants).
463-
query const_eval(key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>)
464+
///
465+
/// In contrast to `const_eval_raw` this performs some validation on the constant, and
466+
/// returns a proper constant that is usable by the rest of the compiler.
467+
///
468+
/// **Do not use this** directly, use one of the following wrappers: `tcx.const_eval_poly`,
469+
/// `tcx.const_eval_resolve`, `tcx.const_eval_instance`, or `tcx.const_eval_promoted`.
470+
query const_eval_validated(key: ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>)
464471
-> ConstEvalResult<'tcx> {
465472
no_force
466473
desc { |tcx|

src/librustc/traits/fulfill.rs

+7-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::infer::{InferCtxt, ShallowResolver};
2-
use crate::mir::interpret::{GlobalId, ErrorHandled};
32
use crate::ty::{self, Ty, TypeFoldable, ToPolyTraitRef};
43
use crate::ty::error::ExpectedFound;
54
use rustc_data_structures::obligation_forest::{DoCompleted, Error, ForestObligation};
@@ -501,27 +500,13 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
501500
ProcessResult::Unchanged
502501
} else {
503502
if !substs.has_local_value() {
504-
let instance = ty::Instance::resolve(
505-
self.selcx.tcx(),
506-
obligation.param_env,
507-
def_id,
508-
substs,
509-
);
510-
if let Some(instance) = instance {
511-
let cid = GlobalId {
512-
instance,
513-
promoted: None,
514-
};
515-
match self.selcx.tcx().at(obligation.cause.span)
516-
.const_eval(obligation.param_env.and(cid)) {
517-
Ok(_) => ProcessResult::Changed(vec![]),
518-
Err(err) => ProcessResult::Error(
519-
CodeSelectionError(ConstEvalFailure(err)))
520-
}
521-
} else {
522-
ProcessResult::Error(CodeSelectionError(
523-
ConstEvalFailure(ErrorHandled::TooGeneric)
524-
))
503+
match self.selcx.tcx().const_eval_resolve(obligation.param_env,
504+
def_id,
505+
substs,
506+
Some(obligation.cause.span)) {
507+
Ok(_) => ProcessResult::Changed(vec![]),
508+
Err(err) => ProcessResult::Error(
509+
CodeSelectionError(ConstEvalFailure(err)))
525510
}
526511
} else {
527512
pending_obligation.stalled_on =

src/librustc/traits/select.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use crate::dep_graph::{DepKind, DepNodeIndex};
3333
use crate::hir::def_id::DefId;
3434
use crate::infer::{CombinedSnapshot, InferCtxt, InferOk, PlaceholderMap, TypeFreshener};
3535
use crate::middle::lang_items;
36-
use crate::mir::interpret::GlobalId;
3736
use crate::ty::fast_reject;
3837
use crate::ty::relate::TypeRelation;
3938
use crate::ty::subst::{Subst, SubstsRef};
@@ -820,22 +819,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
820819
}
821820

822821
ty::Predicate::ConstEvaluatable(def_id, substs) => {
823-
let tcx = self.tcx();
824822
if !(obligation.param_env, substs).has_local_value() {
825-
let param_env = obligation.param_env;
826-
let instance =
827-
ty::Instance::resolve(tcx, param_env, def_id, substs);
828-
if let Some(instance) = instance {
829-
let cid = GlobalId {
830-
instance,
831-
promoted: None,
832-
};
833-
match self.tcx().const_eval(param_env.and(cid)) {
834-
Ok(_) => Ok(EvaluatedToOk),
835-
Err(_) => Ok(EvaluatedToErr),
836-
}
837-
} else {
838-
Ok(EvaluatedToErr)
823+
match self.tcx().const_eval_resolve(obligation.param_env,
824+
def_id,
825+
substs,
826+
None) {
827+
Ok(_) => Ok(EvaluatedToOk),
828+
Err(_) => Ok(EvaluatedToErr),
839829
}
840830
} else {
841831
// Inference variables still left in param_env or substs.

src/librustc/ty/mod.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::middle::cstore::CrateStoreDyn;
1919
use crate::middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
2020
use crate::middle::resolve_lifetime::ObjectLifetimeDefault;
2121
use crate::mir::ReadOnlyBodyAndCache;
22-
use crate::mir::interpret::{GlobalId, ErrorHandled};
22+
use crate::mir::interpret::ErrorHandled;
2323
use crate::mir::GeneratorLayout;
2424
use crate::session::CrateDisambiguator;
2525
use crate::traits::{self, Reveal};
@@ -2344,13 +2344,7 @@ impl<'tcx> AdtDef {
23442344
pub fn eval_explicit_discr(&self, tcx: TyCtxt<'tcx>, expr_did: DefId) -> Option<Discr<'tcx>> {
23452345
let param_env = tcx.param_env(expr_did);
23462346
let repr_type = self.repr.discr_type();
2347-
let substs = InternalSubsts::identity_for_item(tcx, expr_did);
2348-
let instance = ty::Instance::new(expr_did, substs);
2349-
let cid = GlobalId {
2350-
instance,
2351-
promoted: None
2352-
};
2353-
match tcx.const_eval(param_env.and(cid)) {
2347+
match tcx.const_eval_poly(expr_did) {
23542348
Ok(val) => {
23552349
// FIXME: Find the right type and use it instead of `val.ty` here
23562350
if let Some(b) = val.try_eval_bits(tcx, param_env, val.ty) {

src/librustc/ty/sty.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::ty::{self, AdtDef, Discr, DefIdTree, TypeFlags, Ty, TyCtxt, TypeFolda
1515
use crate::ty::{List, TyS, ParamEnvAnd, ParamEnv};
1616
use crate::ty::layout::VariantIdx;
1717
use crate::util::captures::Captures;
18-
use crate::mir::interpret::{Scalar, GlobalId};
18+
use crate::mir::interpret::Scalar;
1919

2020
use polonius_engine::Atom;
2121
use rustc_index::vec::Idx;
@@ -2340,13 +2340,9 @@ impl<'tcx> Const<'tcx> {
23402340

23412341
let (param_env, substs) = param_env_and_substs.into_parts();
23422342

2343-
// try to resolve e.g. associated constants to their definition on an impl
2344-
let instance = ty::Instance::resolve(tcx, param_env, did, substs)?;
2345-
let gid = GlobalId {
2346-
instance,
2347-
promoted: None,
2348-
};
2349-
tcx.const_eval(param_env.and(gid)).ok()
2343+
// try to resolve e.g. associated constants to their definition on an impl, and then
2344+
// evaluate the const.
2345+
tcx.const_eval_resolve(param_env, did, substs, None).ok()
23502346
};
23512347

23522348
match self.val {

src/librustc_codegen_llvm/consts.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::value::Value;
88
use libc::c_uint;
99
use rustc::hir::def_id::DefId;
1010
use rustc::mir::interpret::{ConstValue, Allocation, read_target_uint,
11-
Pointer, ErrorHandled, GlobalId};
11+
Pointer, ErrorHandled};
1212
use rustc::mir::mono::MonoItem;
1313
use rustc::hir::Node;
1414
use rustc_target::abi::HasDataLayout;
@@ -81,13 +81,7 @@ pub fn codegen_static_initializer(
8181
cx: &CodegenCx<'ll, 'tcx>,
8282
def_id: DefId,
8383
) -> Result<(&'ll Value, &'tcx Allocation), ErrorHandled> {
84-
let instance = ty::Instance::mono(cx.tcx, def_id);
85-
let cid = GlobalId {
86-
instance,
87-
promoted: None,
88-
};
89-
let param_env = ty::ParamEnv::reveal_all();
90-
let static_ = cx.tcx.const_eval(param_env.and(cid))?;
84+
let static_ = cx.tcx.const_eval_poly(def_id)?;
9185

9286
let alloc = match static_.val {
9387
ty::ConstKind::Value(ConstValue::ByRef {

0 commit comments

Comments
 (0)