Skip to content

Commit 42d009c

Browse files
committed
Auto merge of #147186 - Zalathar:rollup-sza9wxl, r=Zalathar
Rollup of 5 pull requests Successful merges: - #140916 (Fix unuseful span in type error in some format_args!() invocations) - #146011 (Point at fn bound that introduced lifetime obligation) - #146649 (cmse: fix 'region variables should not be hashed') - #147109 (Rename various "concrete opaque type" things to say "hidden type") - #147167 (Don't condition RUSTDOC_LIBDIR on `--no-doc`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents a2db928 + 2264303 commit 42d009c

File tree

47 files changed

+499
-161
lines changed

Some content is hidden

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

47 files changed

+499
-161
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
426426
}
427427

428428
pub(crate) fn path_does_not_live_long_enough(&self, span: Span, path: &str) -> Diag<'infcx> {
429-
struct_span_code_err!(self.dcx(), span, E0597, "{} does not live long enough", path,)
429+
struct_span_code_err!(self.dcx(), span, E0597, "{} does not live long enough", path)
430430
}
431431

432432
pub(crate) fn cannot_return_reference_to_local(
@@ -480,7 +480,7 @@ impl<'infcx, 'tcx> crate::MirBorrowckCtxt<'_, 'infcx, 'tcx> {
480480
}
481481

482482
pub(crate) fn temporary_value_borrowed_for_too_long(&self, span: Span) -> Diag<'infcx> {
483-
struct_span_code_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed",)
483+
struct_span_code_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed")
484484
}
485485
}
486486

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,6 +2992,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
29922992
self.buffer_error(err);
29932993
}
29942994

2995+
#[tracing::instrument(level = "debug", skip(self, explanation))]
29952996
fn report_local_value_does_not_live_long_enough(
29962997
&self,
29972998
location: Location,
@@ -3001,13 +3002,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30013002
borrow_spans: UseSpans<'tcx>,
30023003
explanation: BorrowExplanation<'tcx>,
30033004
) -> Diag<'infcx> {
3004-
debug!(
3005-
"report_local_value_does_not_live_long_enough(\
3006-
{:?}, {:?}, {:?}, {:?}, {:?}\
3007-
)",
3008-
location, name, borrow, drop_span, borrow_spans
3009-
);
3010-
30113005
let borrow_span = borrow_spans.var_or_use_path_span();
30123006
if let BorrowExplanation::MustBeValidFor {
30133007
category,

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,26 @@ impl<'tcx> BorrowExplanation<'tcx> {
416416
{
417417
self.add_object_lifetime_default_note(tcx, err, unsize_ty);
418418
}
419+
420+
let mut preds = path
421+
.iter()
422+
.filter_map(|constraint| match constraint.category {
423+
ConstraintCategory::Predicate(pred) if !pred.is_dummy() => Some(pred),
424+
_ => None,
425+
})
426+
.collect::<Vec<Span>>();
427+
preds.sort();
428+
preds.dedup();
429+
if !preds.is_empty() {
430+
let s = if preds.len() == 1 { "" } else { "s" };
431+
err.span_note(
432+
preds,
433+
format!(
434+
"requirement{s} that the value outlives `{region_name}` introduced here"
435+
),
436+
);
437+
}
438+
419439
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
420440
}
421441
_ => {}

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub fn provide(providers: &mut Providers) {
119119
fn mir_borrowck(
120120
tcx: TyCtxt<'_>,
121121
def: LocalDefId,
122-
) -> Result<&ConcreteOpaqueTypes<'_>, ErrorGuaranteed> {
122+
) -> Result<&DefinitionSiteHiddenTypes<'_>, ErrorGuaranteed> {
123123
assert!(!tcx.is_typeck_child(def.to_def_id()));
124124
let (input_body, _) = tcx.mir_promoted(def);
125125
debug!("run query mir_borrowck: {}", tcx.def_path_str(def));
@@ -130,7 +130,7 @@ fn mir_borrowck(
130130
Err(guar)
131131
} else if input_body.should_skip() {
132132
debug!("Skipping borrowck because of injected body");
133-
let opaque_types = ConcreteOpaqueTypes(Default::default());
133+
let opaque_types = DefinitionSiteHiddenTypes(Default::default());
134134
Ok(tcx.arena.alloc(opaque_types))
135135
} else {
136136
let mut root_cx = BorrowCheckRootCtxt::new(tcx, def, None);

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,10 +1382,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
13821382
}
13831383

13841384
/// The constraints we get from equating the hidden type of each use of an opaque
1385-
/// with its final concrete type may end up getting preferred over other, potentially
1385+
/// with its final hidden type may end up getting preferred over other, potentially
13861386
/// longer constraint paths.
13871387
///
1388-
/// Given that we compute the final concrete type by relying on this existing constraint
1388+
/// Given that we compute the final hidden type by relying on this existing constraint
13891389
/// path, this can easily end up hiding the actual reason for why we require these regions
13901390
/// to be equal.
13911391
///

compiler/rustc_borrowck/src/region_infer/opaque_types/mod.rs

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_infer::infer::outlives::env::RegionBoundPairs;
88
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, OpaqueTypeStorageEntries};
99
use rustc_infer::traits::ObligationCause;
1010
use rustc_macros::extension;
11-
use rustc_middle::mir::{Body, ConcreteOpaqueTypes, ConstraintCategory};
11+
use rustc_middle::mir::{Body, ConstraintCategory, DefinitionSiteHiddenTypes};
1212
use rustc_middle::ty::{
1313
self, DefiningScopeKind, EarlyBinder, FallibleTypeFolder, GenericArg, GenericArgsRef,
1414
OpaqueHiddenType, OpaqueTypeKey, Region, RegionVid, Ty, TyCtxt, TypeFoldable,
@@ -129,17 +129,17 @@ fn nll_var_to_universal_region<'tcx>(
129129
/// Collect all defining uses of opaque types inside of this typeck root. This
130130
/// expects the hidden type to be mapped to the definition parameters of the opaque
131131
/// and errors if we end up with distinct hidden types.
132-
fn add_concrete_opaque_type<'tcx>(
132+
fn add_hidden_type<'tcx>(
133133
tcx: TyCtxt<'tcx>,
134-
concrete_opaque_types: &mut ConcreteOpaqueTypes<'tcx>,
134+
hidden_types: &mut DefinitionSiteHiddenTypes<'tcx>,
135135
def_id: LocalDefId,
136136
hidden_ty: OpaqueHiddenType<'tcx>,
137137
) {
138138
// Sometimes two opaque types are the same only after we remap the generic parameters
139139
// back to the opaque type definition. E.g. we may have `OpaqueType<X, Y>` mapped to
140140
// `(X, Y)` and `OpaqueType<Y, X>` mapped to `(Y, X)`, and those are the same, but we
141141
// only know that once we convert the generic parameters to those of the opaque type.
142-
if let Some(prev) = concrete_opaque_types.0.get_mut(&def_id) {
142+
if let Some(prev) = hidden_types.0.get_mut(&def_id) {
143143
if prev.ty != hidden_ty.ty {
144144
let guar = hidden_ty.ty.error_reported().err().unwrap_or_else(|| {
145145
let (Ok(e) | Err(e)) = prev.build_mismatch_error(&hidden_ty, tcx).map(|d| d.emit());
@@ -151,15 +151,15 @@ fn add_concrete_opaque_type<'tcx>(
151151
// FIXME(oli-obk): collect multiple spans for better diagnostics down the road.
152152
prev.span = prev.span.substitute_dummy(hidden_ty.span);
153153
} else {
154-
concrete_opaque_types.0.insert(def_id, hidden_ty);
154+
hidden_types.0.insert(def_id, hidden_ty);
155155
}
156156
}
157157

158-
fn get_concrete_opaque_type<'tcx>(
159-
concrete_opaque_types: &ConcreteOpaqueTypes<'tcx>,
158+
fn get_hidden_type<'tcx>(
159+
hidden_types: &DefinitionSiteHiddenTypes<'tcx>,
160160
def_id: LocalDefId,
161161
) -> Option<EarlyBinder<'tcx, OpaqueHiddenType<'tcx>>> {
162-
concrete_opaque_types.0.get(&def_id).map(|ty| EarlyBinder::bind(*ty))
162+
hidden_types.0.get(&def_id).map(|ty| EarlyBinder::bind(*ty))
163163
}
164164

165165
#[derive(Debug)]
@@ -173,22 +173,22 @@ struct DefiningUse<'tcx> {
173173
}
174174

175175
/// This computes the actual hidden types of the opaque types and maps them to their
176-
/// definition sites. Outside of registering the computed concrete types this function
176+
/// definition sites. Outside of registering the computed hidden types this function
177177
/// does not mutate the current borrowck state.
178178
///
179179
/// While it may fail to infer the hidden type and return errors, we always apply
180-
/// the computed concrete hidden type to all opaque type uses to check whether they
180+
/// the computed hidden type to all opaque type uses to check whether they
181181
/// are correct. This is necessary to support non-defining uses of opaques in their
182182
/// defining scope.
183183
///
184184
/// It also means that this whole function is not really soundness critical as we
185185
/// recheck all uses of the opaques regardless.
186-
pub(crate) fn compute_concrete_opaque_types<'tcx>(
186+
pub(crate) fn compute_definition_site_hidden_types<'tcx>(
187187
infcx: &BorrowckInferCtxt<'tcx>,
188188
universal_region_relations: &Frozen<UniversalRegionRelations<'tcx>>,
189189
constraints: &MirTypeckRegionConstraints<'tcx>,
190190
location_map: Rc<DenseLocationMap>,
191-
concrete_opaque_types: &mut ConcreteOpaqueTypes<'tcx>,
191+
hidden_types: &mut DefinitionSiteHiddenTypes<'tcx>,
192192
opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)],
193193
) -> Vec<DeferredOpaqueTypeError<'tcx>> {
194194
let mut errors = Vec::new();
@@ -201,20 +201,19 @@ pub(crate) fn compute_concrete_opaque_types<'tcx>(
201201
// We start by checking each use of an opaque type during type check and
202202
// check whether the generic arguments of the opaque type are fully
203203
// universal, if so, it's a defining use.
204-
let defining_uses =
205-
collect_defining_uses(&mut rcx, concrete_opaque_types, opaque_types, &mut errors);
204+
let defining_uses = collect_defining_uses(&mut rcx, hidden_types, opaque_types, &mut errors);
206205

207206
// We now compute and apply member constraints for all regions in the hidden
208207
// types of each defining use. This mutates the region values of the `rcx` which
209208
// is used when mapping the defining uses to the definition site.
210209
apply_member_constraints(&mut rcx, &defining_uses);
211210

212211
// After applying member constraints, we now check whether all member regions ended
213-
// up equal to one of their choice regions and compute the actual concrete type of
212+
// up equal to one of their choice regions and compute the actual hidden type of
214213
// the opaque type definition. This is stored in the `root_cx`.
215-
compute_concrete_types_from_defining_uses(
214+
compute_definition_site_hidden_types_from_defining_uses(
216215
&rcx,
217-
concrete_opaque_types,
216+
hidden_types,
218217
&defining_uses,
219218
&mut errors,
220219
);
@@ -224,7 +223,7 @@ pub(crate) fn compute_concrete_opaque_types<'tcx>(
224223
#[instrument(level = "debug", skip_all, ret)]
225224
fn collect_defining_uses<'tcx>(
226225
rcx: &mut RegionCtxt<'_, 'tcx>,
227-
concrete_opaque_types: &mut ConcreteOpaqueTypes<'tcx>,
226+
hidden_types: &mut DefinitionSiteHiddenTypes<'tcx>,
228227
opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)],
229228
errors: &mut Vec<DeferredOpaqueTypeError<'tcx>>,
230229
) -> Vec<DefiningUse<'tcx>> {
@@ -244,9 +243,9 @@ fn collect_defining_uses<'tcx>(
244243
// with `TypingMode::Borrowck`.
245244
if infcx.tcx.use_typing_mode_borrowck() {
246245
match err {
247-
NonDefiningUseReason::Tainted(guar) => add_concrete_opaque_type(
246+
NonDefiningUseReason::Tainted(guar) => add_hidden_type(
248247
infcx.tcx,
249-
concrete_opaque_types,
248+
hidden_types,
250249
opaque_type_key.def_id,
251250
OpaqueHiddenType::new_error(infcx.tcx, guar),
252251
),
@@ -277,9 +276,9 @@ fn collect_defining_uses<'tcx>(
277276
defining_uses
278277
}
279278

280-
fn compute_concrete_types_from_defining_uses<'tcx>(
279+
fn compute_definition_site_hidden_types_from_defining_uses<'tcx>(
281280
rcx: &RegionCtxt<'_, 'tcx>,
282-
concrete_opaque_types: &mut ConcreteOpaqueTypes<'tcx>,
281+
hidden_types: &mut DefinitionSiteHiddenTypes<'tcx>,
283282
defining_uses: &[DefiningUse<'tcx>],
284283
errors: &mut Vec<DeferredOpaqueTypeError<'tcx>>,
285284
) {
@@ -358,9 +357,9 @@ fn compute_concrete_types_from_defining_uses<'tcx>(
358357
},
359358
));
360359
}
361-
add_concrete_opaque_type(
360+
add_hidden_type(
362361
tcx,
363-
concrete_opaque_types,
362+
hidden_types,
364363
opaque_type_key.def_id,
365364
OpaqueHiddenType { span: hidden_type.span, ty },
366365
);
@@ -489,20 +488,20 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for ToArgRegionsFolder<'_, 'tcx> {
489488
///
490489
/// It does this by equating the hidden type of each use with the instantiated final
491490
/// hidden type of the opaque.
492-
pub(crate) fn apply_computed_concrete_opaque_types<'tcx>(
491+
pub(crate) fn apply_definition_site_hidden_types<'tcx>(
493492
infcx: &BorrowckInferCtxt<'tcx>,
494493
body: &Body<'tcx>,
495494
universal_regions: &UniversalRegions<'tcx>,
496495
region_bound_pairs: &RegionBoundPairs<'tcx>,
497496
known_type_outlives_obligations: &[ty::PolyTypeOutlivesPredicate<'tcx>],
498497
constraints: &mut MirTypeckRegionConstraints<'tcx>,
499-
concrete_opaque_types: &mut ConcreteOpaqueTypes<'tcx>,
498+
hidden_types: &mut DefinitionSiteHiddenTypes<'tcx>,
500499
opaque_types: &[(OpaqueTypeKey<'tcx>, OpaqueHiddenType<'tcx>)],
501500
) -> Vec<DeferredOpaqueTypeError<'tcx>> {
502501
let tcx = infcx.tcx;
503502
let mut errors = Vec::new();
504503
for &(key, hidden_type) in opaque_types {
505-
let Some(expected) = get_concrete_opaque_type(concrete_opaque_types, key.def_id) else {
504+
let Some(expected) = get_hidden_type(hidden_types, key.def_id) else {
506505
if !tcx.use_typing_mode_borrowck() {
507506
if let ty::Alias(ty::Opaque, alias_ty) = hidden_type.ty.kind()
508507
&& alias_ty.def_id == key.def_id.to_def_id()
@@ -521,12 +520,7 @@ pub(crate) fn apply_computed_concrete_opaque_types<'tcx>(
521520
hidden_type.span,
522521
"non-defining use in the defining scope with no defining uses",
523522
);
524-
add_concrete_opaque_type(
525-
tcx,
526-
concrete_opaque_types,
527-
key.def_id,
528-
OpaqueHiddenType::new_error(tcx, guar),
529-
);
523+
add_hidden_type(tcx, hidden_types, key.def_id, OpaqueHiddenType::new_error(tcx, guar));
530524
continue;
531525
};
532526

@@ -566,18 +560,13 @@ pub(crate) fn apply_computed_concrete_opaque_types<'tcx>(
566560
"equating opaque types",
567561
),
568562
) {
569-
add_concrete_opaque_type(
570-
tcx,
571-
concrete_opaque_types,
572-
key.def_id,
573-
OpaqueHiddenType::new_error(tcx, guar),
574-
);
563+
add_hidden_type(tcx, hidden_types, key.def_id, OpaqueHiddenType::new_error(tcx, guar));
575564
}
576565
}
577566
errors
578567
}
579568

580-
/// In theory `apply_concrete_opaque_types` could introduce new uses of opaque types.
569+
/// In theory `apply_definition_site_hidden_types` could introduce new uses of opaque types.
581570
/// We do not check these new uses so this could be unsound.
582571
///
583572
/// We detect any new uses and simply delay a bug if they occur. If this results in
@@ -682,13 +671,6 @@ impl<'tcx> InferCtxt<'tcx> {
682671
///
683672
/// (*) C1 and C2 were introduced in the comments on
684673
/// `register_member_constraints`. Read that comment for more context.
685-
///
686-
/// # Parameters
687-
///
688-
/// - `def_id`, the `impl Trait` type
689-
/// - `args`, the args used to instantiate this opaque type
690-
/// - `instantiated_ty`, the inferred type C1 -- fully resolved, lifted version of
691-
/// `opaque_defn.concrete_ty`
692674
#[instrument(level = "debug", skip(self))]
693675
fn infer_opaque_definition_from_instantiation(
694676
&self,

compiler/rustc_borrowck/src/root_cx.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ use smallvec::SmallVec;
1212
use crate::consumers::BorrowckConsumer;
1313
use crate::nll::compute_closure_requirements_modulo_opaques;
1414
use crate::region_infer::opaque_types::{
15-
apply_computed_concrete_opaque_types, clone_and_resolve_opaque_types,
16-
compute_concrete_opaque_types, detect_opaque_types_added_while_handling_opaque_types,
15+
apply_definition_site_hidden_types, clone_and_resolve_opaque_types,
16+
compute_definition_site_hidden_types, detect_opaque_types_added_while_handling_opaque_types,
1717
};
1818
use crate::type_check::{Locations, constraint_conversion};
1919
use crate::{
20-
ClosureRegionRequirements, CollectRegionConstraintsResult, ConcreteOpaqueTypes,
20+
ClosureRegionRequirements, CollectRegionConstraintsResult, DefinitionSiteHiddenTypes,
2121
PropagatedBorrowCheckResults, borrowck_check_region_constraints,
2222
borrowck_collect_region_constraints,
2323
};
@@ -27,7 +27,7 @@ use crate::{
2727
pub(super) struct BorrowCheckRootCtxt<'tcx> {
2828
pub tcx: TyCtxt<'tcx>,
2929
root_def_id: LocalDefId,
30-
concrete_opaque_types: ConcreteOpaqueTypes<'tcx>,
30+
hidden_types: DefinitionSiteHiddenTypes<'tcx>,
3131
/// The region constraints computed by [borrowck_collect_region_constraints]. This uses
3232
/// an [FxIndexMap] to guarantee that iterating over it visits nested bodies before
3333
/// their parents.
@@ -49,7 +49,7 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
4949
BorrowCheckRootCtxt {
5050
tcx,
5151
root_def_id,
52-
concrete_opaque_types: Default::default(),
52+
hidden_types: Default::default(),
5353
collect_region_constraints_results: Default::default(),
5454
propagated_borrowck_results: Default::default(),
5555
tainted_by_errors: None,
@@ -72,11 +72,11 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
7272
&self.propagated_borrowck_results[&nested_body_def_id].used_mut_upvars
7373
}
7474

75-
pub(super) fn finalize(self) -> Result<&'tcx ConcreteOpaqueTypes<'tcx>, ErrorGuaranteed> {
75+
pub(super) fn finalize(self) -> Result<&'tcx DefinitionSiteHiddenTypes<'tcx>, ErrorGuaranteed> {
7676
if let Some(guar) = self.tainted_by_errors {
7777
Err(guar)
7878
} else {
79-
Ok(self.tcx.arena.alloc(self.concrete_opaque_types))
79+
Ok(self.tcx.arena.alloc(self.hidden_types))
8080
}
8181
}
8282

@@ -88,12 +88,12 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
8888
&input.universal_region_relations,
8989
&mut input.constraints,
9090
);
91-
input.deferred_opaque_type_errors = compute_concrete_opaque_types(
91+
input.deferred_opaque_type_errors = compute_definition_site_hidden_types(
9292
&input.infcx,
9393
&input.universal_region_relations,
9494
&input.constraints,
9595
Rc::clone(&input.location_map),
96-
&mut self.concrete_opaque_types,
96+
&mut self.hidden_types,
9797
&opaque_types,
9898
);
9999
per_body_info.push((num_entries, opaque_types));
@@ -103,14 +103,14 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
103103
self.collect_region_constraints_results.values_mut().zip(per_body_info)
104104
{
105105
if input.deferred_opaque_type_errors.is_empty() {
106-
input.deferred_opaque_type_errors = apply_computed_concrete_opaque_types(
106+
input.deferred_opaque_type_errors = apply_definition_site_hidden_types(
107107
&input.infcx,
108108
&input.body_owned,
109109
&input.universal_region_relations.universal_regions,
110110
&input.region_bound_pairs,
111111
&input.known_type_outlives_obligations,
112112
&mut input.constraints,
113-
&mut self.concrete_opaque_types,
113+
&mut self.hidden_types,
114114
&opaque_types,
115115
);
116116
}

0 commit comments

Comments
 (0)