Skip to content

Commit d1991c5

Browse files
Auto merge of #145566 - Zalathar:rollup-hqapzc4, r=<try>
Rollup of 18 pull requests try-job: dist-loongarch64-musl
2 parents 425a9c0 + 374a77f commit d1991c5

File tree

239 files changed

+2252
-1670
lines changed

Some content is hidden

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

239 files changed

+2252
-1670
lines changed

compiler/rustc_attr_parsing/messages.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ attr_parsing_empty_attribute =
1212
1313
attr_parsing_invalid_target = `#[{$name}]` attribute cannot be used on {$target}
1414
.help = `#[{$name}]` can {$only}be applied to {$applied}
15+
.suggestion = remove the attribute
1516
attr_parsing_invalid_target_lint = `#[{$name}]` attribute cannot be used on {$target}
1617
.warn = {-attr_parsing_previously_accepted}
1718
.help = `#[{$name}]` can {$only}be applied to {$applied}
19+
.suggestion = remove the attribute
1820
1921
attr_parsing_empty_confusables =
2022
expected at least one confusable name

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
6262
}
6363
}
6464
ArgParser::NameValue(_) => {
65-
let suggestions =
66-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "inline");
65+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
66+
.suggestions(cx.attr_style, "inline");
6767
let span = cx.attr_span;
6868
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
6969
return None;

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ impl<S: Stage> AttributeParser<S> for MacroUseParser {
107107
}
108108
}
109109
ArgParser::NameValue(_) => {
110-
let suggestions = MACRO_USE_TEMPLATE.suggestions(false, sym::macro_use);
110+
let suggestions = MACRO_USE_TEMPLATE.suggestions(cx.attr_style, sym::macro_use);
111111
cx.emit_err(session_diagnostics::IllFormedAttributeInputLint {
112112
num_suggestions: suggestions.len(),
113113
suggestions: DiagArgValue::StrListSepByAnd(

compiler/rustc_attr_parsing/src/attributes/must_use.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use rustc_errors::DiagArgValue;
22
use rustc_feature::{AttributeTemplate, template};
33
use rustc_hir::attrs::AttributeKind;
4+
use rustc_hir::{MethodKind, Target};
45
use rustc_span::{Symbol, sym};
56

67
use crate::attributes::{AttributeOrder, OnDuplicate, SingleAttributeParser};
7-
use crate::context::{ALL_TARGETS, AcceptContext, AllowedTargets, Stage};
8+
use crate::context::MaybeWarn::{Allow, Error};
9+
use crate::context::{AcceptContext, AllowedTargets, Stage};
810
use crate::parser::ArgParser;
911
use crate::session_diagnostics;
1012
pub(crate) struct MustUseParser;
@@ -13,7 +15,21 @@ impl<S: Stage> SingleAttributeParser<S> for MustUseParser {
1315
const PATH: &[Symbol] = &[sym::must_use];
1416
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepOutermost;
1517
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
16-
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS); //FIXME Still checked fully in `check_attr.rs`
18+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowListWarnRest(&[
19+
Allow(Target::Fn),
20+
Allow(Target::Enum),
21+
Allow(Target::Struct),
22+
Allow(Target::Union),
23+
Allow(Target::Method(MethodKind::Trait { body: false })),
24+
Allow(Target::Method(MethodKind::Trait { body: true })),
25+
Allow(Target::Method(MethodKind::Inherent)),
26+
Allow(Target::ForeignFn),
27+
// `impl Trait` in return position can trip
28+
// `unused_must_use` if `Trait` is marked as
29+
// `#[must_use]`
30+
Allow(Target::Trait),
31+
Error(Target::WherePredicate),
32+
]);
1733
const TEMPLATE: AttributeTemplate = template!(
1834
Word, NameValueStr: "reason",
1935
"https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"
@@ -35,8 +51,8 @@ impl<S: Stage> SingleAttributeParser<S> for MustUseParser {
3551
Some(value_str)
3652
}
3753
ArgParser::List(_) => {
38-
let suggestions =
39-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "must_use");
54+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
55+
.suggestions(cx.attr_style, "must_use");
4056
cx.emit_err(session_diagnostics::IllFormedAttributeInputLint {
4157
num_suggestions: suggestions.len(),
4258
suggestions: DiagArgValue::StrListSepByAnd(

compiler/rustc_attr_parsing/src/attributes/test_attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
2929
ArgParser::NameValue(name_value) => {
3030
let Some(str_value) = name_value.value_as_str() else {
3131
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
32-
.suggestions(false, "ignore");
32+
.suggestions(cx.attr_style, "ignore");
3333
let span = cx.attr_span;
3434
cx.emit_lint(
3535
AttributeLintKind::IllFormedAttributeInput { suggestions },
@@ -40,8 +40,8 @@ impl<S: Stage> SingleAttributeParser<S> for IgnoreParser {
4040
Some(str_value)
4141
}
4242
ArgParser::List(_) => {
43-
let suggestions =
44-
<Self as SingleAttributeParser<S>>::TEMPLATE.suggestions(false, "ignore");
43+
let suggestions = <Self as SingleAttributeParser<S>>::TEMPLATE
44+
.suggestions(cx.attr_style, "ignore");
4545
let span = cx.attr_span;
4646
cx.emit_lint(AttributeLintKind::IllFormedAttributeInput { suggestions }, span);
4747
return None;

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::LazyLock;
55

66
use itertools::Itertools;
77
use private::Sealed;
8-
use rustc_ast::{self as ast, LitKind, MetaItemLit, NodeId};
8+
use rustc_ast::{self as ast, AttrStyle, LitKind, MetaItemLit, NodeId};
99
use rustc_errors::{DiagCtxtHandle, Diagnostic};
1010
use rustc_feature::{AttributeTemplate, Features};
1111
use rustc_hir::attrs::AttributeKind;
@@ -313,6 +313,7 @@ pub struct AcceptContext<'f, 'sess, S: Stage> {
313313
/// The span of the attribute currently being parsed
314314
pub(crate) attr_span: Span,
315315

316+
pub(crate) attr_style: AttrStyle,
316317
/// The expected structure of the attribute.
317318
///
318319
/// Used in reporting errors to give a hint to users what the attribute *should* look like.
@@ -394,6 +395,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
394395
i.kind.is_bytestr().then(|| self.sess().source_map().start_point(i.span))
395396
}),
396397
},
398+
attr_style: self.attr_style,
397399
})
398400
}
399401

@@ -404,6 +406,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
404406
template: self.template.clone(),
405407
attribute: self.attr_path.clone(),
406408
reason: AttributeParseErrorReason::ExpectedIntegerLiteral,
409+
attr_style: self.attr_style,
407410
})
408411
}
409412

@@ -414,6 +417,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
414417
template: self.template.clone(),
415418
attribute: self.attr_path.clone(),
416419
reason: AttributeParseErrorReason::ExpectedList,
420+
attr_style: self.attr_style,
417421
})
418422
}
419423

@@ -424,6 +428,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
424428
template: self.template.clone(),
425429
attribute: self.attr_path.clone(),
426430
reason: AttributeParseErrorReason::ExpectedNoArgs,
431+
attr_style: self.attr_style,
427432
})
428433
}
429434

@@ -435,6 +440,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
435440
template: self.template.clone(),
436441
attribute: self.attr_path.clone(),
437442
reason: AttributeParseErrorReason::ExpectedIdentifier,
443+
attr_style: self.attr_style,
438444
})
439445
}
440446

@@ -447,6 +453,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
447453
template: self.template.clone(),
448454
attribute: self.attr_path.clone(),
449455
reason: AttributeParseErrorReason::ExpectedNameValue(name),
456+
attr_style: self.attr_style,
450457
})
451458
}
452459

@@ -458,6 +465,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
458465
template: self.template.clone(),
459466
attribute: self.attr_path.clone(),
460467
reason: AttributeParseErrorReason::DuplicateKey(key),
468+
attr_style: self.attr_style,
461469
})
462470
}
463471

@@ -470,6 +478,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
470478
template: self.template.clone(),
471479
attribute: self.attr_path.clone(),
472480
reason: AttributeParseErrorReason::UnexpectedLiteral,
481+
attr_style: self.attr_style,
473482
})
474483
}
475484

@@ -480,6 +489,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
480489
template: self.template.clone(),
481490
attribute: self.attr_path.clone(),
482491
reason: AttributeParseErrorReason::ExpectedSingleArgument,
492+
attr_style: self.attr_style,
483493
})
484494
}
485495

@@ -490,6 +500,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
490500
template: self.template.clone(),
491501
attribute: self.attr_path.clone(),
492502
reason: AttributeParseErrorReason::ExpectedAtLeastOneArgument,
503+
attr_style: self.attr_style,
493504
})
494505
}
495506

@@ -508,6 +519,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
508519
strings: false,
509520
list: false,
510521
},
522+
attr_style: self.attr_style,
511523
})
512524
}
513525

@@ -526,6 +538,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
526538
strings: false,
527539
list: true,
528540
},
541+
attr_style: self.attr_style,
529542
})
530543
}
531544

@@ -544,6 +557,7 @@ impl<'f, 'sess: 'f, S: Stage> AcceptContext<'f, 'sess, S> {
544557
strings: true,
545558
list: false,
546559
},
560+
attr_style: self.attr_style,
547561
})
548562
}
549563

@@ -802,6 +816,7 @@ impl<'sess> AttributeParser<'sess, Early> {
802816
},
803817
},
804818
attr_span: attr.span,
819+
attr_style: attr.style,
805820
template,
806821
attr_path: path.get_attribute_path(),
807822
};
@@ -912,6 +927,7 @@ impl<'sess, S: Stage> AttributeParser<'sess, S> {
912927
emit_lint: &mut emit_lint,
913928
},
914929
attr_span: lower_span(attr.span),
930+
attr_style: attr.style,
915931
template: &accept.template,
916932
attr_path: path.get_attribute_path(),
917933
};

compiler/rustc_attr_parsing/src/lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub fn emit_attribute_lint<L: LintEmitter>(lint: &AttributeLint<HirId>, lint_emi
5353
target: target.plural_name(),
5454
applied: applied.clone(),
5555
only,
56+
attr_span: *span,
5657
},
5758
),
5859
}

compiler/rustc_attr_parsing/src/session_diagnostics.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::num::IntErrorKind;
22

3-
use rustc_ast as ast;
3+
use rustc_ast::{self as ast, AttrStyle};
44
use rustc_errors::codes::*;
55
use rustc_errors::{
66
Applicability, Diag, DiagArgValue, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level,
@@ -489,13 +489,16 @@ pub(crate) struct InvalidTargetLint {
489489
pub target: &'static str,
490490
pub applied: String,
491491
pub only: &'static str,
492+
#[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")]
493+
pub attr_span: Span,
492494
}
493495

494496
#[derive(Diagnostic)]
495497
#[help]
496498
#[diag(attr_parsing_invalid_target)]
497499
pub(crate) struct InvalidTarget {
498500
#[primary_span]
501+
#[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")]
499502
pub span: Span,
500503
pub name: Symbol,
501504
pub target: &'static str,
@@ -579,6 +582,7 @@ pub(crate) enum AttributeParseErrorReason {
579582
pub(crate) struct AttributeParseError {
580583
pub(crate) span: Span,
581584
pub(crate) attr_span: Span,
585+
pub(crate) attr_style: AttrStyle,
582586
pub(crate) template: AttributeTemplate,
583587
pub(crate) attribute: AttrPath,
584588
pub(crate) reason: AttributeParseErrorReason,
@@ -717,7 +721,8 @@ impl<'a, G: EmissionGuarantee> Diagnostic<'a, G> for AttributeParseError {
717721
if let Some(link) = self.template.docs {
718722
diag.note(format!("for more information, visit <{link}>"));
719723
}
720-
let suggestions = self.template.suggestions(false, &name);
724+
let suggestions = self.template.suggestions(self.attr_style, &name);
725+
721726
diag.span_suggestions(
722727
self.attr_span,
723728
if suggestions.len() == 1 {

compiler/rustc_borrowck/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ borrowck_lifetime_constraints_error =
9090
lifetime may not live long enough
9191
9292
borrowck_limitations_implies_static =
93-
due to current limitations in the borrow checker, this implies a `'static` lifetime
93+
due to a current limitation of the type system, this implies a `'static` lifetime
9494
9595
borrowck_move_closure_suggestion =
9696
consider adding 'move' keyword before the nested closure

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use rustc_abi::{FieldIdx, VariantIdx};
66
use rustc_data_structures::fx::FxIndexMap;
77
use rustc_errors::{Applicability, Diag, EmissionGuarantee, MultiSpan, listify};
88
use rustc_hir::def::{CtorKind, Namespace};
9-
use rustc_hir::{self as hir, CoroutineKind, LangItem};
9+
use rustc_hir::{
10+
self as hir, CoroutineKind, GenericBound, LangItem, WhereBoundPredicate, WherePredicateKind,
11+
};
1012
use rustc_index::{IndexSlice, IndexVec};
1113
use rustc_infer::infer::{BoundRegionConversionTime, NllRegionVariableOrigin};
1214
use rustc_infer::traits::SelectionError;
@@ -658,25 +660,66 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
658660

659661
/// Add a note to region errors and borrow explanations when higher-ranked regions in predicates
660662
/// implicitly introduce an "outlives `'static`" constraint.
663+
///
664+
/// This is very similar to `fn suggest_static_lifetime_for_gat_from_hrtb` which handles this
665+
/// note for failed type tests instead of outlives errors.
661666
fn add_placeholder_from_predicate_note<G: EmissionGuarantee>(
662667
&self,
663-
err: &mut Diag<'_, G>,
668+
diag: &mut Diag<'_, G>,
664669
path: &[OutlivesConstraint<'tcx>],
665670
) {
666-
let predicate_span = path.iter().find_map(|constraint| {
671+
let tcx = self.infcx.tcx;
672+
let Some((gat_hir_id, generics)) = path.iter().find_map(|constraint| {
667673
let outlived = constraint.sub;
668674
if let Some(origin) = self.regioncx.definitions.get(outlived)
669-
&& let NllRegionVariableOrigin::Placeholder(_) = origin.origin
670-
&& let ConstraintCategory::Predicate(span) = constraint.category
675+
&& let NllRegionVariableOrigin::Placeholder(placeholder) = origin.origin
676+
&& let Some(id) = placeholder.bound.kind.get_id()
677+
&& let Some(placeholder_id) = id.as_local()
678+
&& let gat_hir_id = tcx.local_def_id_to_hir_id(placeholder_id)
679+
&& let Some(generics_impl) =
680+
tcx.parent_hir_node(tcx.parent_hir_id(gat_hir_id)).generics()
671681
{
672-
Some(span)
682+
Some((gat_hir_id, generics_impl))
673683
} else {
674684
None
675685
}
676-
});
686+
}) else {
687+
return;
688+
};
677689

678-
if let Some(span) = predicate_span {
679-
err.span_note(span, "due to current limitations in the borrow checker, this implies a `'static` lifetime");
690+
// Look for the where-bound which introduces the placeholder.
691+
// As we're using the HIR, we need to handle both `for<'a> T: Trait<'a>`
692+
// and `T: for<'a> Trait`<'a>.
693+
for pred in generics.predicates {
694+
let WherePredicateKind::BoundPredicate(WhereBoundPredicate {
695+
bound_generic_params,
696+
bounds,
697+
..
698+
}) = pred.kind
699+
else {
700+
continue;
701+
};
702+
if bound_generic_params
703+
.iter()
704+
.rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
705+
.is_some()
706+
{
707+
diag.span_note(pred.span, fluent::borrowck_limitations_implies_static);
708+
return;
709+
}
710+
for bound in bounds.iter() {
711+
if let GenericBound::Trait(bound) = bound {
712+
if bound
713+
.bound_generic_params
714+
.iter()
715+
.rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == gat_hir_id)
716+
.is_some()
717+
{
718+
diag.span_note(bound.span, fluent::borrowck_limitations_implies_static);
719+
return;
720+
}
721+
}
722+
}
680723
}
681724
}
682725

0 commit comments

Comments
 (0)