Skip to content

Commit 697739d

Browse files
author
Alexander Regueiro
committed
Aggregation of drive-by cosmetic changes.
1 parent c7b5f4d commit 697739d

File tree

11 files changed

+73
-63
lines changed

11 files changed

+73
-63
lines changed

src/librustc/hir/lowering.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ use crate::hir::map::{DefKey, DefPathData, Definitions};
3737
use crate::hir::def_id::{DefId, DefIndex, DefIndexAddressSpace, CRATE_DEF_INDEX};
3838
use crate::hir::def::{Def, PathResolution, PerNS};
3939
use crate::hir::{GenericArg, ConstArg};
40-
use crate::lint::builtin::{self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
41-
ELIDED_LIFETIMES_IN_PATHS};
40+
use crate::lint::builtin::{
41+
self, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES, ELIDED_LIFETIMES_IN_PATHS
42+
};
4243
use crate::middle::cstore::CrateStore;
4344
use crate::session::Session;
4445
use crate::session::config::nightly_options;
@@ -5145,7 +5146,7 @@ impl<'a> LoweringContext<'a> {
51455146
/// `Box<dyn Debug + '_>`. In those cases, `lower_lifetime` is invoked.
51465147
fn elided_dyn_bound(&mut self, span: Span) -> hir::Lifetime {
51475148
match self.anonymous_lifetime_mode {
5148-
// NB. We intentionally ignore the create-parameter mode here.
5149+
// N.B., We intentionally ignore the create-parameter mode here.
51495150
// and instead "pass through" to resolve-lifetimes, which will apply
51505151
// the object-lifetime-defaulting rules. Elided object lifetime defaults
51515152
// do not act like other elided lifetimes. In other words, given this:

src/librustc/infer/opaque_types/mod.rs

+45-36
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ use crate::util::nodemap::DefIdMap;
1313

1414
pub type OpaqueTypeMap<'tcx> = DefIdMap<OpaqueTypeDecl<'tcx>>;
1515

16-
/// Information about the opaque, abstract types whose values we
16+
/// Information about the opaque, existential types whose values we
1717
/// are inferring in this function (these are the `impl Trait` that
1818
/// appear in the return type).
1919
#[derive(Copy, Clone, Debug)]
2020
pub struct OpaqueTypeDecl<'tcx> {
21-
/// The substitutions that we apply to the abstract that this
21+
/// The substitutions that we apply to the existential type that this
2222
/// `impl Trait` desugars to. e.g., if:
2323
///
2424
/// fn foo<'a, 'b, T>() -> impl Trait<'a>
2525
///
2626
/// winds up desugared to:
2727
///
28-
/// abstract type Foo<'x, X>: Trait<'x>
28+
/// existential type Foo<'x, X>: Trait<'x>
2929
/// fn foo<'a, 'b, T>() -> Foo<'a, T>
3030
///
3131
/// then `substs` would be `['a, T]`.
3232
pub substs: SubstsRef<'tcx>,
3333

34-
/// The type variable that represents the value of the abstract type
34+
/// The type variable that represents the value of the existential type
3535
/// that we require. In other words, after we compile this function,
36-
/// we will be created a constraint like:
36+
/// we will have created a constraint like:
3737
///
3838
/// Foo<'a, T> = ?C
3939
///
@@ -141,12 +141,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
141141
/// Here, we have two `impl Trait` types whose values are being
142142
/// inferred (the `impl Bar<'a>` and the `impl
143143
/// Bar<'b>`). Conceptually, this is sugar for a setup where we
144-
/// define underlying abstract types (`Foo1`, `Foo2`) and then, in
144+
/// define underlying existential types (`Foo1`, `Foo2`) and then, in
145145
/// the return type of `foo`, we *reference* those definitions:
146146
///
147147
/// ```text
148-
/// abstract type Foo1<'x>: Bar<'x>;
149-
/// abstract type Foo2<'x>: Bar<'x>;
148+
/// existential type Foo1<'x>: Bar<'x>;
149+
/// existential type Foo2<'x>: Bar<'x>;
150150
/// fn foo<'a, 'b>(..) -> (Foo1<'a>, Foo2<'b>) { .. }
151151
/// // ^^^^ ^^
152152
/// // | |
@@ -205,7 +205,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
205205
///
206206
/// This is actually a bit of a tricky constraint in general. We
207207
/// want to say that each variable (e.g., `'0`) can only take on
208-
/// values that were supplied as arguments to the abstract type
208+
/// values that were supplied as arguments to the existential type
209209
/// (e.g., `'a` for `Foo1<'a>`) or `'static`, which is always in
210210
/// scope. We don't have a constraint quite of this kind in the current
211211
/// region checker.
@@ -232,10 +232,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
232232
/// # The `free_region_relations` parameter
233233
///
234234
/// The `free_region_relations` argument is used to find the
235-
/// "minimum" of the regions supplied to a given abstract type.
235+
/// "minimum" of the regions supplied to a given existential type.
236236
/// It must be a relation that can answer whether `'a <= 'b`,
237237
/// where `'a` and `'b` are regions that appear in the "substs"
238-
/// for the abstract type references (the `<'a>` in `Foo1<'a>`).
238+
/// for the existential type references (the `<'a>` in `Foo1<'a>`).
239239
///
240240
/// Note that we do not impose the constraints based on the
241241
/// generic regions from the `Foo1` definition (e.g., `'x`). This
@@ -251,7 +251,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
251251
///
252252
/// Here, the fact that `'b: 'a` is known only because of the
253253
/// implied bounds from the `&'a &'b u32` parameter, and is not
254-
/// "inherent" to the abstract type definition.
254+
/// "inherent" to the existential type definition.
255255
///
256256
/// # Parameters
257257
///
@@ -364,16 +364,16 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
364364
}
365365

366366
Component::Param(_) => {
367-
// ignore type parameters like `T`, they are captured
368-
// implicitly by the `impl Trait`
367+
// Ignore type parameters like `T`, since they are captured
368+
// implicitly by the `impl Trait`.
369369
}
370370

371371
Component::UnresolvedInferenceVariable(_) => {
372-
// we should get an error that more type
373-
// annotations are needed in this case
372+
// We should get an error that more type
373+
// annotations are needed in this case.
374374
self.tcx
375375
.sess
376-
.delay_span_bug(span, "unresolved inf var in opaque");
376+
.delay_span_bug(span, "unresolved inf var in opaque type");
377377
}
378378

379379
Component::Projection(ty::ProjectionTy {
@@ -397,20 +397,32 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
397397
}
398398
}
399399
}
400+
401+
// For soundness, we need to ensure that every region that is captured by the opaque type
402+
// but does not explicitly appear in the opaque type outlives the actual (concrete) type.
403+
// This allows for invariant lifetimes to be captured by opaque types as long as
404+
// short-lived lifetimes are not permitted to escape and cause UB.
405+
let opaque_substs_regions = opaque_defn.substs.regions().collect();
406+
let captured_regions = concrete_ty_regions.difference(&opaque_substs_regions);
407+
for captured_region in captured_regions {
408+
for concrete_ty_region in &concrete_ty_regions {
409+
self.sub_regions(infer::CallReturn(span), *concrete_ty_region, captured_region)
410+
}
411+
}
400412
}
401413

402414
/// Given the fully resolved, instantiated type for an opaque
403415
/// type, i.e., the value of an inference variable like C1 or C2
404-
/// (*), computes the "definition type" for an abstract type
416+
/// (*), computes the "definition type" for an existential type
405417
/// definition -- that is, the inferred value of `Foo1<'x>` or
406418
/// `Foo2<'x>` that we would conceptually use in its definition:
407419
///
408-
/// abstract type Foo1<'x>: Bar<'x> = AAA; <-- this type AAA
409-
/// abstract type Foo2<'x>: Bar<'x> = BBB; <-- or this type BBB
420+
/// existential type Foo1<'x>: Bar<'x> = AAA; <-- this type AAA
421+
/// existential type Foo2<'x>: Bar<'x> = BBB; <-- or this type BBB
410422
/// fn foo<'a, 'b>(..) -> (Foo1<'a>, Foo2<'b>) { .. }
411423
///
412-
/// Note that these values are defined in terms of a distinct set of
413-
/// generic parameters (`'x` instead of `'a`) from C1 or C2. The main
424+
/// Note that these values are defined in terms of a set of generic
425+
/// parameters (`'x` instead of `'a`) distinct from C1 and C2. The main
414426
/// purpose of this function is to do that translation.
415427
///
416428
/// (*) C1 and C2 were introduced in the comments on
@@ -450,23 +462,20 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
450462
.collect();
451463

452464
// Convert the type from the function into a type valid outside
453-
// the function, by replacing invalid regions with 'static,
465+
// the function, by replacing invalid regions with `'static`,
454466
// after producing an error for each of them.
455467
let definition_ty =
456-
instantiated_ty.fold_with(&mut ReverseMapper::new(
457-
self.tcx,
458-
self.is_tainted_by_errors(),
459-
def_id,
468+
instantiated_ty.fold_with(&mut ReverseMapper {
469+
tcx: self.tcx,
460470
map,
461-
instantiated_ty,
462-
));
471+
});
463472
debug!(
464473
"infer_opaque_definition_from_instantiation: definition_ty={:?}",
465474
definition_ty
466475
);
467476

468477
// We can unwrap here because our reverse mapper always
469-
// produces things with 'gcx lifetime, though the type folder
478+
// produces things with `'gcx` lifetime, though the type folder
470479
// obscures that.
471480
let definition_ty = gcx.lift(&definition_ty).unwrap();
472481

@@ -656,20 +665,20 @@ impl<'a, 'gcx, 'tcx> Instantiator<'a, 'gcx, 'tcx> {
656665
// value we are inferring. At present, this is
657666
// always true during the first phase of
658667
// type-check, but not always true later on during
659-
// NLL. Once we support named abstract types more fully,
668+
// NLL. Once we support named existential types more fully,
660669
// this same scenario will be able to arise during all phases.
661670
//
662-
// Here is an example using `abstract type` that indicates
671+
// Here is an example using `existential type` that indicates
663672
// the distinction we are checking for:
664673
//
665674
// ```rust
666675
// mod a {
667-
// pub abstract type Foo: Iterator;
668-
// pub fn make_foo() -> Foo { .. }
676+
// pub existential type Foo: Iterator;
677+
// pub fn make_foo() -> Foo { .. }
669678
// }
670679
//
671680
// mod b {
672-
// fn foo() -> a::Foo { a::make_foo() }
681+
// fn foo() -> a::Foo { a::make_foo() }
673682
// }
674683
// ```
675684
//
@@ -777,7 +786,7 @@ impl<'a, 'gcx, 'tcx> Instantiator<'a, 'gcx, 'tcx> {
777786
required_region_bounds
778787
);
779788

780-
// make sure that we are in fact defining the *entire* type
789+
// Make sure that we are in fact defining the *entire* type
781790
// e.g., `existential type Foo<T: Bound>: Bar;` needs to be
782791
// defined by a function like `fn foo<T: Bound>() -> Foo<T>`.
783792
debug!(

src/librustc/infer/outlives/env.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> OutlivesEnvironment<'tcx> {
197197
) where
198198
I: IntoIterator<Item = OutlivesBound<'tcx>>,
199199
{
200-
// Record relationships such as `T:'x` that don't go into the
201-
// free-region-map but which we use here.
200+
// Record relationships such as `T: 'x`, which don't go into the
201+
// free-region-map, but which we use here.
202202
for outlives_bound in outlives_bounds {
203203
debug!("add_outlives_bounds: outlives_bound={:?}", outlives_bound);
204204
match outlives_bound {

src/librustc/infer/outlives/free_region_map.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl<'tcx> FreeRegionMap<'tcx> {
1515
self.relation.is_empty()
1616
}
1717

18-
// Record that `'sup:'sub`. Or, put another way, `'sub <= 'sup`.
18+
// Record that `'sup: 'sub`. Or, put another way, `'sub <= 'sup`.
1919
// (with the exception that `'static: 'x` is not notable)
2020
pub fn relate_regions(&mut self, sub: Region<'tcx>, sup: Region<'tcx>) {
2121
debug!("relate_regions(sub={:?}, sup={:?})", sub, sup);
@@ -24,7 +24,7 @@ impl<'tcx> FreeRegionMap<'tcx> {
2424
}
2525
}
2626

27-
/// Computes the least-upper-bound of two free regions. In some
27+
/// Computes the least upper bound of two free regions. In some
2828
/// cases, this is more conservative than necessary, in order to
2929
/// avoid making arbitrary choices. See
3030
/// `TransitiveRelation::postdom_upper_bound` for more details.
@@ -90,6 +90,7 @@ impl_stable_hash_for!(struct FreeRegionMap<'tcx> {
9090

9191
impl<'a, 'tcx> Lift<'tcx> for FreeRegionMap<'a> {
9292
type Lifted = FreeRegionMap<'tcx>;
93+
9394
fn lift_to_tcx<'b, 'gcx>(&self, tcx: TyCtxt<'b, 'gcx, 'tcx>) -> Option<FreeRegionMap<'tcx>> {
9495
self.relation.maybe_map(|&fr| tcx.lift(&fr))
9596
.map(|relation| FreeRegionMap { relation })

src/librustc/traits/auto_trait.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,9 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
141141
// selection and projection:
142142
//
143143
// * We can always cache the result of a particular trait selection for the lifetime of
144-
// an InfCtxt
144+
// an `InferCtxt`.
145145
// * Given a projection bound such as '<T as SomeTrait>::SomeItem = K', if 'T:
146-
// SomeTrait' doesn't hold, then we don't need to care about the 'SomeItem = K'
146+
// SomeTrait' doesn't hold, then we don't need to care about the 'SomeItem = K'.
147147
//
148148
// We fix the first assumption by manually clearing out all of the InferCtxt's caches
149149
// in between calls to SelectionContext.select. This allows us to keep all of the

src/librustc/traits/query/outlives_bounds.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
7272
/// argument types are well-formed. This may imply certain relationships
7373
/// between generic parameters. For example:
7474
///
75-
/// fn foo<'a,T>(x: &'a T)
75+
/// fn foo<'a, T>(x: &'a T)
7676
///
7777
/// can only be called with a `'a` and `T` such that `&'a T` is WF.
7878
/// For `&'a T` to be WF, `T: 'a` must hold. So we can assume `T: 'a`.
@@ -102,7 +102,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
102102
Err(NoSolution) => {
103103
self.tcx.sess.delay_span_bug(
104104
span,
105-
"implied_outlives_bounds failed to solve all obligations"
105+
"implied_outlives_bounds: failed to solve all obligations",
106106
);
107107
return vec![];
108108
}
@@ -117,7 +117,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
117117
Err(_) => {
118118
self.tcx.sess.delay_span_bug(
119119
span,
120-
"implied_outlives_bounds failed to instantiate"
120+
"implied_outlives_bounds: failed to instantiate",
121121
);
122122
return vec![];
123123
}
@@ -130,7 +130,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
130130
if fulfill_cx.select_all_or_error(self).is_err() {
131131
self.tcx.sess.delay_span_bug(
132132
span,
133-
"implied_outlives_bounds failed to solve obligations from instantiation"
133+
"implied_outlives_bounds: failed to solve obligations from instantiation",
134134
);
135135
}
136136

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ pub struct ParamEnv<'tcx> {
16431643
pub reveal: traits::Reveal,
16441644

16451645
/// If this `ParamEnv` comes from a call to `tcx.param_env(def_id)`,
1646-
/// register that `def_id` (useful for transitioning to the chalk trait
1646+
/// register that `def_id` (useful for transitioning to the Chalk trait
16471647
/// solver).
16481648
pub def_id: Option<DefId>,
16491649
}

src/librustc/ty/outlives.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// The outlines relation `T: 'a` or `'a: 'b`. This code frequently
1+
// The outlives relation; `T: 'a` or `'a: 'b`. This code frequently
22
// refers to rules defined in RFC 1214 (`OutlivesFooBar`), so see that
33
// RFC for reference.
44

src/librustc/ty/query/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ rustc_query_append! { [define_queries!][ <'tcx>
158158
[] fn is_const_fn_raw: IsConstFn(DefId) -> bool,
159159

160160

161-
/// Returns true if calls to the function may be promoted
161+
/// Returns `true` if calls to the function may be promoted.
162162
///
163163
/// This is either because the function is e.g., a tuple-struct or tuple-variant
164164
/// constructor, or because it has the `#[rustc_promotable]` attribute. The attribute should
@@ -174,22 +174,22 @@ rustc_query_append! { [define_queries!][ <'tcx>
174174
/// instead.
175175
[] fn crate_variances: crate_variances(CrateNum) -> Lrc<ty::CrateVariancesMap>,
176176

177-
/// Maps from def-id of a type or region parameter to its
177+
/// Maps from def-ID of a type or region parameter to its
178178
/// (inferred) variance.
179179
[] fn variances_of: ItemVariances(DefId) -> Lrc<Vec<ty::Variance>>,
180180
},
181181

182182
TypeChecking {
183-
/// Maps from def-id of a type to its (inferred) outlives.
183+
/// Maps from def-ID of a type to its (inferred) outlives.
184184
[] fn inferred_outlives_crate: InferredOutlivesCrate(CrateNum)
185185
-> Lrc<ty::CratePredicatesMap<'tcx>>,
186186
},
187187

188188
Other {
189-
/// Maps from an impl/trait def-id to a list of the def-ids of its items
189+
/// Maps from an impl/trait def-ID to a list of the def-IDs of its items.
190190
[] fn associated_item_def_ids: AssociatedItemDefIds(DefId) -> Lrc<Vec<DefId>>,
191191

192-
/// Maps from a trait item to the trait item "descriptor"
192+
/// Maps from a trait item to the trait item "descriptor".
193193
[] fn associated_item: AssociatedItems(DefId) -> ty::AssociatedItem,
194194

195195
[] fn impl_trait_ref: ImplTraitRef(DefId) -> Option<ty::TraitRef<'tcx>>,

src/librustc_data_structures/transitive_relation.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ use std::fmt::Debug;
77
use std::hash::Hash;
88
use std::mem;
99

10-
1110
#[derive(Clone, Debug)]
1211
pub struct TransitiveRelation<T: Clone + Debug + Eq + Hash> {
13-
// List of elements. This is used to map from a T to a usize.
12+
// List of elements. This is used to map from a `T` to a `usize`.
1413
elements: Vec<T>,
1514

1615
// Maps each element to an index.
@@ -22,7 +21,7 @@ pub struct TransitiveRelation<T: Clone + Debug + Eq + Hash> {
2221

2322
// This is a cached transitive closure derived from the edges.
2423
// Currently, we build it lazilly and just throw out any existing
25-
// copy whenever a new edge is added. (The Lock is to permit
24+
// copy whenever a new edge is added. (The `Lock` is to permit
2625
// the lazy computation.) This is kind of silly, except for the
2726
// fact its size is tied to `self.elements.len()`, so I wanted to
2827
// wait before building it up to avoid reallocating as new edges
@@ -209,10 +208,10 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
209208
}
210209
};
211210

212-
// in some cases, there are some arbitrary choices to be made;
211+
// In some cases, there are some arbitrary choices to be made;
213212
// it doesn't really matter what we pick, as long as we pick
214213
// the same thing consistently when queried, so ensure that
215-
// (a, b) are in a consistent relative order
214+
// (a, b) are in a consistent relative order.
216215
if a > b {
217216
mem::swap(&mut a, &mut b);
218217
}

0 commit comments

Comments
 (0)