Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit baf4abf

Browse files
committedNov 21, 2023
Auto merge of rust-lang#118107 - matthiaskrgr:rollup-k5bfkfr, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#117327 (Add documentation for some queries) - rust-lang#117835 (Note about object lifetime defaults in does not live long enough error) - rust-lang#117851 (Uplift `InferConst` to `rustc_type_ir`) - rust-lang#117973 (test: Add test for async-move in 2015 Rust proc macro) - rust-lang#117992 (Don't require intercrate mode for negative coherence) - rust-lang#118010 (Typeck break expr even if break is illegal) - rust-lang#118026 (Don't consider regions in `deref_into_dyn_supertrait` lint) - rust-lang#118089 (intercrate_ambiguity_causes: handle self ty infer + reservation impls) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8534923 + 6c62b42 commit baf4abf

File tree

54 files changed

+568
-226
lines changed

Some content is hidden

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

54 files changed

+568
-226
lines changed
 

‎compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ use rustc_hir as hir;
55
use rustc_hir::intravisit::Visitor;
66
use rustc_index::IndexSlice;
77
use rustc_infer::infer::NllRegionVariableOrigin;
8+
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
89
use rustc_middle::mir::{
910
Body, CallSource, CastKind, ConstraintCategory, FakeReadCause, Local, LocalInfo, Location,
1011
Operand, Place, Rvalue, Statement, StatementKind, TerminatorKind,
1112
};
1213
use rustc_middle::ty::adjustment::PointerCoercion;
13-
use rustc_middle::ty::{self, RegionVid, TyCtxt};
14+
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
1415
use rustc_span::symbol::{kw, Symbol};
1516
use rustc_span::{sym, DesugaringKind, Span};
1617
use rustc_trait_selection::traits::error_reporting::FindExprBySpan;
@@ -290,12 +291,69 @@ impl<'tcx> BorrowExplanation<'tcx> {
290291
}
291292
}
292293

294+
if let ConstraintCategory::Cast { unsize_to: Some(unsize_ty) } = category {
295+
self.add_object_lifetime_default_note(tcx, err, unsize_ty);
296+
}
293297
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
294298
}
295299
_ => {}
296300
}
297301
}
298302

303+
fn add_object_lifetime_default_note(
304+
&self,
305+
tcx: TyCtxt<'tcx>,
306+
err: &mut Diagnostic,
307+
unsize_ty: Ty<'tcx>,
308+
) {
309+
if let ty::Adt(def, args) = unsize_ty.kind() {
310+
// We try to elaborate the object lifetime defaults and present those to the user. This should
311+
// make it clear where the region constraint is coming from.
312+
let generics = tcx.generics_of(def.did());
313+
314+
let mut has_dyn = false;
315+
let mut failed = false;
316+
317+
let elaborated_args = std::iter::zip(*args, &generics.params).map(|(arg, param)| {
318+
if let Some(ty::Dynamic(obj, _, ty::DynKind::Dyn)) = arg.as_type().map(Ty::kind) {
319+
let default = tcx.object_lifetime_default(param.def_id);
320+
321+
let re_static = tcx.lifetimes.re_static;
322+
323+
let implied_region = match default {
324+
// This is not entirely precise.
325+
ObjectLifetimeDefault::Empty => re_static,
326+
ObjectLifetimeDefault::Ambiguous => {
327+
failed = true;
328+
re_static
329+
}
330+
ObjectLifetimeDefault::Param(param_def_id) => {
331+
let index = generics.param_def_id_to_index[&param_def_id] as usize;
332+
args.get(index).and_then(|arg| arg.as_region()).unwrap_or_else(|| {
333+
failed = true;
334+
re_static
335+
})
336+
}
337+
ObjectLifetimeDefault::Static => re_static,
338+
};
339+
340+
has_dyn = true;
341+
342+
Ty::new_dynamic(tcx, obj, implied_region, ty::DynKind::Dyn).into()
343+
} else {
344+
arg
345+
}
346+
});
347+
let elaborated_ty = Ty::new_adt(tcx, *def, tcx.mk_args_from_iter(elaborated_args));
348+
349+
if has_dyn && !failed {
350+
err.note(format!(
351+
"due to object lifetime defaults, `{unsize_ty}` actually means `{elaborated_ty}`"
352+
));
353+
}
354+
}
355+
}
356+
299357
fn add_lifetime_bound_suggestion_to_diagnostic(
300358
&self,
301359
err: &mut Diagnostic,

‎compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
5353
ConstraintCategory::Yield => "yielding this value ",
5454
ConstraintCategory::UseAsConst => "using this value as a constant ",
5555
ConstraintCategory::UseAsStatic => "using this value as a static ",
56-
ConstraintCategory::Cast => "cast ",
56+
ConstraintCategory::Cast { .. } => "cast ",
5757
ConstraintCategory::CallArgument(_) => "argument ",
5858
ConstraintCategory::TypeAnnotation => "type annotation ",
5959
ConstraintCategory::ClosureBounds => "closure body ",

0 commit comments

Comments
 (0)
This repository has been archived.