Skip to content

Commit 2a17174

Browse files
committed
Auto merge of #107309 - matthiaskrgr:rollup-0wgq6be, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #105345 (Add hint for missing lifetime bound on trait object when type alias is used) - #106897 (Tweak E0597) - #106944 (Suggest using a lock for `*Cell: Sync` bounds) - #107239 (Bring tests back into rustc source tarball) - #107244 (rustdoc: rearrange HTML in primitive reference links) - #107255 (add test where we ignore hr implied bounds) - #107256 (Delete `SimplifyArmIdentity` and `SimplifyBranchSame` mir opts) - #107266 (rustdoc: prohibit scroll bar on source viewer in Safari) - #107282 (erica solver: implement builtin `Pointee` trait impl candidates) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents c18a5e8 + a20b86f commit 2a17174

File tree

274 files changed

+1433
-1433
lines changed

Some content is hidden

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

274 files changed

+1433
-1433
lines changed

compiler/rustc_borrowck/src/borrowck_errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
3737
desc,
3838
);
3939

40-
err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_desc));
40+
err.span_label(borrow_span, format!("{} is borrowed here", borrow_desc));
4141
err.span_label(span, format!("use of borrowed {}", borrow_desc));
4242
err
4343
}
@@ -250,8 +250,8 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
250250
desc,
251251
);
252252

253-
err.span_label(borrow_span, format!("borrow of {} occurs here", desc));
254-
err.span_label(span, format!("assignment to borrowed {} occurs here", desc));
253+
err.span_label(borrow_span, format!("{} is borrowed here", desc));
254+
err.span_label(span, format!("{} is assigned to here but it was already borrowed", desc));
255255
err
256256
}
257257

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17361736
&self.local_names,
17371737
&mut err,
17381738
"",
1739-
None,
1739+
Some(borrow_span),
17401740
None,
17411741
);
17421742
}

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Print diagnostics to explain why values are borrowed.
22
33
use rustc_errors::{Applicability, Diagnostic};
4+
use rustc_hir as hir;
5+
use rustc_hir::intravisit::Visitor;
46
use rustc_index::vec::IndexVec;
57
use rustc_infer::infer::NllRegionVariableOrigin;
68
use rustc_middle::mir::{
@@ -11,6 +13,7 @@ use rustc_middle::ty::adjustment::PointerCast;
1113
use rustc_middle::ty::{self, RegionVid, TyCtxt};
1214
use rustc_span::symbol::{kw, Symbol};
1315
use rustc_span::{sym, DesugaringKind, Span};
16+
use rustc_trait_selection::traits::error_reporting::FindExprBySpan;
1417

1518
use crate::region_infer::{BlameConstraint, ExtraConstraintInfo};
1619
use crate::{
@@ -63,6 +66,36 @@ impl<'tcx> BorrowExplanation<'tcx> {
6366
borrow_span: Option<Span>,
6467
multiple_borrow_span: Option<(Span, Span)>,
6568
) {
69+
if let Some(span) = borrow_span {
70+
let def_id = body.source.def_id();
71+
if let Some(node) = tcx.hir().get_if_local(def_id)
72+
&& let Some(body_id) = node.body_id()
73+
{
74+
let body = tcx.hir().body(body_id);
75+
let mut expr_finder = FindExprBySpan::new(span);
76+
expr_finder.visit_expr(body.value);
77+
if let Some(mut expr) = expr_finder.result {
78+
while let hir::ExprKind::AddrOf(_, _, inner)
79+
| hir::ExprKind::Unary(hir::UnOp::Deref, inner)
80+
| hir::ExprKind::Field(inner, _)
81+
| hir::ExprKind::MethodCall(_, inner, _, _)
82+
| hir::ExprKind::Index(inner, _) = &expr.kind
83+
{
84+
expr = inner;
85+
}
86+
if let hir::ExprKind::Path(hir::QPath::Resolved(None, p)) = expr.kind
87+
&& let [hir::PathSegment { ident, args: None, .. }] = p.segments
88+
&& let hir::def::Res::Local(hir_id) = p.res
89+
&& let Some(hir::Node::Pat(pat)) = tcx.hir().find(hir_id)
90+
{
91+
err.span_label(
92+
pat.span,
93+
&format!("binding `{ident}` declared here"),
94+
);
95+
}
96+
}
97+
}
98+
}
6699
match *self {
67100
BorrowExplanation::UsedLater(later_use_kind, var_or_use_span, path_span) => {
68101
let message = match later_use_kind {

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
448448
};
449449
self.note_type_does_not_implement_copy(err, &place_desc, place_ty, Some(span), "");
450450

451-
use_spans.args_span_label(err, format!("move out of {place_desc} occurs here"));
451+
use_spans.args_span_label(err, format!("{place_desc} is moved here"));
452452
}
453453
}
454454
}

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+40-19
Original file line numberDiff line numberDiff line change
@@ -813,17 +813,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
813813
if *outlived_f != ty::ReStatic {
814814
return;
815815
}
816+
let suitable_region = self.infcx.tcx.is_suitable_region(f);
817+
let Some(suitable_region) = suitable_region else { return; };
816818

817-
let fn_returns = self
818-
.infcx
819-
.tcx
820-
.is_suitable_region(f)
821-
.map(|r| self.infcx.tcx.return_type_impl_or_dyn_traits(r.def_id))
822-
.unwrap_or_default();
823-
824-
if fn_returns.is_empty() {
825-
return;
826-
}
819+
let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.def_id);
827820

828821
let param = if let Some(param) = find_param_with_region(self.infcx.tcx, f, outlived_f) {
829822
param
@@ -839,15 +832,43 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
839832
};
840833
let captures = format!("captures data from {arg}");
841834

842-
return nice_region_error::suggest_new_region_bound(
843-
self.infcx.tcx,
844-
diag,
845-
fn_returns,
846-
lifetime.to_string(),
847-
Some(arg),
848-
captures,
849-
Some((param.param_ty_span, param.param_ty.to_string())),
850-
self.infcx.tcx.is_suitable_region(f).map(|r| r.def_id),
835+
if !fn_returns.is_empty() {
836+
nice_region_error::suggest_new_region_bound(
837+
self.infcx.tcx,
838+
diag,
839+
fn_returns,
840+
lifetime.to_string(),
841+
Some(arg),
842+
captures,
843+
Some((param.param_ty_span, param.param_ty.to_string())),
844+
Some(suitable_region.def_id),
845+
);
846+
return;
847+
}
848+
849+
let Some((alias_tys, alias_span)) = self
850+
.infcx
851+
.tcx
852+
.return_type_impl_or_dyn_traits_with_type_alias(suitable_region.def_id) else { return; };
853+
854+
// in case the return type of the method is a type alias
855+
let mut spans_suggs: Vec<_> = Vec::new();
856+
for alias_ty in alias_tys {
857+
if alias_ty.span.desugaring_kind().is_some() {
858+
// Skip `async` desugaring `impl Future`.
859+
()
860+
}
861+
if let TyKind::TraitObject(_, lt, _) = alias_ty.kind {
862+
spans_suggs.push((lt.ident.span.shrink_to_hi(), " + 'a".to_string()));
863+
}
864+
}
865+
spans_suggs.push((alias_span.shrink_to_hi(), "<'a>".to_string()));
866+
diag.multipart_suggestion_verbose(
867+
&format!(
868+
"to declare that the trait object {captures}, you can add a lifetime parameter `'a` in the type alias"
869+
),
870+
spans_suggs,
871+
Applicability::MaybeIncorrect,
851872
);
852873
}
853874
}

compiler/rustc_hir/src/hir.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3524,6 +3524,13 @@ impl<'hir> Node<'hir> {
35243524
}
35253525
}
35263526

3527+
pub fn alias_ty(self) -> Option<&'hir Ty<'hir>> {
3528+
match self {
3529+
Node::Item(Item { kind: ItemKind::TyAlias(ty, ..), .. }) => Some(ty),
3530+
_ => None,
3531+
}
3532+
}
3533+
35273534
pub fn body_id(&self) -> Option<BodyId> {
35283535
match self {
35293536
Node::TraitItem(TraitItem {

compiler/rustc_middle/src/ty/context.rs

+24
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,30 @@ impl<'tcx> TyCtxt<'tcx> {
997997
v.0
998998
}
999999

1000+
/// Given a `DefId` for an `fn`, return all the `dyn` and `impl` traits in its return type and associated alias span when type alias is used
1001+
pub fn return_type_impl_or_dyn_traits_with_type_alias(
1002+
self,
1003+
scope_def_id: LocalDefId,
1004+
) -> Option<(Vec<&'tcx hir::Ty<'tcx>>, Span)> {
1005+
let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id);
1006+
let mut v = TraitObjectVisitor(vec![], self.hir());
1007+
// when the return type is a type alias
1008+
if let Some(hir::FnDecl { output: hir::FnRetTy::Return(hir_output), .. }) = self.hir().fn_decl_by_hir_id(hir_id)
1009+
&& let hir::TyKind::Path(hir::QPath::Resolved(
1010+
None,
1011+
hir::Path { res: hir::def::Res::Def(DefKind::TyAlias, def_id), .. }, )) = hir_output.kind
1012+
&& let Some(local_id) = def_id.as_local()
1013+
&& let Some(alias_ty) = self.hir().get_by_def_id(local_id).alias_ty() // it is type alias
1014+
&& let Some(alias_generics) = self.hir().get_by_def_id(local_id).generics()
1015+
{
1016+
v.visit_ty(alias_ty);
1017+
if !v.0.is_empty() {
1018+
return Some((v.0, alias_generics.span));
1019+
}
1020+
}
1021+
return None;
1022+
}
1023+
10001024
pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> {
10011025
// `type_of()` will fail on these (#55796, #86483), so only allow `fn`s or closures.
10021026
match self.hir().get_by_def_id(scope_def_id) {

compiler/rustc_mir_transform/src/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ mod shim;
9090
pub mod simplify;
9191
mod simplify_branches;
9292
mod simplify_comparison_integral;
93-
mod simplify_try;
9493
mod sroa;
9594
mod uninhabited_enum_branching;
9695
mod unreachable_prop;
@@ -567,8 +566,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
567566
&o1(simplify_branches::SimplifyConstCondition::new("after-const-prop")),
568567
&early_otherwise_branch::EarlyOtherwiseBranch,
569568
&simplify_comparison_integral::SimplifyComparisonIntegral,
570-
&simplify_try::SimplifyArmIdentity,
571-
&simplify_try::SimplifyBranchSame,
572569
&dead_store_elimination::DeadStoreElimination,
573570
&dest_prop::DestinationPropagation,
574571
&o1(simplify_branches::SimplifyConstCondition::new("final")),

0 commit comments

Comments
 (0)