Skip to content

Commit b596184

Browse files
committed
Auto merge of #131948 - matthiaskrgr:rollup-c9rvzu6, r=matthiaskrgr
Rollup of 12 pull requests Successful merges: - #116863 (warn less about non-exhaustive in ffi) - #127675 (Remove invalid help diagnostics for const pointer) - #131772 (Remove `const_refs_to_static` TODO in proc_macro) - #131789 (Make sure that outer opaques capture inner opaques's lifetimes even with precise capturing syntax) - #131795 (Stop inverting expectation in normalization errors) - #131920 (Add codegen test for branchy bool match) - #131921 (replace STATX_ALL with (STATX_BASIC_STATS | STATX_BTIME) as former is deprecated) - #131925 (Warn on redundant `--cfg` directive when revisions are used) - #131931 (Remove unnecessary constness from `lower_generic_args_of_path`) - #131932 (use tracked_path in rustc_fluent_macro) - #131936 (feat(rustdoc-json-types): introduce rustc-hash feature) - #131939 (Get rid of `OnlySelfBounds`) Failed merges: - #131181 (Compiletest: Custom differ) r? `@ghost` `@rustbot` modify labels: rollup
2 parents da93539 + 2ecfdba commit b596184

File tree

51 files changed

+437
-374
lines changed

Some content is hidden

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

51 files changed

+437
-374
lines changed

compiler/rustc_ast_lowering/src/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15741574
.collect();
15751575

15761576
// Introduce extra lifetimes if late resolution tells us to.
1577-
let extra_lifetimes = self.resolver.take_extra_lifetime_params(parent_node_id);
1577+
let extra_lifetimes = self.resolver.extra_lifetime_params(parent_node_id);
15781578
params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
15791579
self.lifetime_res_to_generic_param(
15801580
ident,

compiler/rustc_ast_lowering/src/lib.rs

+21-58
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ impl ResolverAstLowering {
268268
///
269269
/// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring
270270
/// should appear at the enclosing `PolyTraitRef`.
271-
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
272-
self.extra_lifetime_params_map.remove(&id).unwrap_or_default()
271+
fn extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
272+
self.extra_lifetime_params_map.get(&id).cloned().unwrap_or_default()
273273
}
274274
}
275275

@@ -885,7 +885,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
885885
let mut generic_params: Vec<_> = self
886886
.lower_generic_params_mut(generic_params, hir::GenericParamSource::Binder)
887887
.collect();
888-
let extra_lifetimes = self.resolver.take_extra_lifetime_params(binder);
888+
let extra_lifetimes = self.resolver.extra_lifetime_params(binder);
889889
debug!(?extra_lifetimes);
890890
generic_params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
891891
self.lifetime_res_to_generic_param(ident, node_id, res, hir::GenericParamSource::Binder)
@@ -1495,62 +1495,25 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14951495
// frequently opened issues show.
14961496
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
14971497

1498-
let captured_lifetimes_to_duplicate = if let Some(args) =
1499-
// We only look for one `use<...>` syntax since we syntactially reject more than one.
1500-
bounds.iter().find_map(
1501-
|bound| match bound {
1502-
ast::GenericBound::Use(a, _) => Some(a),
1503-
_ => None,
1504-
},
1505-
) {
1506-
// We'll actually validate these later on; all we need is the list of
1507-
// lifetimes to duplicate during this portion of lowering.
1508-
args.iter()
1509-
.filter_map(|arg| match arg {
1510-
PreciseCapturingArg::Lifetime(lt) => Some(*lt),
1511-
PreciseCapturingArg::Arg(..) => None,
1512-
})
1513-
// Add in all the lifetimes mentioned in the bounds. We will error
1514-
// them out later, but capturing them here is important to make sure
1515-
// they actually get resolved in resolve_bound_vars.
1516-
.chain(lifetime_collector::lifetimes_in_bounds(self.resolver, bounds))
1517-
.collect()
1518-
} else {
1519-
match origin {
1520-
hir::OpaqueTyOrigin::TyAlias { .. } => {
1521-
// type alias impl trait and associated type position impl trait were
1522-
// decided to capture all in-scope lifetimes, which we collect for
1523-
// all opaques during resolution.
1524-
self.resolver
1525-
.take_extra_lifetime_params(opaque_ty_node_id)
1526-
.into_iter()
1527-
.map(|(ident, id, _)| Lifetime { id, ident })
1528-
.collect()
1529-
}
1530-
hir::OpaqueTyOrigin::FnReturn { in_trait_or_impl, .. } => {
1531-
if in_trait_or_impl.is_some()
1532-
|| self.tcx.features().lifetime_capture_rules_2024
1533-
|| span.at_least_rust_2024()
1534-
{
1535-
// return-position impl trait in trait was decided to capture all
1536-
// in-scope lifetimes, which we collect for all opaques during resolution.
1537-
self.resolver
1538-
.take_extra_lifetime_params(opaque_ty_node_id)
1539-
.into_iter()
1540-
.map(|(ident, id, _)| Lifetime { id, ident })
1541-
.collect()
1542-
} else {
1543-
// in fn return position, like the `fn test<'a>() -> impl Debug + 'a`
1544-
// example, we only need to duplicate lifetimes that appear in the
1545-
// bounds, since those are the only ones that are captured by the opaque.
1546-
lifetime_collector::lifetimes_in_bounds(self.resolver, bounds)
1547-
}
1548-
}
1549-
hir::OpaqueTyOrigin::AsyncFn { .. } => {
1550-
unreachable!("should be using `lower_async_fn_ret_ty`")
1551-
}
1498+
// Whether this opaque always captures lifetimes in scope.
1499+
// Right now, this is all RPITIT and TAITs, and when `lifetime_capture_rules_2024`
1500+
// is enabled. We don't check the span of the edition, since this is done
1501+
// on a per-opaque basis to account for nested opaques.
1502+
let always_capture_in_scope = match origin {
1503+
_ if self.tcx.features().lifetime_capture_rules_2024 => true,
1504+
hir::OpaqueTyOrigin::TyAlias { .. } => true,
1505+
hir::OpaqueTyOrigin::FnReturn { in_trait_or_impl, .. } => in_trait_or_impl.is_some(),
1506+
hir::OpaqueTyOrigin::AsyncFn { .. } => {
1507+
unreachable!("should be using `lower_coroutine_fn_ret_ty`")
15521508
}
15531509
};
1510+
let captured_lifetimes_to_duplicate = lifetime_collector::lifetimes_for_opaque(
1511+
self.resolver,
1512+
always_capture_in_scope,
1513+
opaque_ty_node_id,
1514+
bounds,
1515+
span,
1516+
);
15541517
debug!(?captured_lifetimes_to_duplicate);
15551518

15561519
// Feature gate for RPITIT + use<..>
@@ -1920,7 +1883,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
19201883

19211884
let captured_lifetimes = self
19221885
.resolver
1923-
.take_extra_lifetime_params(opaque_ty_node_id)
1886+
.extra_lifetime_params(opaque_ty_node_id)
19241887
.into_iter()
19251888
.map(|(ident, id, _)| Lifetime { id, ident })
19261889
.collect();

compiler/rustc_ast_lowering/src/lifetime_collector.rs

+43-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use rustc_ast::visit::{self, BoundKind, LifetimeCtxt, Visitor};
2-
use rustc_ast::{GenericBounds, Lifetime, NodeId, PathSegment, PolyTraitRef, Ty, TyKind};
2+
use rustc_ast::{
3+
GenericBound, GenericBounds, Lifetime, NodeId, PathSegment, PolyTraitRef, Ty, TyKind,
4+
};
35
use rustc_data_structures::fx::FxIndexSet;
46
use rustc_hir::def::{DefKind, LifetimeRes, Res};
57
use rustc_middle::span_bug;
@@ -10,14 +12,41 @@ use rustc_span::symbol::{Ident, kw};
1012
use super::ResolverAstLoweringExt;
1113

1214
struct LifetimeCollectVisitor<'ast> {
13-
resolver: &'ast ResolverAstLowering,
15+
resolver: &'ast mut ResolverAstLowering,
16+
always_capture_in_scope: bool,
1417
current_binders: Vec<NodeId>,
1518
collected_lifetimes: FxIndexSet<Lifetime>,
1619
}
1720

1821
impl<'ast> LifetimeCollectVisitor<'ast> {
19-
fn new(resolver: &'ast ResolverAstLowering) -> Self {
20-
Self { resolver, current_binders: Vec::new(), collected_lifetimes: FxIndexSet::default() }
22+
fn new(resolver: &'ast mut ResolverAstLowering, always_capture_in_scope: bool) -> Self {
23+
Self {
24+
resolver,
25+
always_capture_in_scope,
26+
current_binders: Vec::new(),
27+
collected_lifetimes: FxIndexSet::default(),
28+
}
29+
}
30+
31+
fn visit_opaque(&mut self, opaque_ty_node_id: NodeId, bounds: &'ast GenericBounds, span: Span) {
32+
// If we're edition 2024 or within a TAIT or RPITIT, *and* there is no
33+
// `use<>` statement to override the default capture behavior, then
34+
// capture all of the in-scope lifetimes.
35+
if (self.always_capture_in_scope || span.at_least_rust_2024())
36+
&& bounds.iter().all(|bound| !matches!(bound, GenericBound::Use(..)))
37+
{
38+
for (ident, id, _) in self.resolver.extra_lifetime_params(opaque_ty_node_id) {
39+
self.record_lifetime_use(Lifetime { id, ident });
40+
}
41+
}
42+
43+
// We also recurse on the bounds to make sure we capture all the lifetimes
44+
// mentioned in the bounds. These may disagree with the `use<>` list, in which
45+
// case we will error on these later. We will also recurse to visit any
46+
// nested opaques, which may *implicitly* capture lifetimes.
47+
for bound in bounds {
48+
self.visit_param_bound(bound, BoundKind::Bound);
49+
}
2150
}
2251

2352
fn record_lifetime_use(&mut self, lifetime: Lifetime) {
@@ -99,20 +128,24 @@ impl<'ast> Visitor<'ast> for LifetimeCollectVisitor<'ast> {
99128
self.record_elided_anchor(t.id, t.span);
100129
visit::walk_ty(self, t);
101130
}
131+
TyKind::ImplTrait(opaque_ty_node_id, bounds) => {
132+
self.visit_opaque(*opaque_ty_node_id, bounds, t.span)
133+
}
102134
_ => {
103135
visit::walk_ty(self, t);
104136
}
105137
}
106138
}
107139
}
108140

109-
pub(crate) fn lifetimes_in_bounds(
110-
resolver: &ResolverAstLowering,
141+
pub(crate) fn lifetimes_for_opaque(
142+
resolver: &mut ResolverAstLowering,
143+
always_capture_in_scope: bool,
144+
opaque_ty_node_id: NodeId,
111145
bounds: &GenericBounds,
146+
span: Span,
112147
) -> FxIndexSet<Lifetime> {
113-
let mut visitor = LifetimeCollectVisitor::new(resolver);
114-
for bound in bounds {
115-
visitor.visit_param_bound(bound, BoundKind::Bound);
116-
}
148+
let mut visitor = LifetimeCollectVisitor::new(resolver, always_capture_in_scope);
149+
visitor.visit_opaque(opaque_ty_node_id, bounds, span);
117150
visitor.collected_lifetimes
118151
}

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11461146
}
11471147
// don't create labels for compiler-generated spans
11481148
Some(_) => None,
1149+
// don't create labels for the span not from user's code
1150+
None if opt_assignment_rhs_span
1151+
.is_some_and(|span| self.infcx.tcx.sess.source_map().is_imported(span)) =>
1152+
{
1153+
None
1154+
}
11491155
None => {
11501156
let (has_sugg, decl_span, sugg) = if name != kw::SelfLower {
11511157
suggest_ampmut(
@@ -1198,18 +1204,21 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11981204
sugg.push(s);
11991205
}
12001206

1201-
err.multipart_suggestion_verbose(
1202-
format!(
1203-
"consider changing this to be a mutable {pointer_desc}{}",
1204-
if is_trait_sig {
1205-
" in the `impl` method and the `trait` definition"
1206-
} else {
1207-
""
1208-
}
1209-
),
1210-
sugg,
1211-
Applicability::MachineApplicable,
1212-
);
1207+
if sugg.iter().all(|(span, _)| !self.infcx.tcx.sess.source_map().is_imported(*span))
1208+
{
1209+
err.multipart_suggestion_verbose(
1210+
format!(
1211+
"consider changing this to be a mutable {pointer_desc}{}",
1212+
if is_trait_sig {
1213+
" in the `impl` method and the `trait` definition"
1214+
} else {
1215+
""
1216+
}
1217+
),
1218+
sugg,
1219+
Applicability::MachineApplicable,
1220+
);
1221+
}
12131222
}
12141223
Some((false, err_label_span, message, _)) => {
12151224
let def_id = self.body.source.def_id();

compiler/rustc_fluent_macro/src/fluent.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use fluent_syntax::ast::{
88
Attribute, Entry, Expression, Identifier, InlineExpression, Message, Pattern, PatternElement,
99
};
1010
use fluent_syntax::parser::ParserError;
11+
use proc_macro::tracked_path::path;
1112
use proc_macro::{Diagnostic, Level, Span};
1213
use proc_macro2::TokenStream;
1314
use quote::quote;
@@ -99,8 +100,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
99100

100101
let crate_name = Ident::new(&crate_name, resource_str.span());
101102

102-
// As this macro also outputs an `include_str!` for this file, the macro will always be
103-
// re-executed when the file changes.
103+
path(absolute_ftl_path.to_str().unwrap());
104104
let resource_contents = match read_to_string(absolute_ftl_path) {
105105
Ok(resource_contents) => resource_contents,
106106
Err(e) => {

compiler/rustc_fluent_macro/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![feature(proc_macro_diagnostic)]
77
#![feature(proc_macro_span)]
88
#![feature(rustdoc_internals)]
9+
#![feature(track_path)]
910
#![warn(unreachable_pub)]
1011
// tidy-alphabetical-end
1112

compiler/rustc_hir_analysis/src/bounds.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty::{self, Ty, TyCtxt, Upcast};
99
use rustc_span::Span;
1010
use rustc_span::def_id::DefId;
1111

12-
use crate::hir_ty_lowering::OnlySelfBounds;
12+
use crate::hir_ty_lowering::PredicateFilter;
1313

1414
/// Collects together a list of type bounds. These lists of bounds occur in many places
1515
/// in Rust's syntax:
@@ -52,7 +52,7 @@ impl<'tcx> Bounds<'tcx> {
5252
span: Span,
5353
polarity: ty::PredicatePolarity,
5454
constness: ty::BoundConstness,
55-
only_self_bounds: OnlySelfBounds,
55+
predicate_filter: PredicateFilter,
5656
) {
5757
let clause = (
5858
bound_trait_ref
@@ -72,9 +72,18 @@ impl<'tcx> Bounds<'tcx> {
7272
// FIXME(effects): Lift this out of `push_trait_bound`, and move it somewhere else.
7373
// Perhaps moving this into `lower_poly_trait_ref`, just like we lower associated
7474
// type bounds.
75-
if !tcx.features().effects || only_self_bounds.0 {
75+
if !tcx.features().effects {
7676
return;
7777
}
78+
match predicate_filter {
79+
PredicateFilter::SelfOnly | PredicateFilter::SelfThatDefines(_) => {
80+
return;
81+
}
82+
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
83+
// Ok.
84+
}
85+
}
86+
7887
// For `T: ~const Tr` or `T: const Tr`, we need to add an additional bound on the
7988
// associated type of `<T as Tr>` and make sure that the effect is compatible.
8089
let compat_val = match (tcx.def_kind(defining_def_id), constness) {

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+4-36
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::bounds::Bounds;
1616
use crate::collect::ItemCtxt;
1717
use crate::constrained_generic_params as cgp;
1818
use crate::delegation::inherit_predicates_for_delegation_item;
19-
use crate::hir_ty_lowering::{HirTyLowerer, OnlySelfBounds, PredicateFilter, RegionInferReason};
19+
use crate::hir_ty_lowering::{HirTyLowerer, PredicateFilter, RegionInferReason};
2020

2121
/// Returns a list of all type predicates (explicit and implicit) for the definition with
2222
/// ID `def_id`. This includes all predicates returned by `explicit_predicates_of`, plus
@@ -270,7 +270,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
270270
bound_pred.bounds.iter(),
271271
&mut bounds,
272272
bound_vars,
273-
OnlySelfBounds(false),
273+
PredicateFilter::All,
274274
);
275275
predicates.extend(bounds.clauses(tcx));
276276
effects_min_tys.extend(bounds.effects_min_tys());
@@ -825,20 +825,6 @@ impl<'tcx> ItemCtxt<'tcx> {
825825
continue;
826826
};
827827

828-
// Subtle: If we're collecting `SelfAndAssociatedTypeBounds`, then we
829-
// want to only consider predicates with `Self: ...`, but we don't want
830-
// `OnlySelfBounds(true)` since we want to collect the nested associated
831-
// type bound as well.
832-
let (only_self_bounds, assoc_name) = match filter {
833-
PredicateFilter::All | PredicateFilter::SelfAndAssociatedTypeBounds => {
834-
(OnlySelfBounds(false), None)
835-
}
836-
PredicateFilter::SelfOnly => (OnlySelfBounds(true), None),
837-
PredicateFilter::SelfThatDefines(assoc_name) => {
838-
(OnlySelfBounds(true), Some(assoc_name))
839-
}
840-
};
841-
842828
let bound_ty = if predicate.is_param_bound(param_def_id.to_def_id()) {
843829
ty
844830
} else if matches!(filter, PredicateFilter::All) {
@@ -850,31 +836,13 @@ impl<'tcx> ItemCtxt<'tcx> {
850836
let bound_vars = self.tcx.late_bound_vars(predicate.hir_id);
851837
self.lowerer().lower_poly_bounds(
852838
bound_ty,
853-
predicate.bounds.iter().filter(|bound| {
854-
assoc_name
855-
.map_or(true, |assoc_name| self.bound_defines_assoc_item(bound, assoc_name))
856-
}),
839+
predicate.bounds.iter(),
857840
&mut bounds,
858841
bound_vars,
859-
only_self_bounds,
842+
filter,
860843
);
861844
}
862845

863846
bounds.clauses(self.tcx).collect()
864847
}
865-
866-
#[instrument(level = "trace", skip(self))]
867-
fn bound_defines_assoc_item(&self, b: &hir::GenericBound<'_>, assoc_name: Ident) -> bool {
868-
match b {
869-
hir::GenericBound::Trait(poly_trait_ref) => {
870-
let trait_ref = &poly_trait_ref.trait_ref;
871-
if let Some(trait_did) = trait_ref.trait_def_id() {
872-
self.tcx.trait_may_define_assoc_item(trait_did, assoc_name)
873-
} else {
874-
false
875-
}
876-
}
877-
_ => false,
878-
}
879-
}
880848
}

0 commit comments

Comments
 (0)