Skip to content

Commit b894a6b

Browse files
committed
Add docstrings, add def_id method to ConstEvalInput, and elide some lifetime parameters.
1 parent 0f0ed7f commit b894a6b

File tree

19 files changed

+71
-34
lines changed

19 files changed

+71
-34
lines changed

src/librustc/infer/canonical/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ impl<'tcx, R> Canonical<'tcx, QueryResponse<'tcx, R>> {
267267
}
268268

269269
impl<'tcx, V: TypeFoldable<'tcx>> Canonical<'tcx, V> {
270+
/// Construct a canonical `value` with an empty set of variables
271+
/// in the `ROOT` universe.
270272
pub fn empty(value: V) -> Canonical<'tcx, V> {
271273
assert!(!value.has_local_value() && !value.has_placeholders());
272274
Canonical {

src/librustc/mir/interpret/mod.rs

+16
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ pub use self::pointer::{CheckInAllocMsg, Pointer, PointerArithmetic};
117117

118118
use crate::infer::canonical::Canonical;
119119
use crate::mir;
120+
use crate::traits::Reveal;
120121
use crate::ty::codec::TyDecoder;
121122
use crate::ty::layout::{self, Size};
122123
use crate::ty::subst::GenericArgKind;
@@ -148,8 +149,23 @@ pub struct GlobalId<'tcx> {
148149
pub promoted: Option<mir::Promoted>,
149150
}
150151

152+
/// The input type for const eval queries. Const eval queries a given both the `ParamEnv` in which
153+
/// the constant is evaluated in and the identifier of the constant.
151154
pub type ConstEvalInput<'tcx> = Canonical<'tcx, ty::ParamEnvAnd<'tcx, GlobalId<'tcx>>>;
152155

156+
impl ConstEvalInput<'_> {
157+
/// The `DefId` of the constant that is being evaluated.
158+
pub fn def_id(&self) -> DefId {
159+
self.value.value.instance.def_id()
160+
}
161+
162+
pub fn with_reveal_user_facing(&self) -> Self {
163+
let mut new_input = self.clone();
164+
new_input.value.param_env.reveal = Reveal::UserFacing;
165+
new_input
166+
}
167+
}
168+
153169
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)]
154170
pub struct AllocId(pub u64);
155171

src/librustc/mir/interpret/queries.rs

+17
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ impl<'tcx> TyCtxt<'tcx> {
4444
.enter(|ref infcx| infcx.const_eval_resolve(param_env, def_id, substs, span))
4545
}
4646

47+
/// Evaluates the constant represented by the instance.
4748
pub fn const_eval_instance(
4849
self,
4950
param_env: ty::ParamEnv<'tcx>,
@@ -72,6 +73,10 @@ impl<'tcx> TyCtxt<'tcx> {
7273
}
7374

7475
impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
76+
/// Evaluates the constant represented by the instance.
77+
///
78+
/// The given `ParamEnv` and `Instance` can contain inference variables from this inference
79+
/// context.
7580
pub fn const_eval_instance(
7681
&self,
7782
param_env: ty::ParamEnv<'tcx>,
@@ -88,6 +93,18 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
8893
}
8994
}
9095

96+
/// Resolves and evaluates a constant.
97+
///
98+
/// The constant can be located on a trait like `<A as B>::C`, in which case the given
99+
/// substitutions and environment are used to resolve the constant. Alternatively if the
100+
/// constant has generic parameters in scope the substitutions are used to evaluate the value of
101+
/// the constant. For example in `fn foo<T>() { let _ = [0; bar::<T>()]; }` the repeat count
102+
/// constant `bar::<T>()` requires a substitution for `T`, if the substitution for `T` is still
103+
/// too generic for the constant to be evaluated then `Err(ErrorHandled::TooGeneric)` is
104+
/// returned.
105+
///
106+
/// The given `ParamEnv` and `substs` can contain inference variables from this inference
107+
/// context.
91108
pub fn const_eval_resolve(
92109
&self,
93110
param_env: ty::ParamEnv<'tcx>,

src/librustc/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ rustc_queries! {
472472
no_force
473473
desc { |tcx|
474474
"const-evaluating `{}`",
475-
tcx.def_path_str(key.value.value.instance.def.def_id())
475+
tcx.def_path_str(key.def_id())
476476
}
477477
}
478478

@@ -489,7 +489,7 @@ rustc_queries! {
489489
no_force
490490
desc { |tcx|
491491
"const-evaluating + checking `{}`",
492-
tcx.def_path_str(key.value.value.instance.def.def_id())
492+
tcx.def_path_str(key.def_id())
493493
}
494494
cache_on_disk_if(_, opt_result) {
495495
// Only store results without errors

src/librustc/traits/query/resolve_vtable.rs

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ use crate::traits::{ObligationCause, Vtable};
44
use crate::ty;
55

66
impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
7+
/// Attempts to resolves the `Vtable` for a given trait within a `ParamEnv`.
8+
///
9+
/// If it can't due to the result being ambiguous, or an error occurred during selection, `None`
10+
/// is returned.
711
pub fn resolve_vtable(
812
&self,
913
param_env: ty::ParamEnv<'tcx>,

src/librustc/ty/instance.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,6 @@ impl<'tcx> Instance<'tcx> {
203203
/// trying to resolve `Debug::fmt` applied to `T` will yield `None`, because we do not
204204
/// know what code ought to run. (Note that this setting is also affected by the
205205
/// `RevealMode` in the parameter environment.)
206-
///
207-
/// Presuming that coherence and type-check have succeeded, if this method is invoked
208-
/// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return
209-
/// `Some`.
210206
pub fn resolve<'infcx>(
211207
infcx: &'infcx InferCtxt<'infcx, 'tcx>,
212208
param_env: ty::ParamEnv<'tcx>,
@@ -255,6 +251,12 @@ impl<'tcx> Instance<'tcx> {
255251
result
256252
}
257253

254+
/// Resolves a `(def_id, substs)` pair to an instance within a monomorphic context
255+
/// (i.e., like during codegen). This is most commonly used to find the precise code that
256+
/// will run for a trait method invocation.
257+
///
258+
/// This assumes that resolving the instance will be successful, so should only be used after
259+
/// coherence and type-check have succeeded.
258260
pub fn resolve_mono(
259261
tcx: TyCtxt<'tcx>,
260262
def_id: DefId,

src/librustc_mir/const_eval/eval_queries.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ fn validate_and_turn_into_const<'tcx>(
167167
constant: RawConst<'tcx>,
168168
key: ConstEvalInput<'tcx>,
169169
) -> ::rustc::mir::interpret::ConstEvalResult<'tcx> {
170-
let span = tcx.def_span(key.value.value.instance.def_id());
170+
let def_id = key.def_id();
171+
let span = tcx.def_span(def_id);
171172
tcx.infer_ctxt().enter_with_canonical(
172173
span,
173174
&key,
174175
|ref infcx, ty::ParamEnvAnd { param_env, value: cid }, _| {
175-
let def_id = cid.instance.def.def_id();
176176
let is_static = tcx.is_static(def_id);
177177
let ecx = InterpCx::new(
178178
tcx.at(span),
@@ -227,9 +227,7 @@ pub fn const_eval_validated_provider<'tcx>(
227227

228228
// see comment in const_eval_raw_provider for what we're doing here
229229
if param_env.reveal == Reveal::All {
230-
let mut key = key.clone();
231-
key.value.param_env.reveal = Reveal::UserFacing;
232-
match tcx.const_eval_validated(key) {
230+
match tcx.const_eval_validated(key.with_reveal_user_facing()) {
233231
// try again with reveal all as requested
234232
Err(ErrorHandled::TooGeneric) => {}
235233
// dedupliate calls
@@ -270,9 +268,7 @@ pub fn const_eval_raw_provider<'tcx>(
270268

271269
// In case we fail in the `UserFacing` variant, we just do the real computation.
272270
if param_env.reveal == Reveal::All {
273-
let mut key = key.clone();
274-
key.value.param_env.reveal = Reveal::UserFacing;
275-
match tcx.const_eval_raw(key) {
271+
match tcx.const_eval_raw(key.with_reveal_user_facing()) {
276272
// try again with reveal all as requested
277273
Err(ErrorHandled::TooGeneric) => {}
278274
// dedupliate calls
@@ -289,15 +285,15 @@ pub fn const_eval_raw_provider<'tcx>(
289285
trace!("const eval: {:?} ({})", key, instance);
290286
}
291287

292-
let def_id = cid.instance.def.def_id();
288+
let def_id = key.def_id();
293289

294290
if def_id.is_local() && tcx.typeck_tables_of(def_id).tainted_by_errors {
295291
return Err(ErrorHandled::Reported);
296292
}
297293

298294
let is_static = tcx.is_static(def_id);
299295

300-
let span = tcx.def_span(cid.instance.def_id());
296+
let span = tcx.def_span(def_id);
301297
tcx.infer_ctxt().enter_with_canonical(
302298
span,
303299
&key,

src/librustc_mir/const_eval/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::interpret::{
1717

1818
use super::error::*;
1919

20-
impl<'infcx, 'mir, 'tcx> InterpCx<'infcx, 'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
20+
impl<'mir, 'tcx> InterpCx<'_, 'mir, 'tcx, CompileTimeInterpreter<'mir, 'tcx>> {
2121
/// Evaluate a const function where all arguments (if any) are zero-sized types.
2222
/// The evaluation is memoized thanks to the query system.
2323
///

src/librustc_mir/interpret/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_apfloat::{Float, FloatConvert};
1111

1212
use super::{FnVal, ImmTy, Immediate, InterpCx, Machine, OpTy, PlaceTy};
1313

14-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M> {
14+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'_, 'mir, 'tcx, M> {
1515
pub fn cast(
1616
&mut self,
1717
src: OpTy<'tcx, M::PointerTag>,

src/librustc_mir/interpret/eval_context.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,14 @@ impl<'mir, 'tcx, Tag, Extra> Frame<'mir, 'tcx, Tag, Extra> {
179179
}
180180
}
181181

182-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for InterpCx<'infcx, 'mir, 'tcx, M> {
182+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout for InterpCx<'_, 'mir, 'tcx, M> {
183183
#[inline]
184184
fn data_layout(&self) -> &layout::TargetDataLayout {
185185
&self.tcx.data_layout
186186
}
187187
}
188188

189-
impl<'infcx, 'mir, 'tcx, M> layout::HasTyCtxt<'tcx> for InterpCx<'infcx, 'mir, 'tcx, M>
189+
impl<'mir, 'tcx, M> layout::HasTyCtxt<'tcx> for InterpCx<'_, 'mir, 'tcx, M>
190190
where
191191
M: Machine<'mir, 'tcx>,
192192
{
@@ -196,7 +196,7 @@ where
196196
}
197197
}
198198

199-
impl<'infcx, 'mir, 'tcx, M> layout::HasParamEnv<'tcx> for InterpCx<'infcx, 'mir, 'tcx, M>
199+
impl<'mir, 'tcx, M> layout::HasParamEnv<'tcx> for InterpCx<'_, 'mir, 'tcx, M>
200200
where
201201
M: Machine<'mir, 'tcx>,
202202
{
@@ -205,7 +205,7 @@ where
205205
}
206206
}
207207

208-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> LayoutOf for InterpCx<'infcx, 'mir, 'tcx, M> {
208+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> LayoutOf for InterpCx<'_, 'mir, 'tcx, M> {
209209
type Ty = Ty<'tcx>;
210210
type TyLayout = InterpResult<'tcx, TyLayout<'tcx>>;
211211

src/librustc_mir/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ crate fn eval_nullary_intrinsic<'tcx>(
8181
})
8282
}
8383

84-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M> {
84+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'_, 'mir, 'tcx, M> {
8585
/// Returns `true` if emulation happened.
8686
pub fn emulate_intrinsic(
8787
&mut self,

src/librustc_mir/interpret/operand.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub(super) fn from_known_layout<'tcx>(
259259
}
260260
}
261261

262-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M> {
262+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'_, 'mir, 'tcx, M> {
263263
/// Normalice `place.ptr` to a `Pointer` if this is a place and not a ZST.
264264
/// Can be helpful to avoid lots of `force_ptr` calls later, if this place is used a lot.
265265
#[inline]

src/librustc_mir/interpret/operator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syntax::ast::FloatTy;
1010

1111
use super::{ImmTy, Immediate, InterpCx, Machine, PlaceTy};
1212

13-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M> {
13+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'_, 'mir, 'tcx, M> {
1414
/// Applies the binary operation `op` to the two operands and writes a tuple of the result
1515
/// and a boolean signifying the potential overflow to the destination.
1616
pub fn binop_with_overflow(
@@ -46,7 +46,7 @@ impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M>
4646
}
4747
}
4848

49-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M> {
49+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'_, 'mir, 'tcx, M> {
5050
fn binary_char_op(
5151
&self,
5252
bin_op: mir::BinOp,

src/librustc_mir/interpret/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl<'tcx, Tag: ::std::fmt::Debug> PlaceTy<'tcx, Tag> {
247247
}
248248

249249
// separating the pointer tag for `impl Trait`, see https://github.com/rust-lang/rust/issues/54385
250-
impl<'infcx, 'mir, 'tcx, Tag, M> InterpCx<'infcx, 'mir, 'tcx, M>
250+
impl<'mir, 'tcx, Tag, M> InterpCx<'_, 'mir, 'tcx, M>
251251
where
252252
// FIXME: Working around https://github.com/rust-lang/rust/issues/54385
253253
Tag: ::std::fmt::Debug + Copy + Eq + Hash + 'static,

src/librustc_mir/interpret/step.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn binop_right_homogeneous(op: mir::BinOp) -> bool {
2929
}
3030
}
3131

32-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M> {
32+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'_, 'mir, 'tcx, M> {
3333
pub fn run(&mut self) -> InterpResult<'tcx> {
3434
while self.step()? {}
3535
Ok(())

src/librustc_mir/interpret/terminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::{
1010
FnVal, ImmTy, InterpCx, InterpResult, MPlaceTy, Machine, OpTy, PlaceTy, StackPopCleanup,
1111
};
1212

13-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M> {
13+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'_, 'mir, 'tcx, M> {
1414
pub(super) fn eval_terminator(
1515
&mut self,
1616
terminator: &mir::Terminator<'tcx>,

src/librustc_mir/interpret/traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc::mir::interpret::{InterpResult, Pointer, PointerArithmetic, Scalar};
44
use rustc::ty::layout::{Align, HasDataLayout, LayoutOf, Size};
55
use rustc::ty::{self, Instance, Ty, TypeFoldable};
66

7-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M> {
7+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'_, 'mir, 'tcx, M> {
88
/// Creates a dynamic vtable for the given type and vtable origin. This is used only for
99
/// objects.
1010
///

src/librustc_mir/interpret/validity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ impl<'rt, 'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'infcx, 'mir,
663663
}
664664
}
665665

666-
impl<'infcx, 'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'infcx, 'mir, 'tcx, M> {
666+
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'_, 'mir, 'tcx, M> {
667667
/// This function checks the data at `op`. `op` is assumed to cover valid memory if it
668668
/// is an indirect operand.
669669
/// It will error if the bits at the destination do not match the ones described by the layout.

src/librustc_traits/resolve_vtable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ pub fn resolve_vtable<'tcx>(
4848
// Currently, we use a fulfillment context to completely resolve
4949
// all nested obligations. This is because they can inform the
5050
// inference of the impl's type parameters.
51-
let mut vtable = selection.map(|predicate| {
51+
let vtable = selection.map(|predicate| {
5252
debug!("resolve_vtable: register_predicate_obligation {:?}", predicate);
5353
fulfill_cx.register_predicate_obligation(&infcx, predicate);
5454
});
5555

56-
vtable = infcx.resolve_vars_if_possible(&vtable);
57-
vtable = tcx.erase_regions(&vtable);
56+
let vtable = infcx.resolve_vars_if_possible(&vtable);
57+
let vtable = tcx.erase_regions(&vtable);
5858

5959
info!("Cache miss: {:?} => {:?}", trait_ref, vtable);
6060
Ok(vtable)

0 commit comments

Comments
 (0)