Skip to content

Commit 0cc4f4f

Browse files
committed
Auto merge of #136248 - matthiaskrgr:rollup-leaxgfd, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #133382 (Suggest considering casting fn item as fn pointer in more cases) - #136092 (Test pipes also when not running on Windows and Linux simultaneously) - #136190 (Remove duplicated code in RISC-V asm bad-reg test) - #136192 (ci: remove unused windows runner) - #136205 (Properly check that array length is valid type during built-in unsizing in index) - #136211 (Update mdbook to 0.4.44) - #136212 (Tweak `&mut self` suggestion span) - #136214 (Make crate AST mutation accessible for driver callback) r? `@ghost` `@rustbot` modify labels: rollup
2 parents a1d7676 + c941b1c commit 0cc4f4f

Some content is hidden

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

44 files changed

+321
-206
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -1140,10 +1140,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11401140

11411141
let amp_mut_sugg = match *local_decl.local_info() {
11421142
LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => {
1143-
let suggestion = suggest_ampmut_self(self.infcx.tcx, decl_span);
1144-
let additional =
1145-
local_trait.map(|span| (span, suggest_ampmut_self(self.infcx.tcx, span)));
1146-
Some(AmpMutSugg { has_sugg: true, span: decl_span, suggestion, additional })
1143+
let (span, suggestion) = suggest_ampmut_self(self.infcx.tcx, decl_span);
1144+
let additional = local_trait.map(|span| suggest_ampmut_self(self.infcx.tcx, span));
1145+
Some(AmpMutSugg { has_sugg: true, span, suggestion, additional })
11471146
}
11481147

11491148
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
@@ -1202,10 +1201,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
12021201
opt_ty_info: None,
12031202
..
12041203
})) => {
1205-
let sugg = suggest_ampmut_self(self.infcx.tcx, decl_span);
1204+
let (span, sugg) =
1205+
suggest_ampmut_self(self.infcx.tcx, decl_span);
12061206
Some(AmpMutSugg {
12071207
has_sugg: true,
1208-
span: decl_span,
1208+
span,
12091209
suggestion: sugg,
12101210
additional: None,
12111211
})
@@ -1461,17 +1461,12 @@ fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<Symb
14611461
}
14621462
}
14631463

1464-
fn suggest_ampmut_self<'tcx>(tcx: TyCtxt<'tcx>, span: Span) -> String {
1464+
fn suggest_ampmut_self(tcx: TyCtxt<'_>, span: Span) -> (Span, String) {
14651465
match tcx.sess.source_map().span_to_snippet(span) {
1466-
Ok(snippet) => {
1467-
let lt_pos = snippet.find('\'');
1468-
if let Some(lt_pos) = lt_pos {
1469-
format!("&{}mut self", &snippet[lt_pos..snippet.len() - 4])
1470-
} else {
1471-
"&mut self".to_string()
1472-
}
1466+
Ok(snippet) if snippet.ends_with("self") => {
1467+
(span.with_hi(span.hi() - BytePos(4)).shrink_to_hi(), "mut ".to_string())
14731468
}
1474-
_ => "&mut self".to_string(),
1469+
_ => (span, "&mut self".to_string()),
14751470
}
14761471
}
14771472

compiler/rustc_driver_impl/src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub trait Callbacks {
160160
fn after_crate_root_parsing(
161161
&mut self,
162162
_compiler: &interface::Compiler,
163-
_queries: &ast::Crate,
163+
_krate: &mut ast::Crate,
164164
) -> Compilation {
165165
Compilation::Continue
166166
}
@@ -311,7 +311,7 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
311311

312312
// Parse the crate root source code (doesn't parse submodules yet)
313313
// Everything else is parsed during macro expansion.
314-
let krate = passes::parse(sess);
314+
let mut krate = passes::parse(sess);
315315

316316
// If pretty printing is requested: Figure out the representation, print it and exit
317317
if let Some(pp_mode) = sess.opts.pretty {
@@ -328,7 +328,7 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
328328
return early_exit();
329329
}
330330

331-
if callbacks.after_crate_root_parsing(compiler, &krate) == Compilation::Stop {
331+
if callbacks.after_crate_root_parsing(compiler, &mut krate) == Compilation::Stop {
332332
return early_exit();
333333
}
334334

compiler/rustc_hir_typeck/src/place_op.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use rustc_errors::Applicability;
22
use rustc_hir_analysis::autoderef::Autoderef;
33
use rustc_infer::infer::InferOk;
4+
use rustc_infer::traits::{Obligation, ObligationCauseCode};
45
use rustc_middle::span_bug;
56
use rustc_middle::ty::adjustment::{
67
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, OverloadedDeref,
@@ -136,8 +137,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
136137
let mut self_ty = adjusted_ty;
137138
if unsize {
138139
// We only unsize arrays here.
139-
if let ty::Array(element_ty, _) = adjusted_ty.kind() {
140-
self_ty = Ty::new_slice(self.tcx, *element_ty);
140+
if let ty::Array(element_ty, ct) = *adjusted_ty.kind() {
141+
self.register_predicate(Obligation::new(
142+
self.tcx,
143+
self.cause(base_expr.span, ObligationCauseCode::ArrayLen(adjusted_ty)),
144+
self.param_env,
145+
ty::ClauseKind::ConstArgHasType(ct, self.tcx.types.usize),
146+
));
147+
self_ty = Ty::new_slice(self.tcx, element_ty);
141148
} else {
142149
continue;
143150
}

compiler/rustc_middle/src/traits/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ pub enum ObligationCauseCode<'tcx> {
194194
/// A slice or array is WF only if `T: Sized`.
195195
SliceOrArrayElem,
196196

197+
/// An array `[T; N]` can only be indexed (and is only well-formed if) `N` has type usize.
198+
ArrayLen(Ty<'tcx>),
199+
197200
/// A tuple is WF only if its middle elements are `Sized`.
198201
TupleElem,
199202

compiler/rustc_trait_selection/messages.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ trait_selection_explicit_lifetime_required_with_param_type = explicit lifetime r
165165
166166
trait_selection_fn_consider_casting = consider casting the fn item to a fn pointer: `{$casting}`
167167
168+
trait_selection_fn_consider_casting_both = consider casting both fn items to fn pointers using `as {$sig}`
169+
168170
trait_selection_fn_uniq_types = different fn items have unique types, even if their signatures are the same
169171
trait_selection_fps_cast = consider casting to a fn pointer
170172
trait_selection_fps_cast_both = consider casting both fn items to fn pointers using `as {$expected_sig}`

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18441844
self.suggest_tuple_pattern(cause, &exp_found, diag);
18451845
self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
18461846
self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
1847-
self.suggest_function_pointers(cause, span, &exp_found, diag);
1847+
self.suggest_function_pointers(cause, span, &exp_found, terr, diag);
18481848
self.suggest_turning_stmt_into_expr(cause, &exp_found, diag);
18491849
}
18501850
}

compiler/rustc_trait_selection/src/error_reporting/infer/suggest.rs

+34-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_middle::traits::{
1212
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
1313
StatementAsExpression,
1414
};
15+
use rustc_middle::ty::error::TypeError;
1516
use rustc_middle::ty::print::with_no_trimmed_paths;
1617
use rustc_middle::ty::{self as ty, GenericArgKind, IsSuggestable, Ty, TypeVisitableExt};
1718
use rustc_span::{Span, sym};
@@ -20,7 +21,7 @@ use tracing::debug;
2021
use crate::error_reporting::TypeErrCtxt;
2122
use crate::error_reporting::infer::hir::Path;
2223
use crate::errors::{
23-
ConsiderAddingAwait, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
24+
ConsiderAddingAwait, FnConsiderCasting, FnConsiderCastingBoth, FnItemsAreDistinct, FnUniqTypes,
2425
FunctionPointerSuggestion, SuggestAccessingField, SuggestRemoveSemiOrReturnBinding,
2526
SuggestTuplePatternMany, SuggestTuplePatternOne, TypeErrorAdditionalDiags,
2627
};
@@ -381,14 +382,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
381382
}
382383
}
383384

384-
pub(super) fn suggest_function_pointers(
385+
pub fn suggest_function_pointers_impl(
385386
&self,
386-
cause: &ObligationCause<'tcx>,
387-
span: Span,
387+
span: Option<Span>,
388388
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
389389
diag: &mut Diag<'_>,
390390
) {
391-
debug!("suggest_function_pointers(cause={:?}, exp_found={:?})", cause, exp_found);
392391
let ty::error::ExpectedFound { expected, found } = exp_found;
393392
let expected_inner = expected.peel_refs();
394393
let found_inner = found.peel_refs();
@@ -411,6 +410,13 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
411410
return;
412411
}
413412

413+
let Some(span) = span else {
414+
let casting = format!("{fn_name} as {sig}");
415+
diag.subdiagnostic(FnItemsAreDistinct);
416+
diag.subdiagnostic(FnConsiderCasting { casting });
417+
return;
418+
};
419+
414420
let sugg = match (expected.is_ref(), found.is_ref()) {
415421
(true, false) => FunctionPointerSuggestion::UseRef { span, fn_name },
416422
(false, true) => FunctionPointerSuggestion::RemoveRef { span, fn_name },
@@ -445,6 +451,12 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
445451
}
446452

447453
let fn_name = self.tcx.def_path_str_with_args(*did2, args2);
454+
455+
let Some(span) = span else {
456+
diag.subdiagnostic(FnConsiderCastingBoth { sig: *expected_sig });
457+
return;
458+
};
459+
448460
let sug = if found.is_ref() {
449461
FunctionPointerSuggestion::CastBothRef {
450462
span,
@@ -488,6 +500,23 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
488500
};
489501
}
490502

503+
pub(super) fn suggest_function_pointers(
504+
&self,
505+
cause: &ObligationCause<'tcx>,
506+
span: Span,
507+
exp_found: &ty::error::ExpectedFound<Ty<'tcx>>,
508+
terr: TypeError<'tcx>,
509+
diag: &mut Diag<'_>,
510+
) {
511+
debug!("suggest_function_pointers(cause={:?}, exp_found={:?})", cause, exp_found);
512+
513+
if exp_found.expected.peel_refs().is_fn() && exp_found.found.peel_refs().is_fn() {
514+
self.suggest_function_pointers_impl(Some(span), exp_found, diag);
515+
} else if let TypeError::Sorts(exp_found) = terr {
516+
self.suggest_function_pointers_impl(None, &exp_found, diag);
517+
}
518+
}
519+
491520
pub fn should_suggest_as_ref_kind(
492521
&self,
493522
expected: Ty<'tcx>,

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
19691969
StringPart::highlighted(exp_found.found.to_string()),
19701970
StringPart::normal("`"),
19711971
]);
1972+
self.suggest_function_pointers_impl(None, &exp_found, err);
19721973
}
19731974

19741975
true

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
27692769
ObligationCauseCode::SliceOrArrayElem => {
27702770
err.note("slice and array elements must have `Sized` type");
27712771
}
2772+
ObligationCauseCode::ArrayLen(array_ty) => {
2773+
err.note(format!("the length of array `{array_ty}` must be type `usize`"));
2774+
}
27722775
ObligationCauseCode::TupleElem => {
27732776
err.note("only the last element of a tuple may have a dynamically sized type");
27742777
}

compiler/rustc_trait_selection/src/errors.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1496,6 +1496,12 @@ pub struct FnConsiderCasting {
14961496
pub casting: String,
14971497
}
14981498

1499+
#[derive(Subdiagnostic)]
1500+
#[help(trait_selection_fn_consider_casting_both)]
1501+
pub struct FnConsiderCastingBoth<'a> {
1502+
pub sig: Binder<'a, FnSig<'a>>,
1503+
}
1504+
14991505
#[derive(Subdiagnostic)]
15001506
pub enum SuggestAccessingField<'a> {
15011507
#[suggestion(

compiler/rustc_trait_selection/src/traits/wf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
689689
self.require_sized(subty, ObligationCauseCode::SliceOrArrayElem);
690690
// Note that the len being WF is implicitly checked while visiting.
691691
// Here we just check that it's of type usize.
692-
let cause = self.cause(ObligationCauseCode::Misc);
692+
let cause = self.cause(ObligationCauseCode::ArrayLen(t));
693693
self.out.push(traits::Obligation::with_depth(
694694
tcx,
695695
cause,

library/std/src/io/pipe/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::io::{Read, Write, pipe};
22

33
#[test]
4-
#[cfg(all(windows, unix, not(miri)))]
4+
#[cfg(all(any(unix, windows), not(miri)))]
55
fn pipe_creation_clone_and_rw() {
66
let (rx, tx) = pipe().unwrap();
77

src/ci/github-actions/jobs.yml

-4
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ runners:
4343
os: windows-2022-8core-32gb
4444
<<: *base-job
4545

46-
- &job-windows-16c
47-
os: windows-2022-16core-64gb
48-
<<: *base-job
49-
5046
- &job-aarch64-linux
5147
# Free some disk space to avoid running out of space during the build.
5248
free_disk: true

src/doc/rustc-dev-guide/examples/rustc-driver-example.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl rustc_driver::Callbacks for MyCallbacks {
5858
fn after_crate_root_parsing(
5959
&mut self,
6060
_compiler: &Compiler,
61-
krate: &rustc_ast::Crate,
61+
krate: &mut rustc_ast::Crate,
6262
) -> Compilation {
6363
for item in &krate.items {
6464
println!("{}", item_to_string(&item));

src/doc/rustc-dev-guide/examples/rustc-driver-interacting-with-the-ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl rustc_driver::Callbacks for MyCallbacks {
5858
fn after_crate_root_parsing(
5959
&mut self,
6060
_compiler: &Compiler,
61-
krate: &rustc_ast::Crate,
61+
krate: &mut rustc_ast::Crate,
6262
) -> Compilation {
6363
for item in &krate.items {
6464
println!("{}", item_to_string(&item));

0 commit comments

Comments
 (0)