Skip to content

Commit 502ce82

Browse files
committed
Auto merge of #121086 - GuillaumeGomez:rollup-y82fs9y, r=GuillaumeGomez
Rollup of 6 pull requests Successful merges: - #120893 (Move some tests) - #120966 (Remove importing suggestions when there is a shadowed typo candidate) - #121035 (Format `async` trait bounds in rustfmt) - #121075 (Fix false positive with if let and ranges) - #121083 (Extend documentation for `Ty::to_opt_closure_kind` method) - #121084 (Make sure `tcx.create_def` also depends on the forever red node, instead of just `tcx.at(span).create_def`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 340bb19 + 2062325 commit 502ce82

24 files changed

+125
-43
lines changed

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,11 @@ trait UnusedDelimLint {
651651

652652
fn is_expr_delims_necessary(
653653
inner: &ast::Expr,
654+
ctx: UnusedDelimsCtx,
654655
followed_by_block: bool,
655-
followed_by_else: bool,
656656
) -> bool {
657+
let followed_by_else = ctx == UnusedDelimsCtx::AssignedValueLetElse;
658+
657659
if followed_by_else {
658660
match inner.kind {
659661
ast::ExprKind::Binary(op, ..) if op.node.is_lazy() => return true,
@@ -662,6 +664,13 @@ trait UnusedDelimLint {
662664
}
663665
}
664666

667+
// Check it's range in LetScrutineeExpr
668+
if let ast::ExprKind::Range(..) = inner.kind
669+
&& matches!(ctx, UnusedDelimsCtx::LetScrutineeExpr)
670+
{
671+
return true;
672+
}
673+
665674
// Check if LHS needs parens to prevent false-positives in cases like `fn x() -> u8 { ({ 0 } + 1) }`.
666675
{
667676
let mut innermost = inner;
@@ -1007,8 +1016,7 @@ impl UnusedDelimLint for UnusedParens {
10071016
) {
10081017
match value.kind {
10091018
ast::ExprKind::Paren(ref inner) => {
1010-
let followed_by_else = ctx == UnusedDelimsCtx::AssignedValueLetElse;
1011-
if !Self::is_expr_delims_necessary(inner, followed_by_block, followed_by_else)
1019+
if !Self::is_expr_delims_necessary(inner, ctx, followed_by_block)
10121020
&& value.attrs.is_empty()
10131021
&& !value.span.from_expansion()
10141022
&& (ctx != UnusedDelimsCtx::LetScrutineeExpr
@@ -1334,7 +1342,7 @@ impl UnusedDelimLint for UnusedBraces {
13341342
// FIXME(const_generics): handle paths when #67075 is fixed.
13351343
if let [stmt] = inner.stmts.as_slice() {
13361344
if let ast::StmtKind::Expr(ref expr) = stmt.kind {
1337-
if !Self::is_expr_delims_necessary(expr, followed_by_block, false)
1345+
if !Self::is_expr_delims_necessary(expr, ctx, followed_by_block)
13381346
&& (ctx != UnusedDelimsCtx::AnonConst
13391347
|| (matches!(expr.kind, ast::ExprKind::Lit(_))
13401348
&& !expr.span.from_expansion()))

Diff for: compiler/rustc_middle/src/ty/context.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1053,12 +1053,6 @@ impl<'tcx> TyCtxtAt<'tcx> {
10531053
name: Symbol,
10541054
def_kind: DefKind,
10551055
) -> TyCtxtFeed<'tcx, LocalDefId> {
1056-
// This function modifies `self.definitions` using a side-effect.
1057-
// We need to ensure that these side effects are re-run by the incr. comp. engine.
1058-
// Depending on the forever-red node will tell the graph that the calling query
1059-
// needs to be re-evaluated.
1060-
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
1061-
10621056
// The following call has the side effect of modifying the tables inside `definitions`.
10631057
// These very tables are relied on by the incr. comp. engine to decode DepNodes and to
10641058
// decode the on-disk cache.
@@ -1087,6 +1081,12 @@ impl<'tcx> TyCtxt<'tcx> {
10871081
let data = def_kind.def_path_data(name);
10881082
let def_id = self.untracked.definitions.write().create_def(parent, data);
10891083

1084+
// This function modifies `self.definitions` using a side-effect.
1085+
// We need to ensure that these side effects are re-run by the incr. comp. engine.
1086+
// Depending on the forever-red node will tell the graph that the calling query
1087+
// needs to be re-evaluated.
1088+
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
1089+
10901090
let feed = self.feed_local_def_id(def_id);
10911091
feed.def_kind(def_kind);
10921092
// Unique types created for closures participate in type privacy checking.

Diff for: compiler/rustc_middle/src/ty/sty.rs

+14
Original file line numberDiff line numberDiff line change
@@ -2373,6 +2373,20 @@ impl<'tcx> Ty<'tcx> {
23732373
/// to represent the closure kind, because it has not yet been
23742374
/// inferred. Once upvar inference (in `rustc_hir_analysis/src/check/upvar.rs`)
23752375
/// is complete, that type variable will be unified.
2376+
///
2377+
/// To be noted that you can use [`ClosureArgs::kind()`] or [`CoroutineClosureArgs::kind()`]
2378+
/// to get the same information, which you can get by calling [`GenericArgs::as_closure()`]
2379+
/// or [`GenericArgs::as_coroutine_closure()`], depending on the type of the closure.
2380+
///
2381+
/// Otherwise, this method can be used as follows:
2382+
///
2383+
/// ```rust,ignore (snippet of compiler code)
2384+
/// let TyKind::Closure(def_id, [closure_fn_kind_ty, ..]) = closure_ty.kind()
2385+
/// && let Some(closure_kind) = closure_fn_kind_ty.expect_ty().to_opt_closure_kind()
2386+
/// {
2387+
/// // your code
2388+
/// }
2389+
/// ```
23762390
pub fn to_opt_closure_kind(self) -> Option<ty::ClosureKind> {
23772391
match self.kind() {
23782392
Int(int_ty) => match int_ty {

Diff for: compiler/rustc_resolve/src/late/diagnostics.rs

+39-23
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
460460
return (err, Vec::new());
461461
}
462462

463-
let (found, candidates) = self.try_lookup_name_relaxed(
463+
let (found, mut candidates) = self.try_lookup_name_relaxed(
464464
&mut err,
465465
source,
466466
path,
@@ -473,10 +473,12 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
473473
return (err, candidates);
474474
}
475475

476-
let mut fallback = self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
476+
if self.suggest_shadowed(&mut err, source, path, following_seg, span) {
477+
// if there is already a shadowed name, don'suggest candidates for importing
478+
candidates.clear();
479+
}
477480

478-
// if we have suggested using pattern matching, then don't add needless suggestions
479-
// for typos.
481+
let mut fallback = self.suggest_trait_and_bounds(&mut err, source, res, span, &base_error);
480482
fallback |= self.suggest_typo(&mut err, source, path, following_seg, span, &base_error);
481483

482484
if fallback {
@@ -872,25 +874,6 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
872874
let ident_span = path.last().map_or(span, |ident| ident.ident.span);
873875
let typo_sugg =
874876
self.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected);
875-
let is_in_same_file = &|sp1, sp2| {
876-
let source_map = self.r.tcx.sess.source_map();
877-
let file1 = source_map.span_to_filename(sp1);
878-
let file2 = source_map.span_to_filename(sp2);
879-
file1 == file2
880-
};
881-
// print 'you might have meant' if the candidate is (1) is a shadowed name with
882-
// accessible definition and (2) either defined in the same crate as the typo
883-
// (could be in a different file) or introduced in the same file as the typo
884-
// (could belong to a different crate)
885-
if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg
886-
&& res.opt_def_id().is_some_and(|id| id.is_local() || is_in_same_file(span, sugg_span))
887-
{
888-
err.span_label(
889-
sugg_span,
890-
format!("you might have meant to refer to this {}", res.descr()),
891-
);
892-
return true;
893-
}
894877
let mut fallback = false;
895878
let typo_sugg = typo_sugg.to_opt_suggestion();
896879
if !self.r.add_typo_suggestion(err, typo_sugg, ident_span) {
@@ -918,6 +901,39 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
918901
fallback
919902
}
920903

904+
fn suggest_shadowed(
905+
&mut self,
906+
err: &mut Diagnostic,
907+
source: PathSource<'_>,
908+
path: &[Segment],
909+
following_seg: Option<&Segment>,
910+
span: Span,
911+
) -> bool {
912+
let is_expected = &|res| source.is_expected(res);
913+
let typo_sugg =
914+
self.lookup_typo_candidate(path, following_seg, source.namespace(), is_expected);
915+
let is_in_same_file = &|sp1, sp2| {
916+
let source_map = self.r.tcx.sess.source_map();
917+
let file1 = source_map.span_to_filename(sp1);
918+
let file2 = source_map.span_to_filename(sp2);
919+
file1 == file2
920+
};
921+
// print 'you might have meant' if the candidate is (1) is a shadowed name with
922+
// accessible definition and (2) either defined in the same crate as the typo
923+
// (could be in a different file) or introduced in the same file as the typo
924+
// (could belong to a different crate)
925+
if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg
926+
&& res.opt_def_id().is_some_and(|id| id.is_local() || is_in_same_file(span, sugg_span))
927+
{
928+
err.span_label(
929+
sugg_span,
930+
format!("you might have meant to refer to this {}", res.descr()),
931+
);
932+
return true;
933+
}
934+
false
935+
}
936+
921937
fn err_code_special_cases(
922938
&mut self,
923939
err: &mut Diagnostic,

Diff for: src/tools/rustfmt/src/types.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -537,18 +537,29 @@ impl Rewrite for ast::Lifetime {
537537
impl Rewrite for ast::GenericBound {
538538
fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
539539
match *self {
540-
ast::GenericBound::Trait(ref poly_trait_ref, modifiers) => {
540+
ast::GenericBound::Trait(
541+
ref poly_trait_ref,
542+
ast::TraitBoundModifiers {
543+
constness,
544+
asyncness,
545+
polarity,
546+
},
547+
) => {
541548
let snippet = context.snippet(self.span());
542549
let has_paren = snippet.starts_with('(') && snippet.ends_with(')');
543-
let mut constness = modifiers.constness.as_str().to_string();
550+
let mut constness = constness.as_str().to_string();
544551
if !constness.is_empty() {
545552
constness.push(' ');
546553
}
547-
let polarity = modifiers.polarity.as_str();
554+
let mut asyncness = asyncness.as_str().to_string();
555+
if !asyncness.is_empty() {
556+
asyncness.push(' ');
557+
}
558+
let polarity = polarity.as_str();
548559
let shape = shape.offset_left(constness.len() + polarity.len())?;
549560
poly_trait_ref
550561
.rewrite(context, shape)
551-
.map(|s| format!("{constness}{polarity}{s}"))
562+
.map(|s| format!("{constness}{asyncness}{polarity}{s}"))
552563
.map(|s| if has_paren { format!("({})", s) } else { s })
553564
}
554565
ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape),

Diff for: src/tools/rustfmt/tests/target/asyncness.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// rustfmt-edition: 2018
2+
3+
fn foo() -> impl async Fn() {}

Diff for: src/tools/tidy/src/ui_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::path::{Path, PathBuf};
1414
// #73494.
1515
const ENTRY_LIMIT: usize = 900;
1616
// FIXME: The following limits should be reduced eventually.
17-
const ISSUES_ENTRY_LIMIT: usize = 1794;
17+
const ISSUES_ENTRY_LIMIT: usize = 1781;
1818
const ROOT_ENTRY_LIMIT: usize = 870;
1919

2020
const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: tests/ui/lint/issue-121070-let-range.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// check-pass
2+
3+
#![feature(let_chains)]
4+
#![allow(irrefutable_let_patterns)]
5+
fn main() {
6+
let _a = 0..1;
7+
8+
if let x = (0..1) {
9+
eprintln!("x: {:?}", x);
10+
}
11+
if let x = (0..1) &&
12+
let _y = (0..2)
13+
{
14+
eprintln!("x: {:?}", x);
15+
}
16+
}
File renamed without changes.

Diff for: tests/ui/resolve/issue-120559.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use std::io::Read;
2+
3+
fn f<T: Read, U, Read>() {} //~ ERROR expected trait, found type parameter `Read`
4+
5+
fn main() {}

Diff for: tests/ui/resolve/issue-120559.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0404]: expected trait, found type parameter `Read`
2+
--> $DIR/issue-120559.rs:3:9
3+
|
4+
LL | use std::io::Read;
5+
| ---- you might have meant to refer to this trait
6+
LL |
7+
LL | fn f<T: Read, U, Read>() {}
8+
| ^^^^ ---- found this type parameter
9+
| |
10+
| not a trait
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0404`.

Diff for: tests/ui/span/issue-35987.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ LL | impl<T: Clone, Add> Add for Foo<T> {
88
| --- ^^^ not a trait
99
| |
1010
| found this type parameter
11-
|
12-
help: consider importing this trait instead
13-
|
14-
LL + use std::ops::Add;
15-
|
1611

1712
error: aborting due to 1 previous error
1813

0 commit comments

Comments
 (0)