Skip to content

Commit 748cb1f

Browse files
committed
Auto merge of #99493 - Dylan-DPC:rollup-lli4gcx, r=Dylan-DPC
Rollup of 5 pull requests Successful merges: - #98784 (Suggest returning local on "expected `ty`, found `()`" due to expr-less block) - #98916 (Windows: Use `FindFirstFileW` for getting the metadata of locked system files) - #99433 (Erase regions before comparing signatures of foreign fns.) - #99452 (int_macros was only using to_xe_bytes_doc and not from_xe_bytes_doc) - #99481 (Add regression test for #71547) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 03d488b + d7a65a5 commit 748cb1f

File tree

16 files changed

+462
-84
lines changed

16 files changed

+462
-84
lines changed

Diff for: compiler/rustc_lint/src/builtin.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2858,9 +2858,10 @@ impl ClashingExternDeclarations {
28582858
let a_poly_sig = a.fn_sig(tcx);
28592859
let b_poly_sig = b.fn_sig(tcx);
28602860

2861-
// As we don't compare regions, skip_binder is fine.
2862-
let a_sig = a_poly_sig.skip_binder();
2863-
let b_sig = b_poly_sig.skip_binder();
2861+
// We don't compare regions, but leaving bound regions around ICEs, so
2862+
// we erase them.
2863+
let a_sig = tcx.erase_late_bound_regions(a_poly_sig);
2864+
let b_sig = tcx.erase_late_bound_regions(b_poly_sig);
28642865

28652866
(a_sig.abi, a_sig.unsafety, a_sig.c_variadic)
28662867
== (b_sig.abi, b_sig.unsafety, b_sig.c_variadic)

Diff for: compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+4-40
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ use rustc_middle::ty::{self, DefIdTree, IsSuggestable, Ty};
3131
use rustc_session::Session;
3232
use rustc_span::symbol::Ident;
3333
use rustc_span::{self, Span};
34-
use rustc_trait_selection::traits::{
35-
self, ObligationCauseCode, SelectionContext, StatementAsExpression,
36-
};
34+
use rustc_trait_selection::traits::{self, ObligationCauseCode, SelectionContext};
3735

3836
use std::iter;
3937
use std::slice;
@@ -1410,7 +1408,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14101408
&self.misc(sp),
14111409
&mut |err| {
14121410
if let Some(expected_ty) = expected.only_has_type(self) {
1413-
self.consider_hint_about_removing_semicolon(blk, expected_ty, err);
1411+
if !self.consider_removing_semicolon(blk, expected_ty, err) {
1412+
self.consider_returning_binding(blk, expected_ty, err);
1413+
}
14141414
if expected_ty == self.tcx.types.bool {
14151415
// If this is caused by a missing `let` in a `while let`,
14161416
// silence this redundant error, as we already emit E0070.
@@ -1478,42 +1478,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14781478
ty
14791479
}
14801480

1481-
/// A common error is to add an extra semicolon:
1482-
///
1483-
/// ```compile_fail,E0308
1484-
/// fn foo() -> usize {
1485-
/// 22;
1486-
/// }
1487-
/// ```
1488-
///
1489-
/// This routine checks if the final statement in a block is an
1490-
/// expression with an explicit semicolon whose type is compatible
1491-
/// with `expected_ty`. If so, it suggests removing the semicolon.
1492-
fn consider_hint_about_removing_semicolon(
1493-
&self,
1494-
blk: &'tcx hir::Block<'tcx>,
1495-
expected_ty: Ty<'tcx>,
1496-
err: &mut Diagnostic,
1497-
) {
1498-
if let Some((span_semi, boxed)) = self.could_remove_semicolon(blk, expected_ty) {
1499-
if let StatementAsExpression::NeedsBoxing = boxed {
1500-
err.span_suggestion_verbose(
1501-
span_semi,
1502-
"consider removing this semicolon and boxing the expression",
1503-
"",
1504-
Applicability::HasPlaceholders,
1505-
);
1506-
} else {
1507-
err.span_suggestion_short(
1508-
span_semi,
1509-
"remove this semicolon",
1510-
"",
1511-
Applicability::MachineApplicable,
1512-
);
1513-
}
1514-
}
1515-
}
1516-
15171481
fn parent_item_span(&self, id: hir::HirId) -> Option<Span> {
15181482
let node = self.tcx.hir().get_by_def_id(self.tcx.hir().get_parent_item(id));
15191483
match node {

Diff for: compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs

+156-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::astconv::AstConv;
33
use crate::errors::{AddReturnTypeSuggestion, ExpectedReturnTypeLabel};
44

55
use rustc_ast::util::parser::ExprPrecedence;
6+
use rustc_data_structures::stable_set::FxHashSet;
67
use rustc_errors::{Applicability, Diagnostic, MultiSpan};
78
use rustc_hir as hir;
89
use rustc_hir::def::{CtorOf, DefKind};
@@ -11,12 +12,12 @@ use rustc_hir::{
1112
Expr, ExprKind, GenericBound, Node, Path, QPath, Stmt, StmtKind, TyKind, WherePredicate,
1213
};
1314
use rustc_infer::infer::{self, TyCtxtInferExt};
14-
use rustc_infer::traits;
15+
use rustc_infer::traits::{self, StatementAsExpression};
1516
use rustc_middle::lint::in_external_macro;
16-
use rustc_middle::ty::{self, Binder, IsSuggestable, Subst, ToPredicate, Ty};
17+
use rustc_middle::ty::{self, Binder, IsSuggestable, Subst, ToPredicate, Ty, TypeVisitable};
1718
use rustc_span::symbol::sym;
1819
use rustc_span::Span;
19-
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
20+
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
2021

2122
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2223
pub(in super::super) fn suggest_semicolon_at_end(&self, span: Span, err: &mut Diagnostic) {
@@ -864,4 +865,156 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
864865
);
865866
}
866867
}
868+
869+
/// A common error is to add an extra semicolon:
870+
///
871+
/// ```compile_fail,E0308
872+
/// fn foo() -> usize {
873+
/// 22;
874+
/// }
875+
/// ```
876+
///
877+
/// This routine checks if the final statement in a block is an
878+
/// expression with an explicit semicolon whose type is compatible
879+
/// with `expected_ty`. If so, it suggests removing the semicolon.
880+
pub(crate) fn consider_removing_semicolon(
881+
&self,
882+
blk: &'tcx hir::Block<'tcx>,
883+
expected_ty: Ty<'tcx>,
884+
err: &mut Diagnostic,
885+
) -> bool {
886+
if let Some((span_semi, boxed)) = self.could_remove_semicolon(blk, expected_ty) {
887+
if let StatementAsExpression::NeedsBoxing = boxed {
888+
err.span_suggestion_verbose(
889+
span_semi,
890+
"consider removing this semicolon and boxing the expression",
891+
"",
892+
Applicability::HasPlaceholders,
893+
);
894+
} else {
895+
err.span_suggestion_short(
896+
span_semi,
897+
"remove this semicolon",
898+
"",
899+
Applicability::MachineApplicable,
900+
);
901+
}
902+
true
903+
} else {
904+
false
905+
}
906+
}
907+
908+
pub(crate) fn consider_returning_binding(
909+
&self,
910+
blk: &'tcx hir::Block<'tcx>,
911+
expected_ty: Ty<'tcx>,
912+
err: &mut Diagnostic,
913+
) {
914+
let mut shadowed = FxHashSet::default();
915+
let mut candidate_idents = vec![];
916+
let mut find_compatible_candidates = |pat: &hir::Pat<'_>| {
917+
if let hir::PatKind::Binding(_, hir_id, ident, _) = &pat.kind
918+
&& let Some(pat_ty) = self.typeck_results.borrow().node_type_opt(*hir_id)
919+
{
920+
let pat_ty = self.resolve_vars_if_possible(pat_ty);
921+
if self.can_coerce(pat_ty, expected_ty)
922+
&& !(pat_ty, expected_ty).references_error()
923+
&& shadowed.insert(ident.name)
924+
{
925+
candidate_idents.push((*ident, pat_ty));
926+
}
927+
}
928+
true
929+
};
930+
931+
let hir = self.tcx.hir();
932+
for stmt in blk.stmts.iter().rev() {
933+
let StmtKind::Local(local) = &stmt.kind else { continue; };
934+
local.pat.walk(&mut find_compatible_candidates);
935+
}
936+
match hir.find(hir.get_parent_node(blk.hir_id)) {
937+
Some(hir::Node::Expr(hir::Expr { hir_id, .. })) => {
938+
match hir.find(hir.get_parent_node(*hir_id)) {
939+
Some(hir::Node::Arm(hir::Arm { pat, .. })) => {
940+
pat.walk(&mut find_compatible_candidates);
941+
}
942+
Some(
943+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(_, _, body), .. })
944+
| hir::Node::ImplItem(hir::ImplItem {
945+
kind: hir::ImplItemKind::Fn(_, body),
946+
..
947+
})
948+
| hir::Node::TraitItem(hir::TraitItem {
949+
kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(body)),
950+
..
951+
})
952+
| hir::Node::Expr(hir::Expr {
953+
kind: hir::ExprKind::Closure(hir::Closure { body, .. }),
954+
..
955+
}),
956+
) => {
957+
for param in hir.body(*body).params {
958+
param.pat.walk(&mut find_compatible_candidates);
959+
}
960+
}
961+
Some(hir::Node::Expr(hir::Expr {
962+
kind:
963+
hir::ExprKind::If(
964+
hir::Expr { kind: hir::ExprKind::Let(let_), .. },
965+
then_block,
966+
_,
967+
),
968+
..
969+
})) if then_block.hir_id == *hir_id => {
970+
let_.pat.walk(&mut find_compatible_candidates);
971+
}
972+
_ => {}
973+
}
974+
}
975+
_ => {}
976+
}
977+
978+
match &candidate_idents[..] {
979+
[(ident, _ty)] => {
980+
let sm = self.tcx.sess.source_map();
981+
if let Some(stmt) = blk.stmts.last() {
982+
let stmt_span = sm.stmt_span(stmt.span, blk.span);
983+
let sugg = if sm.is_multiline(blk.span)
984+
&& let Some(spacing) = sm.indentation_before(stmt_span)
985+
{
986+
format!("\n{spacing}{ident}")
987+
} else {
988+
format!(" {ident}")
989+
};
990+
err.span_suggestion_verbose(
991+
stmt_span.shrink_to_hi(),
992+
format!("consider returning the local binding `{ident}`"),
993+
sugg,
994+
Applicability::MachineApplicable,
995+
);
996+
} else {
997+
let sugg = if sm.is_multiline(blk.span)
998+
&& let Some(spacing) = sm.indentation_before(blk.span.shrink_to_lo())
999+
{
1000+
format!("\n{spacing} {ident}\n{spacing}")
1001+
} else {
1002+
format!(" {ident} ")
1003+
};
1004+
let left_span = sm.span_through_char(blk.span, '{').shrink_to_hi();
1005+
err.span_suggestion_verbose(
1006+
sm.span_extend_while(left_span, |c| c.is_whitespace()).unwrap_or(left_span),
1007+
format!("consider returning the local binding `{ident}`"),
1008+
sugg,
1009+
Applicability::MachineApplicable,
1010+
);
1011+
}
1012+
}
1013+
values if (1..3).contains(&values.len()) => {
1014+
let spans = values.iter().map(|(ident, _)| ident.span).collect::<Vec<_>>();
1015+
err.span_note(spans, "consider returning one of these bindings");
1016+
}
1017+
_ => {}
1018+
}
1019+
}
8671020
}

Diff for: library/core/src/num/int_macros.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2612,7 +2612,7 @@ macro_rules! int_impl {
26122612
/// Create an integer value from its representation as a byte array in
26132613
/// big endian.
26142614
///
2615-
#[doc = $to_xe_bytes_doc]
2615+
#[doc = $from_xe_bytes_doc]
26162616
///
26172617
/// # Examples
26182618
///
@@ -2641,7 +2641,7 @@ macro_rules! int_impl {
26412641
/// Create an integer value from its representation as a byte array in
26422642
/// little endian.
26432643
///
2644-
#[doc = $to_xe_bytes_doc]
2644+
#[doc = $from_xe_bytes_doc]
26452645
///
26462646
/// # Examples
26472647
///
@@ -2677,7 +2677,7 @@ macro_rules! int_impl {
26772677
/// [`from_be_bytes`]: Self::from_be_bytes
26782678
/// [`from_le_bytes`]: Self::from_le_bytes
26792679
///
2680-
#[doc = $to_xe_bytes_doc]
2680+
#[doc = $from_xe_bytes_doc]
26812681
///
26822682
/// # Examples
26832683
///

Diff for: library/std/src/fs/tests.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1534,3 +1534,20 @@ fn read_large_dir() {
15341534
entry.unwrap();
15351535
}
15361536
}
1537+
1538+
/// Test the fallback for getting the metadata of files like hiberfil.sys that
1539+
/// Windows holds a special lock on, preventing normal means of querying
1540+
/// metadata. See #96980.
1541+
///
1542+
/// Note this fails in CI because `hiberfil.sys` does not actually exist there.
1543+
/// Therefore it's marked as ignored.
1544+
#[test]
1545+
#[ignore]
1546+
#[cfg(windows)]
1547+
fn hiberfil_sys() {
1548+
let hiberfil = Path::new(r"C:\hiberfil.sys");
1549+
assert_eq!(true, hiberfil.try_exists().unwrap());
1550+
fs::symlink_metadata(hiberfil).unwrap();
1551+
fs::metadata(hiberfil).unwrap();
1552+
assert_eq!(true, hiberfil.exists());
1553+
}

0 commit comments

Comments
 (0)