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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 16 additions & 0 deletions
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

Lines changed: 17 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 6 additions & 4 deletions
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

Lines changed: 6 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

0 commit comments

Comments
 (0)