Skip to content

Commit a07f3eb

Browse files
committed
Auto merge of rust-lang#123823 - matthiaskrgr:rollup-8zdtggx, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#122882 (Avoid a panic in `set_output_capture` in the default panic handler) - rust-lang#123523 (Account for trait/impl difference when suggesting changing argument from ref to mut ref) - rust-lang#123744 (Silence `unused_imports` for redundant imports) - rust-lang#123784 (Replace `document.write` with `document.head.insertAdjacent`) - rust-lang#123798 (Avoid invalid socket address in length calculation) - rust-lang#123804 (Stop using `HirId` for fn-like parents since closures are not `OwnerNode`s) - rust-lang#123806 (Panic on overflow in `BorrowedCursor::advance`) - rust-lang#123820 (Add my former address to .mailmap) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 616a8f8 + d2e9ec7 commit a07f3eb

Some content is hidden

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

46 files changed

+330
-367
lines changed

.mailmap

+1
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ Takashi Idobe <[email protected]>
543543
Takayuki Maeda <[email protected]>
544544
Tamir Duberstein <[email protected]> Tamir Duberstein <[email protected]>
545545
Tatsuyuki Ishi <[email protected]>
546+
546547
Tero Hänninen <[email protected]> Tero Hänninen <[email protected]>
547548
548549
Theo Belaire <[email protected]> Theo Belaire <[email protected]>

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+54-35
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_trait_selection::traits::error_reporting::suggestions::TypeErrCtxtExt;
2121

2222
use crate::diagnostics::BorrowedContentSource;
2323
use crate::util::FindAssignments;
24-
use crate::MirBorrowckCtxt;
24+
use crate::{session_diagnostics, MirBorrowckCtxt};
2525

2626
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2727
pub(crate) enum AccessKind {
@@ -234,7 +234,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
234234
Some(mir::BorrowKind::Mut { kind: mir::MutBorrowKind::Default }),
235235
|_kind, var_span| {
236236
let place = self.describe_any_place(access_place.as_ref());
237-
crate::session_diagnostics::CaptureVarCause::MutableBorrowUsePlaceClosure {
237+
session_diagnostics::CaptureVarCause::MutableBorrowUsePlaceClosure {
238238
place,
239239
var_span,
240240
}
@@ -667,19 +667,26 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
667667
/// User cannot make signature of a trait mutable without changing the
668668
/// trait. So we find if this error belongs to a trait and if so we move
669669
/// suggestion to the trait or disable it if it is out of scope of this crate
670-
fn is_error_in_trait(&self, local: Local) -> (bool, Option<Span>) {
670+
///
671+
/// The returned values are:
672+
/// - is the current item an assoc `fn` of an impl that corresponds to a trait def? if so, we
673+
/// have to suggest changing both the impl `fn` arg and the trait `fn` arg
674+
/// - is the trait from the local crate? If not, we can't suggest changing signatures
675+
/// - `Span` of the argument in the trait definition
676+
fn is_error_in_trait(&self, local: Local) -> (bool, bool, Option<Span>) {
671677
if self.body.local_kind(local) != LocalKind::Arg {
672-
return (false, None);
678+
return (false, false, None);
673679
}
674680
let my_def = self.body.source.def_id();
675681
let my_hir = self.infcx.tcx.local_def_id_to_hir_id(my_def.as_local().unwrap());
676682
let Some(td) =
677683
self.infcx.tcx.impl_of_method(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
678684
else {
679-
return (false, None);
685+
return (false, false, None);
680686
};
681687
(
682688
true,
689+
td.is_local(),
683690
td.as_local().and_then(|tld| match self.infcx.tcx.hir_node_by_def_id(tld) {
684691
Node::Item(hir::Item { kind: hir::ItemKind::Trait(_, _, _, _, items), .. }) => {
685692
let mut f_in_trait_opt = None;
@@ -695,19 +702,16 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
695702
break;
696703
}
697704
f_in_trait_opt.and_then(|f_in_trait| {
698-
match self.infcx.tcx.hir_node(f_in_trait) {
699-
Node::TraitItem(hir::TraitItem {
700-
kind:
701-
hir::TraitItemKind::Fn(
702-
hir::FnSig { decl: hir::FnDecl { inputs, .. }, .. },
703-
_,
704-
),
705-
..
706-
}) => {
707-
let hir::Ty { span, .. } = *inputs.get(local.index() - 1)?;
708-
Some(span)
709-
}
710-
_ => None,
705+
if let Node::TraitItem(ti) = self.infcx.tcx.hir_node(f_in_trait)
706+
&& let hir::TraitItemKind::Fn(sig, _) = ti.kind
707+
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
708+
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
709+
&& let hir::Mutability::Not = mut_ty.mutbl
710+
&& sig.decl.implicit_self.has_implicit_self()
711+
{
712+
Some(ty.span)
713+
} else {
714+
None
711715
}
712716
})
713717
}
@@ -1061,20 +1065,24 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10611065
let (pointer_sigil, pointer_desc) =
10621066
if local_decl.ty.is_ref() { ("&", "reference") } else { ("*const", "pointer") };
10631067

1064-
let (is_trait_sig, local_trait) = self.is_error_in_trait(local);
1065-
if is_trait_sig && local_trait.is_none() {
1068+
let (is_trait_sig, is_local, local_trait) = self.is_error_in_trait(local);
1069+
1070+
if is_trait_sig && !is_local {
1071+
// Do not suggest to change the signature when the trait comes from another crate.
1072+
err.span_label(
1073+
local_decl.source_info.span,
1074+
format!("this is an immutable {pointer_desc}"),
1075+
);
10661076
return;
10671077
}
1068-
1069-
let decl_span = match local_trait {
1070-
Some(span) => span,
1071-
None => local_decl.source_info.span,
1072-
};
1078+
let decl_span = local_decl.source_info.span;
10731079

10741080
let label = match *local_decl.local_info() {
10751081
LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => {
10761082
let suggestion = suggest_ampmut_self(self.infcx.tcx, decl_span);
1077-
Some((true, decl_span, suggestion))
1083+
let additional =
1084+
local_trait.map(|span| (span, suggest_ampmut_self(self.infcx.tcx, span)));
1085+
Some((true, decl_span, suggestion, additional))
10781086
}
10791087

10801088
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
@@ -1113,7 +1121,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11131121
// don't create labels for compiler-generated spans
11141122
Some(_) => None,
11151123
None => {
1116-
let label = if name != kw::SelfLower {
1124+
let (has_sugg, decl_span, sugg) = if name != kw::SelfLower {
11171125
suggest_ampmut(
11181126
self.infcx.tcx,
11191127
local_decl.ty,
@@ -1140,7 +1148,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11401148
),
11411149
}
11421150
};
1143-
Some(label)
1151+
Some((has_sugg, decl_span, sugg, None))
11441152
}
11451153
}
11461154
}
@@ -1151,22 +1159,33 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
11511159
})) => {
11521160
let pattern_span: Span = local_decl.source_info.span;
11531161
suggest_ref_mut(self.infcx.tcx, pattern_span)
1154-
.map(|span| (true, span, "mut ".to_owned()))
1162+
.map(|span| (true, span, "mut ".to_owned(), None))
11551163
}
11561164

11571165
_ => unreachable!(),
11581166
};
11591167

11601168
match label {
1161-
Some((true, err_help_span, suggested_code)) => {
1162-
err.span_suggestion_verbose(
1163-
err_help_span,
1164-
format!("consider changing this to be a mutable {pointer_desc}"),
1165-
suggested_code,
1169+
Some((true, err_help_span, suggested_code, additional)) => {
1170+
let mut sugg = vec![(err_help_span, suggested_code)];
1171+
if let Some(s) = additional {
1172+
sugg.push(s);
1173+
}
1174+
1175+
err.multipart_suggestion_verbose(
1176+
format!(
1177+
"consider changing this to be a mutable {pointer_desc}{}",
1178+
if is_trait_sig {
1179+
" in the `impl` method and the `trait` definition"
1180+
} else {
1181+
""
1182+
}
1183+
),
1184+
sugg,
11661185
Applicability::MachineApplicable,
11671186
);
11681187
}
1169-
Some((false, err_label_span, message)) => {
1188+
Some((false, err_label_span, message, _)) => {
11701189
let def_id = self.body.source.def_id();
11711190
let hir_id = if let Some(local_def_id) = def_id.as_local()
11721191
&& let Some(body_id) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)

compiler/rustc_hir_typeck/src/coercion.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -2004,16 +2004,17 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
20042004
}
20052005
}
20062006

2007-
let parent_id = fcx.tcx.hir().get_parent_item(id);
2008-
let mut parent_item = fcx.tcx.hir_node_by_def_id(parent_id.def_id);
2007+
let mut parent_id = fcx.tcx.hir().get_parent_item(id).def_id;
2008+
let mut parent_item = fcx.tcx.hir_node_by_def_id(parent_id);
20092009
// When suggesting return, we need to account for closures and async blocks, not just items.
20102010
for (_, node) in fcx.tcx.hir().parent_iter(id) {
20112011
match node {
20122012
hir::Node::Expr(&hir::Expr {
2013-
kind: hir::ExprKind::Closure(hir::Closure { .. }),
2013+
kind: hir::ExprKind::Closure(hir::Closure { def_id, .. }),
20142014
..
20152015
}) => {
20162016
parent_item = node;
2017+
parent_id = *def_id;
20172018
break;
20182019
}
20192020
hir::Node::Item(_) | hir::Node::TraitItem(_) | hir::Node::ImplItem(_) => break,
@@ -2023,13 +2024,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
20232024

20242025
if let (Some(expr), Some(_), Some(fn_decl)) = (expression, blk_id, parent_item.fn_decl()) {
20252026
fcx.suggest_missing_break_or_return_expr(
2026-
&mut err,
2027-
expr,
2028-
fn_decl,
2029-
expected,
2030-
found,
2031-
id,
2032-
parent_id.into(),
2027+
&mut err, expr, fn_decl, expected, found, id, parent_id,
20332028
);
20342029
}
20352030

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
942942
pub(in super::super) fn get_node_fn_decl(
943943
&self,
944944
node: Node<'tcx>,
945-
) -> Option<(hir::HirId, &'tcx hir::FnDecl<'tcx>, Ident, bool)> {
945+
) -> Option<(LocalDefId, &'tcx hir::FnDecl<'tcx>, Ident, bool)> {
946946
match node {
947947
Node::Item(&hir::Item {
948948
ident,
@@ -953,25 +953,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
953953
// This is less than ideal, it will not suggest a return type span on any
954954
// method called `main`, regardless of whether it is actually the entry point,
955955
// but it will still present it as the reason for the expected type.
956-
Some((
957-
hir::HirId::make_owner(owner_id.def_id),
958-
&sig.decl,
959-
ident,
960-
ident.name != sym::main,
961-
))
956+
Some((owner_id.def_id, &sig.decl, ident, ident.name != sym::main))
962957
}
963958
Node::TraitItem(&hir::TraitItem {
964959
ident,
965960
kind: hir::TraitItemKind::Fn(ref sig, ..),
966961
owner_id,
967962
..
968-
}) => Some((hir::HirId::make_owner(owner_id.def_id), &sig.decl, ident, true)),
963+
}) => Some((owner_id.def_id, &sig.decl, ident, true)),
969964
Node::ImplItem(&hir::ImplItem {
970965
ident,
971966
kind: hir::ImplItemKind::Fn(ref sig, ..),
972967
owner_id,
973968
..
974-
}) => Some((hir::HirId::make_owner(owner_id.def_id), &sig.decl, ident, false)),
969+
}) => Some((owner_id.def_id, &sig.decl, ident, false)),
975970
Node::Expr(&hir::Expr {
976971
hir_id,
977972
kind:
@@ -1001,12 +996,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1001996
}) => (ident, sig, owner_id),
1002997
_ => return None,
1003998
};
1004-
Some((
1005-
hir::HirId::make_owner(owner_id.def_id),
1006-
&sig.decl,
1007-
ident,
1008-
ident.name != sym::main,
1009-
))
999+
Some((owner_id.def_id, &sig.decl, ident, ident.name != sym::main))
10101000
}
10111001
_ => None,
10121002
}
@@ -1017,7 +1007,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10171007
pub fn get_fn_decl(
10181008
&self,
10191009
blk_id: hir::HirId,
1020-
) -> Option<(hir::HirId, &'tcx hir::FnDecl<'tcx>, bool)> {
1010+
) -> Option<(LocalDefId, &'tcx hir::FnDecl<'tcx>, bool)> {
10211011
// Get enclosing Fn, if it is a function or a trait method, unless there's a `loop` or
10221012
// `while` before reaching it, as block tail returns are not available in them.
10231013
self.tcx.hir().get_return_block(blk_id).and_then(|blk_id| {

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::method::probe::{IsSuggestion, Mode, ProbeScope};
99
use crate::rustc_middle::ty::Article;
1010
use core::cmp::min;
1111
use core::iter;
12+
use hir::def_id::LocalDefId;
1213
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX};
1314
use rustc_data_structures::packed::Pu128;
1415
use rustc_errors::{Applicability, Diag, MultiSpan};
@@ -796,7 +797,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
796797
expected: Ty<'tcx>,
797798
found: Ty<'tcx>,
798799
can_suggest: bool,
799-
fn_id: hir::HirId,
800+
fn_id: LocalDefId,
800801
) -> bool {
801802
let found =
802803
self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(found));
@@ -923,7 +924,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
923924
err: &mut Diag<'_>,
924925
expected: Ty<'tcx>,
925926
found: Ty<'tcx>,
926-
fn_id: hir::HirId,
927+
fn_id: LocalDefId,
927928
) {
928929
// Only apply the suggestion if:
929930
// - the return type is a generic parameter
@@ -937,7 +938,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
937938

938939
let ty::Param(expected_ty_as_param) = expected.kind() else { return };
939940

940-
let fn_node = self.tcx.hir_node(fn_id);
941+
let fn_node = self.tcx.hir_node_by_def_id(fn_id);
941942

942943
let hir::Node::Item(hir::Item {
943944
kind:
@@ -1031,7 +1032,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10311032
expected: Ty<'tcx>,
10321033
found: Ty<'tcx>,
10331034
id: hir::HirId,
1034-
fn_id: hir::HirId,
1035+
fn_id: LocalDefId,
10351036
) {
10361037
if !expected.is_unit() {
10371038
return;
@@ -1083,11 +1084,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10831084
let can_return = match fn_decl.output {
10841085
hir::FnRetTy::Return(ty) => {
10851086
let ty = self.lowerer().lower_ty(ty);
1086-
let bound_vars = self.tcx.late_bound_vars(fn_id);
1087+
let bound_vars = self.tcx.late_bound_vars(self.tcx.local_def_id_to_hir_id(fn_id));
10871088
let ty = self
10881089
.tcx
10891090
.instantiate_bound_regions_with_erased(Binder::bind_with_vars(ty, bound_vars));
1090-
let ty = match self.tcx.asyncness(fn_id.owner) {
1091+
let ty = match self.tcx.asyncness(fn_id) {
10911092
ty::Asyncness::Yes => self.get_impl_future_output_ty(ty).unwrap_or_else(|| {
10921093
span_bug!(
10931094
fn_decl.output.span(),
@@ -1108,8 +1109,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11081109
_ => false,
11091110
};
11101111
if can_return
1111-
&& let Some(owner_node) = self.tcx.hir_node(fn_id).as_owner()
1112-
&& let Some(span) = expr.span.find_ancestor_inside(*owner_node.span())
1112+
&& let Some(span) = expr.span.find_ancestor_inside(
1113+
self.tcx.hir().span_with_body(self.tcx.local_def_id_to_hir_id(fn_id)),
1114+
)
11131115
{
11141116
err.multipart_suggestion(
11151117
"you might have meant to return this value",

compiler/rustc_resolve/src/imports.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1391,13 +1391,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13911391
let mut redundant_spans: Vec<_> = redundant_span.present_items().collect();
13921392
redundant_spans.sort();
13931393
redundant_spans.dedup();
1394+
/* FIXME(unused_imports): Add this back as a new lint
13941395
self.lint_buffer.buffer_lint_with_diagnostic(
13951396
UNUSED_IMPORTS,
13961397
id,
13971398
import.span,
13981399
format!("the item `{source}` is imported redundantly"),
13991400
BuiltinLintDiag::RedundantImport(redundant_spans, source),
14001401
);
1402+
*/
14011403
return true;
14021404
}
14031405

compiler/rustc_resolve/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ enum ImplTraitContext {
177177

178178
/// Used for tracking import use types which will be used for redundant import checking.
179179
/// ### Used::Scope Example
180-
/// ```rust,compile_fail
180+
/// ```rust,ignore (redundant_imports)
181181
/// #![deny(unused_imports)]
182182
/// use std::mem::drop;
183183
/// fn main() {

library/core/src/io/borrowed_buf.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,10 @@ impl<'a> BorrowedCursor<'a> {
249249
/// Panics if there are less than `n` bytes initialized.
250250
#[inline]
251251
pub fn advance(&mut self, n: usize) -> &mut Self {
252-
assert!(self.buf.init >= self.buf.filled + n);
252+
let filled = self.buf.filled.strict_add(n);
253+
assert!(filled <= self.buf.init);
253254

254-
self.buf.filled += n;
255+
self.buf.filled = filled;
255256
self
256257
}
257258

0 commit comments

Comments
 (0)