Skip to content

Commit 9a60099

Browse files
committed
Auto merge of #93956 - matthiaskrgr:rollup-zfk35hb, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #89926 (make `Instant::{duration_since, elapsed, sub}` saturating and remove workarounds) - #90532 (More informative error message for E0015) - #93810 (Improve chalk integration) - #93851 (More practical examples for `Option::and_then` & `Result::and_then`) - #93885 (bootstrap.py: Suggest disabling download-ci-llvm option if url fails to download) - #93886 (Stabilise inherent_ascii_escape (FCP in #77174)) - #93930 (add link to format_args! when mention it in docs) - #93936 (Couple of driver cleanups) - #93944 (Don't relabel to a team if there is already a team label) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 3fe2299 + 20ea5c5 commit 9a60099

File tree

157 files changed

+1551
-1131
lines changed

Some content is hidden

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

157 files changed

+1551
-1131
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use either::Either;
2+
use rustc_const_eval::util::{CallDesugaringKind, CallKind};
23
use rustc_data_structures::fx::FxHashSet;
34
use rustc_errors::{Applicability, DiagnosticBuilder};
45
use rustc_hir as hir;
@@ -26,7 +27,7 @@ use crate::{
2627

2728
use super::{
2829
explain_borrow::{BorrowExplanation, LaterUseKind},
29-
FnSelfUseKind, IncludingDowncast, RegionName, RegionNameSource, UseSpans,
30+
IncludingDowncast, RegionName, RegionNameSource, UseSpans,
3031
};
3132

3233
#[derive(Debug)]
@@ -195,7 +196,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
195196
.map(|n| format!("`{}`", n))
196197
.unwrap_or_else(|| "value".to_owned());
197198
match kind {
198-
FnSelfUseKind::FnOnceCall => {
199+
CallKind::FnCall { fn_trait_id, .. }
200+
if Some(fn_trait_id) == self.infcx.tcx.lang_items().fn_once_trait() =>
201+
{
199202
err.span_label(
200203
fn_call_span,
201204
&format!(
@@ -208,7 +211,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
208211
"this value implements `FnOnce`, which causes it to be moved when called",
209212
);
210213
}
211-
FnSelfUseKind::Operator { self_arg } => {
214+
CallKind::Operator { self_arg, .. } => {
215+
let self_arg = self_arg.unwrap();
212216
err.span_label(
213217
fn_call_span,
214218
&format!(
@@ -235,12 +239,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
235239
);
236240
}
237241
}
238-
FnSelfUseKind::Normal {
239-
self_arg,
240-
implicit_into_iter,
241-
is_option_or_result,
242-
} => {
243-
if implicit_into_iter {
242+
CallKind::Normal { self_arg, desugaring, is_option_or_result } => {
243+
let self_arg = self_arg.unwrap();
244+
if let Some((CallDesugaringKind::ForLoopIntoIter, _)) = desugaring {
244245
err.span_label(
245246
fn_call_span,
246247
&format!(
@@ -305,8 +306,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
305306
);
306307
}
307308
}
308-
// Deref::deref takes &self, which cannot cause a move
309-
FnSelfUseKind::DerefCoercion { .. } => unreachable!(),
309+
// Other desugarings takes &self, which cannot cause a move
310+
_ => unreachable!(),
310311
}
311312
} else {
312313
err.span_label(
@@ -433,7 +434,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
433434
}
434435

435436
if let UseSpans::FnSelfUse {
436-
kind: FnSelfUseKind::DerefCoercion { deref_target, deref_target_ty },
437+
kind: CallKind::DerefCoercion { deref_target, deref_target_ty, .. },
437438
..
438439
} = use_spans
439440
{

compiler/rustc_borrowck/src/diagnostics/mod.rs

+22-93
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Borrow checker diagnostics.
22
3+
use rustc_const_eval::util::call_kind;
34
use rustc_errors::DiagnosticBuilder;
45
use rustc_hir as hir;
56
use rustc_hir::def::Namespace;
67
use rustc_hir::def_id::DefId;
7-
use rustc_hir::lang_items::LangItemGroup;
88
use rustc_hir::GeneratorKind;
99
use rustc_middle::mir::{
1010
AggregateKind, Constant, FakeReadCause, Field, Local, LocalInfo, LocalKind, Location, Operand,
@@ -13,7 +13,7 @@ use rustc_middle::mir::{
1313
use rustc_middle::ty::print::Print;
1414
use rustc_middle::ty::{self, DefIdTree, Instance, Ty, TyCtxt};
1515
use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult};
16-
use rustc_span::{hygiene::DesugaringKind, symbol::sym, Span};
16+
use rustc_span::{symbol::sym, Span};
1717
use rustc_target::abi::VariantIdx;
1818

1919
use super::borrow_set::BorrowData;
@@ -37,7 +37,7 @@ crate use mutability_errors::AccessKind;
3737
crate use outlives_suggestion::OutlivesSuggestionBuilder;
3838
crate use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};
3939
crate use region_name::{RegionName, RegionNameSource};
40-
use rustc_span::symbol::Ident;
40+
crate use rustc_const_eval::util::CallKind;
4141

4242
pub(super) struct IncludingDowncast(pub(super) bool);
4343

@@ -563,46 +563,23 @@ pub(super) enum UseSpans<'tcx> {
563563
fn_call_span: Span,
564564
/// The definition span of the method being called
565565
fn_span: Span,
566-
kind: FnSelfUseKind<'tcx>,
566+
kind: CallKind<'tcx>,
567567
},
568568
/// This access is caused by a `match` or `if let` pattern.
569569
PatUse(Span),
570570
/// This access has a single span associated to it: common case.
571571
OtherUse(Span),
572572
}
573573

574-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
575-
pub(super) enum FnSelfUseKind<'tcx> {
576-
/// A normal method call of the form `receiver.foo(a, b, c)`
577-
Normal {
578-
self_arg: Ident,
579-
implicit_into_iter: bool,
580-
/// Whether the self type of the method call has an `.as_ref()` method.
581-
/// Used for better diagnostics.
582-
is_option_or_result: bool,
583-
},
584-
/// A call to `FnOnce::call_once`, desugared from `my_closure(a, b, c)`
585-
FnOnceCall,
586-
/// A call to an operator trait, desuraged from operator syntax (e.g. `a << b`)
587-
Operator { self_arg: Ident },
588-
DerefCoercion {
589-
/// The `Span` of the `Target` associated type
590-
/// in the `Deref` impl we are using.
591-
deref_target: Span,
592-
/// The type `T::Deref` we are dereferencing to
593-
deref_target_ty: Ty<'tcx>,
594-
},
595-
}
596-
597574
impl UseSpans<'_> {
598575
pub(super) fn args_or_use(self) -> Span {
599576
match self {
600577
UseSpans::ClosureUse { args_span: span, .. }
601578
| UseSpans::PatUse(span)
602579
| UseSpans::OtherUse(span) => span,
603-
UseSpans::FnSelfUse {
604-
fn_call_span, kind: FnSelfUseKind::DerefCoercion { .. }, ..
605-
} => fn_call_span,
580+
UseSpans::FnSelfUse { fn_call_span, kind: CallKind::DerefCoercion { .. }, .. } => {
581+
fn_call_span
582+
}
606583
UseSpans::FnSelfUse { var_span, .. } => var_span,
607584
}
608585
}
@@ -613,9 +590,9 @@ impl UseSpans<'_> {
613590
UseSpans::ClosureUse { path_span: span, .. }
614591
| UseSpans::PatUse(span)
615592
| UseSpans::OtherUse(span) => span,
616-
UseSpans::FnSelfUse {
617-
fn_call_span, kind: FnSelfUseKind::DerefCoercion { .. }, ..
618-
} => fn_call_span,
593+
UseSpans::FnSelfUse { fn_call_span, kind: CallKind::DerefCoercion { .. }, .. } => {
594+
fn_call_span
595+
}
619596
UseSpans::FnSelfUse { var_span, .. } => var_span,
620597
}
621598
}
@@ -626,9 +603,9 @@ impl UseSpans<'_> {
626603
UseSpans::ClosureUse { capture_kind_span: span, .. }
627604
| UseSpans::PatUse(span)
628605
| UseSpans::OtherUse(span) => span,
629-
UseSpans::FnSelfUse {
630-
fn_call_span, kind: FnSelfUseKind::DerefCoercion { .. }, ..
631-
} => fn_call_span,
606+
UseSpans::FnSelfUse { fn_call_span, kind: CallKind::DerefCoercion { .. }, .. } => {
607+
fn_call_span
608+
}
632609
UseSpans::FnSelfUse { var_span, .. } => var_span,
633610
}
634611
}
@@ -904,67 +881,19 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
904881
return normal_ret;
905882
};
906883

907-
let tcx = self.infcx.tcx;
908-
let parent = tcx.parent(method_did);
909-
let is_fn_once = parent == tcx.lang_items().fn_once_trait();
910-
let is_operator = !from_hir_call
911-
&& parent.map_or(false, |p| tcx.lang_items().group(LangItemGroup::Op).contains(&p));
912-
let is_deref = !from_hir_call && tcx.is_diagnostic_item(sym::deref_method, method_did);
913-
let fn_call_span = *fn_span;
914-
915-
let self_arg = tcx.fn_arg_names(method_did)[0];
916-
917-
debug!(
918-
"terminator = {:?} from_hir_call={:?}",
919-
self.body[location.block].terminator, from_hir_call
884+
let kind = call_kind(
885+
self.infcx.tcx,
886+
self.param_env,
887+
method_did,
888+
method_substs,
889+
*fn_span,
890+
*from_hir_call,
891+
Some(self.infcx.tcx.fn_arg_names(method_did)[0]),
920892
);
921893

922-
// Check for a 'special' use of 'self' -
923-
// an FnOnce call, an operator (e.g. `<<`), or a
924-
// deref coercion.
925-
let kind = if is_fn_once {
926-
Some(FnSelfUseKind::FnOnceCall)
927-
} else if is_operator {
928-
Some(FnSelfUseKind::Operator { self_arg })
929-
} else if is_deref {
930-
let deref_target =
931-
tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {
932-
Instance::resolve(tcx, self.param_env, deref_target, method_substs)
933-
.transpose()
934-
});
935-
if let Some(Ok(instance)) = deref_target {
936-
let deref_target_ty = instance.ty(tcx, self.param_env);
937-
Some(FnSelfUseKind::DerefCoercion {
938-
deref_target: tcx.def_span(instance.def_id()),
939-
deref_target_ty,
940-
})
941-
} else {
942-
None
943-
}
944-
} else {
945-
None
946-
};
947-
948-
let kind = kind.unwrap_or_else(|| {
949-
// This isn't a 'special' use of `self`
950-
debug!("move_spans: method_did={:?}, fn_call_span={:?}", method_did, fn_call_span);
951-
let implicit_into_iter = Some(method_did) == tcx.lang_items().into_iter_fn()
952-
&& fn_call_span.desugaring_kind() == Some(DesugaringKind::ForLoop);
953-
let parent_self_ty = parent
954-
.filter(|did| tcx.def_kind(*did) == rustc_hir::def::DefKind::Impl)
955-
.and_then(|did| match tcx.type_of(did).kind() {
956-
ty::Adt(def, ..) => Some(def.did),
957-
_ => None,
958-
});
959-
let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
960-
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
961-
});
962-
FnSelfUseKind::Normal { self_arg, implicit_into_iter, is_option_or_result }
963-
});
964-
965894
return FnSelfUse {
966895
var_span: stmt.source_info.span,
967-
fn_call_span,
896+
fn_call_span: *fn_span,
968897
fn_span: self
969898
.infcx
970899
.tcx

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_const_eval::util::CallDesugaringKind;
12
use rustc_errors::{Applicability, DiagnosticBuilder};
23
use rustc_infer::infer::TyCtxtInferExt;
34
use rustc_middle::mir::*;
@@ -8,7 +9,7 @@ use rustc_mir_dataflow::move_paths::{
89
use rustc_span::{sym, Span, DUMMY_SP};
910
use rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions;
1011

11-
use crate::diagnostics::{FnSelfUseKind, UseSpans};
12+
use crate::diagnostics::{CallKind, UseSpans};
1213
use crate::prefixes::PrefixSet;
1314
use crate::MirBorrowckCtxt;
1415

@@ -410,7 +411,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
410411
Applicability::MaybeIncorrect,
411412
);
412413
} else if let Some(UseSpans::FnSelfUse {
413-
kind: FnSelfUseKind::Normal { implicit_into_iter: true, .. },
414+
kind:
415+
CallKind::Normal { desugaring: Some((CallDesugaringKind::ForLoopIntoIter, _)), .. },
414416
..
415417
}) = use_spans
416418
{

0 commit comments

Comments
 (0)