Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4e1356b

Browse files
committedFeb 19, 2025
Auto merge of rust-lang#137290 - matthiaskrgr:rollup-a7xdbi4, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#120580 (Add `MAX_LEN_UTF8` and `MAX_LEN_UTF16` Constants) - rust-lang#132268 (Impl TryFrom<Vec<u8>> for String) - rust-lang#136093 (Match Ergonomics 2024: update old-edition behavior of feature gates) - rust-lang#136344 (Suggest replacing `.` with `::` in more error diagnostics.) - rust-lang#136690 (Use more explicit and reliable ptr select in sort impls) - rust-lang#136815 (CI: Stop /msys64/bin from being prepended to PATH in msys2 shell) - rust-lang#136923 (Lint `#[must_use]` attributes applied to methods in trait impls) - rust-lang#137155 (Organize `OsString`/`OsStr` shims) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f280acf + 3964bb1 commit 4e1356b

File tree

83 files changed

+2896
-934
lines changed

Some content is hidden

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

83 files changed

+2896
-934
lines changed
 

‎compiler/rustc_passes/src/check_attr.rs

+29-18
Original file line numberDiff line numberDiff line change
@@ -1431,37 +1431,48 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
14311431

14321432
/// Warns against some misuses of `#[must_use]`
14331433
fn check_must_use(&self, hir_id: HirId, attr: &Attribute, target: Target) {
1434-
if !matches!(
1434+
if matches!(
14351435
target,
14361436
Target::Fn
14371437
| Target::Enum
14381438
| Target::Struct
14391439
| Target::Union
1440-
| Target::Method(_)
1440+
| Target::Method(MethodKind::Trait { body: false } | MethodKind::Inherent)
14411441
| Target::ForeignFn
14421442
// `impl Trait` in return position can trip
14431443
// `unused_must_use` if `Trait` is marked as
14441444
// `#[must_use]`
14451445
| Target::Trait
14461446
) {
1447-
let article = match target {
1448-
Target::ExternCrate
1449-
| Target::Enum
1450-
| Target::Impl
1451-
| Target::Expression
1452-
| Target::Arm
1453-
| Target::AssocConst
1454-
| Target::AssocTy => "an",
1455-
_ => "a",
1456-
};
1447+
return;
1448+
}
14571449

1458-
self.tcx.emit_node_span_lint(
1459-
UNUSED_ATTRIBUTES,
1460-
hir_id,
1461-
attr.span,
1462-
errors::MustUseNoEffect { article, target },
1463-
);
1450+
// `#[must_use]` can be applied to a trait method definition with a default body
1451+
if let Target::Method(MethodKind::Trait { body: true }) = target
1452+
&& let parent_def_id = self.tcx.hir().get_parent_item(hir_id).def_id
1453+
&& let containing_item = self.tcx.hir().expect_item(parent_def_id)
1454+
&& let hir::ItemKind::Trait(..) = containing_item.kind
1455+
{
1456+
return;
14641457
}
1458+
1459+
let article = match target {
1460+
Target::ExternCrate
1461+
| Target::Enum
1462+
| Target::Impl
1463+
| Target::Expression
1464+
| Target::Arm
1465+
| Target::AssocConst
1466+
| Target::AssocTy => "an",
1467+
_ => "a",
1468+
};
1469+
1470+
self.tcx.emit_node_span_lint(
1471+
UNUSED_ATTRIBUTES,
1472+
hir_id,
1473+
attr.span,
1474+
errors::MustUseNoEffect { article, target },
1475+
);
14651476
}
14661477

14671478
/// Checks if `#[must_not_suspend]` is applied to a struct, enum, union, or trait.

‎compiler/rustc_resolve/src/late/diagnostics.rs

+64-24
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::ptr::P;
88
use rustc_ast::visit::{FnCtxt, FnKind, LifetimeCtxt, Visitor, walk_ty};
99
use rustc_ast::{
1010
self as ast, AssocItemKind, DUMMY_NODE_ID, Expr, ExprKind, GenericParam, GenericParamKind,
11-
Item, ItemKind, MethodCall, NodeId, Path, Ty, TyKind,
11+
Item, ItemKind, MethodCall, NodeId, Path, PathSegment, Ty, TyKind,
1212
};
1313
use rustc_ast_pretty::pprust::where_bound_predicate_to_string;
1414
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
@@ -1529,7 +1529,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
15291529
Applicability::MaybeIncorrect,
15301530
);
15311531
true
1532-
} else if kind == DefKind::Struct
1532+
} else if matches!(kind, DefKind::Struct | DefKind::TyAlias)
15331533
&& let Some(lhs_source_span) = lhs_span.find_ancestor_inside(expr.span)
15341534
&& let Ok(snippet) = this.r.tcx.sess.source_map().span_to_snippet(lhs_source_span)
15351535
{
@@ -1566,7 +1566,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
15661566
}
15671567
};
15681568

1569-
let mut bad_struct_syntax_suggestion = |this: &mut Self, def_id: DefId| {
1569+
let bad_struct_syntax_suggestion = |this: &mut Self, err: &mut Diag<'_>, def_id: DefId| {
15701570
let (followed_by_brace, closing_brace) = this.followed_by_brace(span);
15711571

15721572
match source {
@@ -1740,12 +1740,10 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
17401740
}
17411741
}
17421742
(
1743-
Res::Def(kind @ (DefKind::Mod | DefKind::Trait), _),
1743+
Res::Def(kind @ (DefKind::Mod | DefKind::Trait | DefKind::TyAlias), _),
17441744
PathSource::Expr(Some(parent)),
1745-
) => {
1746-
if !path_sep(self, err, parent, kind) {
1747-
return false;
1748-
}
1745+
) if path_sep(self, err, parent, kind) => {
1746+
return true;
17491747
}
17501748
(
17511749
Res::Def(DefKind::Enum, def_id),
@@ -1777,13 +1775,13 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
17771775
let (ctor_def, ctor_vis, fields) = if let Some(struct_ctor) = struct_ctor {
17781776
if let PathSource::Expr(Some(parent)) = source {
17791777
if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind {
1780-
bad_struct_syntax_suggestion(self, def_id);
1778+
bad_struct_syntax_suggestion(self, err, def_id);
17811779
return true;
17821780
}
17831781
}
17841782
struct_ctor
17851783
} else {
1786-
bad_struct_syntax_suggestion(self, def_id);
1784+
bad_struct_syntax_suggestion(self, err, def_id);
17871785
return true;
17881786
};
17891787

@@ -1861,7 +1859,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
18611859
err.span_label(span, "constructor is not visible here due to private fields");
18621860
}
18631861
(Res::Def(DefKind::Union | DefKind::Variant, def_id), _) if ns == ValueNS => {
1864-
bad_struct_syntax_suggestion(self, def_id);
1862+
bad_struct_syntax_suggestion(self, err, def_id);
18651863
}
18661864
(Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id), _) if ns == ValueNS => {
18671865
match source {
@@ -2471,31 +2469,73 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
24712469
def_id: DefId,
24722470
span: Span,
24732471
) {
2474-
let Some(variants) = self.collect_enum_ctors(def_id) else {
2472+
let Some(variant_ctors) = self.collect_enum_ctors(def_id) else {
24752473
err.note("you might have meant to use one of the enum's variants");
24762474
return;
24772475
};
24782476

2479-
let suggest_only_tuple_variants =
2480-
matches!(source, PathSource::TupleStruct(..)) || source.is_call();
2481-
if suggest_only_tuple_variants {
2477+
// If the expression is a field-access or method-call, try to find a variant with the field/method name
2478+
// that could have been intended, and suggest replacing the `.` with `::`.
2479+
// Otherwise, suggest adding `::VariantName` after the enum;
2480+
// and if the expression is call-like, only suggest tuple variants.
2481+
let (suggest_path_sep_dot_span, suggest_only_tuple_variants) = match source {
2482+
// `Type(a, b)` in a pattern, only suggest adding a tuple variant after `Type`.
2483+
PathSource::TupleStruct(..) => (None, true),
2484+
PathSource::Expr(Some(expr)) => match &expr.kind {
2485+
// `Type(a, b)`, only suggest adding a tuple variant after `Type`.
2486+
ExprKind::Call(..) => (None, true),
2487+
// `Type.Foo(a, b)`, suggest replacing `.` -> `::` if variant `Foo` exists and is a tuple variant,
2488+
// otherwise suggest adding a variant after `Type`.
2489+
ExprKind::MethodCall(box MethodCall {
2490+
receiver,
2491+
span,
2492+
seg: PathSegment { ident, .. },
2493+
..
2494+
}) => {
2495+
let dot_span = receiver.span.between(*span);
2496+
let found_tuple_variant = variant_ctors.iter().any(|(path, _, ctor_kind)| {
2497+
*ctor_kind == CtorKind::Fn
2498+
&& path.segments.last().is_some_and(|seg| seg.ident == *ident)
2499+
});
2500+
(found_tuple_variant.then_some(dot_span), false)
2501+
}
2502+
// `Type.Foo`, suggest replacing `.` -> `::` if variant `Foo` exists and is a unit or tuple variant,
2503+
// otherwise suggest adding a variant after `Type`.
2504+
ExprKind::Field(base, ident) => {
2505+
let dot_span = base.span.between(ident.span);
2506+
let found_tuple_or_unit_variant = variant_ctors.iter().any(|(path, ..)| {
2507+
path.segments.last().is_some_and(|seg| seg.ident == *ident)
2508+
});
2509+
(found_tuple_or_unit_variant.then_some(dot_span), false)
2510+
}
2511+
_ => (None, false),
2512+
},
2513+
_ => (None, false),
2514+
};
2515+
2516+
if let Some(dot_span) = suggest_path_sep_dot_span {
2517+
err.span_suggestion_verbose(
2518+
dot_span,
2519+
"use the path separator to refer to a variant",
2520+
"::",
2521+
Applicability::MaybeIncorrect,
2522+
);
2523+
} else if suggest_only_tuple_variants {
24822524
// Suggest only tuple variants regardless of whether they have fields and do not
24832525
// suggest path with added parentheses.
2484-
let mut suggestable_variants = variants
2526+
let mut suggestable_variants = variant_ctors
24852527
.iter()
24862528
.filter(|(.., kind)| *kind == CtorKind::Fn)
24872529
.map(|(variant, ..)| path_names_to_string(variant))
24882530
.collect::<Vec<_>>();
24892531
suggestable_variants.sort();
24902532

2491-
let non_suggestable_variant_count = variants.len() - suggestable_variants.len();
2533+
let non_suggestable_variant_count = variant_ctors.len() - suggestable_variants.len();
24922534

2493-
let source_msg = if source.is_call() {
2494-
"to construct"
2495-
} else if matches!(source, PathSource::TupleStruct(..)) {
2535+
let source_msg = if matches!(source, PathSource::TupleStruct(..)) {
24962536
"to match against"
24972537
} else {
2498-
unreachable!()
2538+
"to construct"
24992539
};
25002540

25012541
if !suggestable_variants.is_empty() {
@@ -2514,7 +2554,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
25142554
}
25152555

25162556
// If the enum has no tuple variants..
2517-
if non_suggestable_variant_count == variants.len() {
2557+
if non_suggestable_variant_count == variant_ctors.len() {
25182558
err.help(format!("the enum has no tuple variants {source_msg}"));
25192559
}
25202560

@@ -2537,7 +2577,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
25372577
}
25382578
};
25392579

2540-
let mut suggestable_variants = variants
2580+
let mut suggestable_variants = variant_ctors
25412581
.iter()
25422582
.filter(|(_, def_id, kind)| !needs_placeholder(*def_id, *kind))
25432583
.map(|(variant, _, kind)| (path_names_to_string(variant), kind))
@@ -2564,7 +2604,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
25642604
);
25652605
}
25662606

2567-
let mut suggestable_variants_with_placeholders = variants
2607+
let mut suggestable_variants_with_placeholders = variant_ctors
25682608
.iter()
25692609
.filter(|(_, def_id, kind)| needs_placeholder(*def_id, *kind))
25702610
.map(|(variant, _, kind)| (path_names_to_string(variant), kind))

‎library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
#![feature(box_uninit_write)]
106106
#![feature(bstr)]
107107
#![feature(bstr_internals)]
108+
#![feature(char_max_len)]
108109
#![feature(clone_to_uninit)]
109110
#![feature(coerce_unsized)]
110111
#![feature(const_eval_select)]
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.