Skip to content

Commit b260c9e

Browse files
committed
Auto merge of #143057 - matthiaskrgr:rollup-bulih8o, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #124595 (Suggest cloning `Arc` moved into closure) - #139594 (Simplify `ObligationCauseCode::IfExpression`) - #141311 (make `tidy-alphabetical` use a natural sort) - #141648 ([rustdoc] Do not emit redundant_explicit_links lint if the doc comment comes from expansion) - #142285 (tests: Do not run afoul of asm.validity.non-exhaustive in input-stats) - #142393 (Don't give APITs names with macro expansion placeholder fragments in it) - #142884 (StableMIR: Add method to retrieve body of coroutine) - #142981 (Make missing lifetime suggestion verbose) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3b9d04c + 2f8b715 commit b260c9e

File tree

99 files changed

+1204
-569
lines changed

Some content is hidden

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

99 files changed

+1204
-569
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4064,9 +4064,9 @@ mod size_asserts {
40644064
static_assert_size!(MetaItemLit, 40);
40654065
static_assert_size!(Param, 40);
40664066
static_assert_size!(Pat, 72);
4067+
static_assert_size!(PatKind, 48);
40674068
static_assert_size!(Path, 24);
40684069
static_assert_size!(PathSegment, 24);
4069-
static_assert_size!(PatKind, 48);
40704070
static_assert_size!(Stmt, 32);
40714071
static_assert_size!(StmtKind, 16);
40724072
static_assert_size!(Ty, 64);

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
518518
} = move_spans
519519
&& can_suggest_clone
520520
{
521-
self.suggest_cloning(err, ty, expr, Some(move_spans));
521+
self.suggest_cloning(err, place.as_ref(), ty, expr, Some(move_spans));
522522
} else if self.suggest_hoisting_call_outside_loop(err, expr) && can_suggest_clone {
523523
// The place where the type moves would be misleading to suggest clone.
524524
// #121466
525-
self.suggest_cloning(err, ty, expr, Some(move_spans));
525+
self.suggest_cloning(err, place.as_ref(), ty, expr, Some(move_spans));
526526
}
527527
}
528528

@@ -1224,6 +1224,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12241224
pub(crate) fn suggest_cloning(
12251225
&self,
12261226
err: &mut Diag<'_>,
1227+
place: PlaceRef<'tcx>,
12271228
ty: Ty<'tcx>,
12281229
expr: &'tcx hir::Expr<'tcx>,
12291230
use_spans: Option<UseSpans<'tcx>>,
@@ -1238,7 +1239,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12381239
}
12391240

12401241
if self.implements_clone(ty) {
1241-
self.suggest_cloning_inner(err, ty, expr);
1242+
if self.in_move_closure(expr) {
1243+
if let Some(name) = self.describe_place(place) {
1244+
self.suggest_clone_of_captured_var_in_move_closure(err, &name, use_spans);
1245+
}
1246+
} else {
1247+
self.suggest_cloning_inner(err, ty, expr);
1248+
}
12421249
} else if let ty::Adt(def, args) = ty.kind()
12431250
&& def.did().as_local().is_some()
12441251
&& def.variants().iter().all(|variant| {
@@ -1505,7 +1512,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
15051512
if let hir::ExprKind::AddrOf(_, _, borrowed_expr) = expr.kind
15061513
&& let Some(ty) = typeck_results.expr_ty_opt(borrowed_expr)
15071514
{
1508-
self.suggest_cloning(&mut err, ty, borrowed_expr, Some(move_spans));
1515+
self.suggest_cloning(&mut err, place.as_ref(), ty, borrowed_expr, Some(move_spans));
15091516
} else if typeck_results.expr_adjustments(expr).first().is_some_and(|adj| {
15101517
matches!(
15111518
adj.kind,
@@ -1518,7 +1525,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
15181525
)
15191526
}) && let Some(ty) = typeck_results.expr_ty_opt(expr)
15201527
{
1521-
self.suggest_cloning(&mut err, ty, expr, Some(move_spans));
1528+
self.suggest_cloning(&mut err, place.as_ref(), ty, expr, Some(move_spans));
15221529
}
15231530
}
15241531
self.buffer_error(err);

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,17 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
325325
self.cannot_move_out_of(span, &description)
326326
}
327327

328-
fn suggest_clone_of_captured_var_in_move_closure(
328+
pub(in crate::diagnostics) fn suggest_clone_of_captured_var_in_move_closure(
329329
&self,
330330
err: &mut Diag<'_>,
331-
upvar_hir_id: HirId,
332331
upvar_name: &str,
333332
use_spans: Option<UseSpans<'tcx>>,
334333
) {
335334
let tcx = self.infcx.tcx;
336-
let typeck_results = tcx.typeck(self.mir_def_id());
337335
let Some(use_spans) = use_spans else { return };
338336
// We only care about the case where a closure captured a binding.
339337
let UseSpans::ClosureUse { args_span, .. } = use_spans else { return };
340338
let Some(body_id) = tcx.hir_node(self.mir_hir_id()).body_id() else { return };
341-
// Fetch the type of the expression corresponding to the closure-captured binding.
342-
let Some(captured_ty) = typeck_results.node_type_opt(upvar_hir_id) else { return };
343-
if !self.implements_clone(captured_ty) {
344-
// We only suggest cloning the captured binding if the type can actually be cloned.
345-
return;
346-
};
347339
// Find the closure that captured the binding.
348340
let mut expr_finder = FindExprBySpan::new(args_span, tcx);
349341
expr_finder.include_closures = true;
@@ -396,7 +388,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
396388
.indentation_before(stmt.span)
397389
.unwrap_or_else(|| " ".to_string());
398390
err.multipart_suggestion_verbose(
399-
"clone the value before moving it into the closure",
391+
"consider cloning the value before moving it into the closure",
400392
vec![
401393
(
402394
stmt.span.shrink_to_lo(),
@@ -426,7 +418,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
426418
.indentation_before(closure_expr.span)
427419
.unwrap_or_else(|| " ".to_string());
428420
err.multipart_suggestion_verbose(
429-
"clone the value before moving it into the closure",
421+
"consider cloning the value before moving it into the closure",
430422
vec![
431423
(
432424
closure_expr.span.shrink_to_lo(),
@@ -523,20 +515,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
523515
);
524516

525517
let closure_span = tcx.def_span(def_id);
526-
let mut err = self
527-
.cannot_move_out_of(span, &place_description)
518+
self.cannot_move_out_of(span, &place_description)
528519
.with_span_label(upvar_span, "captured outer variable")
529520
.with_span_label(
530521
closure_span,
531522
format!("captured by this `{closure_kind}` closure"),
532-
);
533-
self.suggest_clone_of_captured_var_in_move_closure(
534-
&mut err,
535-
upvar_hir_id,
536-
&upvar_name,
537-
use_spans,
538-
);
539-
err
523+
)
540524
}
541525
_ => {
542526
let source = self.borrowed_content_source(deref_base);
@@ -597,7 +581,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
597581
};
598582

599583
if let Some(expr) = self.find_expr(span) {
600-
self.suggest_cloning(err, place_ty, expr, None);
584+
self.suggest_cloning(err, move_from.as_ref(), place_ty, expr, None);
601585
}
602586

603587
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
@@ -629,7 +613,13 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
629613
};
630614

631615
if let Some(expr) = self.find_expr(use_span) {
632-
self.suggest_cloning(err, place_ty, expr, Some(use_spans));
616+
self.suggest_cloning(
617+
err,
618+
original_path.as_ref(),
619+
place_ty,
620+
expr,
621+
Some(use_spans),
622+
);
633623
}
634624

635625
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {
@@ -832,7 +822,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
832822
let place_desc = self.local_name(*local).map(|sym| format!("`{sym}`"));
833823

834824
if let Some(expr) = self.find_expr(binding_span) {
835-
self.suggest_cloning(err, bind_to.ty, expr, None);
825+
let local_place: PlaceRef<'tcx> = (*local).into();
826+
self.suggest_cloning(err, local_place, bind_to.ty, expr, None);
836827
}
837828

838829
err.subdiagnostic(crate::session_diagnostics::TypeNoCopy::Label {

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,9 @@ mod size_asserts {
878878

879879
use super::*;
880880
// tidy-alphabetical-start
881-
static_assert_size!(Immediate, 48);
882881
static_assert_size!(ImmTy<'_>, 64);
883-
static_assert_size!(Operand, 56);
882+
static_assert_size!(Immediate, 48);
884883
static_assert_size!(OpTy<'_>, 72);
884+
static_assert_size!(Operand, 56);
885885
// tidy-alphabetical-end
886886
}

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,9 +1056,9 @@ mod size_asserts {
10561056

10571057
use super::*;
10581058
// tidy-alphabetical-start
1059+
static_assert_size!(MPlaceTy<'_>, 64);
10591060
static_assert_size!(MemPlace, 48);
10601061
static_assert_size!(MemPlaceMeta, 24);
1061-
static_assert_size!(MPlaceTy<'_>, 64);
10621062
static_assert_size!(Place, 48);
10631063
static_assert_size!(PlaceTy<'_>, 64);
10641064
// tidy-alphabetical-end

compiler/rustc_expand/src/base.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,10 @@ pub trait ResolverExpand {
11181118
trait_def_id: DefId,
11191119
impl_def_id: LocalDefId,
11201120
) -> Result<Vec<(Ident, Option<Ident>)>, Indeterminate>;
1121+
1122+
/// Record the name of an opaque `Ty::ImplTrait` pre-expansion so that it can be used
1123+
/// to generate an item name later that does not reference placeholder macros.
1124+
fn insert_impl_trait_name(&mut self, id: NodeId, name: Symbol);
11211125
}
11221126

11231127
pub trait LintStoreExpand {

compiler/rustc_expand/src/expand.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,16 @@ impl InvocationCollectorNode for ast::Ty {
17781778
fragment.make_ty()
17791779
}
17801780
fn walk(&mut self, collector: &mut InvocationCollector<'_, '_>) {
1781+
// Save the pre-expanded name of this `ImplTrait`, so that later when defining
1782+
// an APIT we use a name that doesn't have any placeholder fragments in it.
1783+
if let ast::TyKind::ImplTrait(..) = self.kind {
1784+
// HACK: pprust breaks strings with newlines when the type
1785+
// gets too long. We don't want these to show up in compiler
1786+
// output or built artifacts, so replace them here...
1787+
// Perhaps we should instead format APITs more robustly.
1788+
let name = Symbol::intern(&pprust::ty_to_string(self).replace('\n', " "));
1789+
collector.cx.resolver.insert_impl_trait_name(self.id, name);
1790+
}
17811791
walk_ty(collector, self)
17821792
}
17831793
fn is_mac_call(&self) -> bool {

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4992,9 +4992,9 @@ mod size_asserts {
49924992
static_assert_size!(LetStmt<'_>, 72);
49934993
static_assert_size!(Param<'_>, 32);
49944994
static_assert_size!(Pat<'_>, 72);
4995+
static_assert_size!(PatKind<'_>, 48);
49954996
static_assert_size!(Path<'_>, 40);
49964997
static_assert_size!(PathSegment<'_>, 48);
4997-
static_assert_size!(PatKind<'_>, 48);
49984998
static_assert_size!(QPath<'_>, 24);
49994999
static_assert_size!(Res, 12);
50005000
static_assert_size!(Stmt<'_>, 32);

compiler/rustc_hir_typeck/src/_match.rs

Lines changed: 5 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use rustc_errors::{Applicability, Diag};
22
use rustc_hir::def::{CtorOf, DefKind, Res};
33
use rustc_hir::def_id::LocalDefId;
4-
use rustc_hir::{self as hir, ExprKind, PatKind};
4+
use rustc_hir::{self as hir, ExprKind, HirId, PatKind};
55
use rustc_hir_pretty::ty_to_string;
66
use rustc_middle::ty::{self, Ty};
77
use rustc_span::Span;
88
use rustc_trait_selection::traits::{
9-
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
9+
MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
1010
};
1111
use tracing::{debug, instrument};
1212

@@ -414,105 +414,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
414414

415415
pub(crate) fn if_cause(
416416
&self,
417-
span: Span,
418-
cond_span: Span,
419-
then_expr: &'tcx hir::Expr<'tcx>,
417+
expr_id: HirId,
420418
else_expr: &'tcx hir::Expr<'tcx>,
421-
then_ty: Ty<'tcx>,
422-
else_ty: Ty<'tcx>,
423419
tail_defines_return_position_impl_trait: Option<LocalDefId>,
424420
) -> ObligationCause<'tcx> {
425-
let mut outer_span = if self.tcx.sess.source_map().is_multiline(span) {
426-
// The `if`/`else` isn't in one line in the output, include some context to make it
427-
// clear it is an if/else expression:
428-
// ```
429-
// LL | let x = if true {
430-
// | _____________-
431-
// LL || 10i32
432-
// || ----- expected because of this
433-
// LL || } else {
434-
// LL || 10u32
435-
// || ^^^^^ expected `i32`, found `u32`
436-
// LL || };
437-
// ||_____- `if` and `else` have incompatible types
438-
// ```
439-
Some(span)
440-
} else {
441-
// The entire expression is in one line, only point at the arms
442-
// ```
443-
// LL | let x = if true { 10i32 } else { 10u32 };
444-
// | ----- ^^^^^ expected `i32`, found `u32`
445-
// | |
446-
// | expected because of this
447-
// ```
448-
None
449-
};
450-
451-
let (error_sp, else_id) = if let ExprKind::Block(block, _) = &else_expr.kind {
452-
let block = block.innermost_block();
453-
454-
// Avoid overlapping spans that aren't as readable:
455-
// ```
456-
// 2 | let x = if true {
457-
// | _____________-
458-
// 3 | | 3
459-
// | | - expected because of this
460-
// 4 | | } else {
461-
// | |____________^
462-
// 5 | ||
463-
// 6 | || };
464-
// | || ^
465-
// | ||_____|
466-
// | |______if and else have incompatible types
467-
// | expected integer, found `()`
468-
// ```
469-
// by not pointing at the entire expression:
470-
// ```
471-
// 2 | let x = if true {
472-
// | ------- `if` and `else` have incompatible types
473-
// 3 | 3
474-
// | - expected because of this
475-
// 4 | } else {
476-
// | ____________^
477-
// 5 | |
478-
// 6 | | };
479-
// | |_____^ expected integer, found `()`
480-
// ```
481-
if block.expr.is_none()
482-
&& block.stmts.is_empty()
483-
&& let Some(outer_span) = &mut outer_span
484-
&& let Some(cond_span) = cond_span.find_ancestor_inside(*outer_span)
485-
{
486-
*outer_span = outer_span.with_hi(cond_span.hi())
487-
}
488-
489-
(self.find_block_span(block), block.hir_id)
490-
} else {
491-
(else_expr.span, else_expr.hir_id)
492-
};
493-
494-
let then_id = if let ExprKind::Block(block, _) = &then_expr.kind {
495-
let block = block.innermost_block();
496-
// Exclude overlapping spans
497-
if block.expr.is_none() && block.stmts.is_empty() {
498-
outer_span = None;
499-
}
500-
block.hir_id
501-
} else {
502-
then_expr.hir_id
503-
};
421+
let error_sp = self.find_block_span_from_hir_id(else_expr.hir_id);
504422

505423
// Finally construct the cause:
506424
self.cause(
507425
error_sp,
508-
ObligationCauseCode::IfExpression(Box::new(IfExpressionCause {
509-
else_id,
510-
then_id,
511-
then_ty,
512-
else_ty,
513-
outer_span,
514-
tail_defines_return_position_impl_trait,
515-
})),
426+
ObligationCauseCode::IfExpression { expr_id, tail_defines_return_position_impl_trait },
516427
)
517428
}
518429

0 commit comments

Comments
 (0)