Skip to content

Commit 492c116

Browse files
committed
Auto merge of #157663 - GuillaumeGomez:rollup-Iwi31Tb, r=GuillaumeGomez
Rollup of 10 pull requests Successful merges: - #157335 (bootstrap: Handle dotted table keys when parsing bootstrap.toml) - #157503 (Disable `tests/debuginfo/pretty-std.rs` `OsString` cdb check) - #148183 (rustdoc: Test & document `test_harness` code block attribute) - #156067 (Fix async drop glue for Box<T>) - #156399 (fix improper ctypes in Znext solver) - #157410 (Implement rustc_public::CrateDef{,Type} for FieldDef) - #157605 (Arg splat experiment - syntax impl) - #157630 (Add multibyte JSON diagnostic regression test) - #157633 (Reorder `impl` restriction rendering and add bottom margin) - #157642 (Report duplicate relaxed bounds during ast lowering)
2 parents beae781 + e502516 commit 492c116

53 files changed

Lines changed: 1210 additions & 236 deletions

File tree

Some content is hidden

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

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_hir::{
1111
};
1212
use rustc_index::{IndexSlice, IndexVec};
1313
use rustc_middle::span_bug;
14+
use rustc_middle::ty::data_structures::IndexMap;
1415
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1516
use rustc_span::def_id::DefId;
1617
use rustc_span::edit_distance::find_best_match_for_name;
@@ -326,7 +327,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
326327
let ident = self.lower_ident(*ident);
327328
let (generics, (ty, rhs)) = self.lower_generics(
328329
generics,
329-
id,
330330
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
331331
|this| {
332332
let ty = this.lower_ty_alloc(
@@ -380,7 +380,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
380380
);
381381

382382
let itctx = ImplTraitContext::Universal;
383-
let (generics, decl) = this.lower_generics(generics, id, itctx, |this| {
383+
let (generics, decl) = this.lower_generics(generics, itctx, |this| {
384384
this.lower_fn_decl(decl, id, *fn_sig_span, FnDeclKind::Fn, coroutine_kind)
385385
});
386386
let sig = hir::FnSig {
@@ -434,7 +434,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
434434
add_ty_alias_where_clause(&mut generics, after_where_clause, true);
435435
let (generics, ty) = self.lower_generics(
436436
&generics,
437-
id,
438437
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
439438
|this| match ty {
440439
None => {
@@ -461,7 +460,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
461460
let ident = self.lower_ident(*ident);
462461
let (generics, variants) = self.lower_generics(
463462
generics,
464-
id,
465463
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
466464
|this| {
467465
this.arena.alloc_from_iter(
@@ -475,7 +473,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
475473
let ident = self.lower_ident(*ident);
476474
let (generics, struct_def) = self.lower_generics(
477475
generics,
478-
id,
479476
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
480477
|this| this.lower_variant_data(hir_id, i, struct_def),
481478
);
@@ -485,7 +482,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
485482
let ident = self.lower_ident(*ident);
486483
let (generics, vdata) = self.lower_generics(
487484
generics,
488-
id,
489485
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
490486
|this| this.lower_variant_data(hir_id, i, vdata),
491487
);
@@ -513,7 +509,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
513509
// parent lifetime.
514510
let itctx = ImplTraitContext::Universal;
515511
let (generics, (of_trait, lowered_ty)) =
516-
self.lower_generics(ast_generics, id, itctx, |this| {
512+
self.lower_generics(ast_generics, itctx, |this| {
517513
let of_trait = of_trait
518514
.as_deref()
519515
.map(|of_trait| this.lower_trait_impl_header(of_trait));
@@ -555,7 +551,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
555551
let ident = self.lower_ident(*ident);
556552
let (generics, (safety, items, bounds)) = self.lower_generics(
557553
generics,
558-
id,
559554
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
560555
|this| {
561556
let bounds = this.lower_param_bounds(
@@ -586,7 +581,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
586581
let ident = self.lower_ident(*ident);
587582
let (generics, bounds) = self.lower_generics(
588583
generics,
589-
id,
590584
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
591585
|this| {
592586
this.lower_param_bounds(
@@ -798,14 +792,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
798792
ForeignItemKind::Fn(Fn { sig, ident, generics, define_opaque, .. }) => {
799793
let fdec = &sig.decl;
800794
let itctx = ImplTraitContext::Universal;
801-
let (generics, (decl, fn_args)) =
802-
self.lower_generics(generics, i.id, itctx, |this| {
803-
(
804-
// Disallow `impl Trait` in foreign items.
805-
this.lower_fn_decl(fdec, i.id, sig.span, FnDeclKind::ExternFn, None),
806-
this.lower_fn_params_to_idents(fdec),
807-
)
808-
});
795+
let (generics, (decl, fn_args)) = self.lower_generics(generics, itctx, |this| {
796+
(
797+
// Disallow `impl Trait` in foreign items.
798+
this.lower_fn_decl(fdec, i.id, sig.span, FnDeclKind::ExternFn, None),
799+
this.lower_fn_params_to_idents(fdec),
800+
)
801+
});
809802

810803
// Unmarked safety in unsafe block defaults to unsafe.
811804
let header = self.lower_fn_header(sig.header, hir::Safety::Unsafe, attrs);
@@ -995,7 +988,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
995988
}) => {
996989
let (generics, kind) = self.lower_generics(
997990
generics,
998-
i.id,
999991
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
1000992
|this| {
1001993
let ty = this.lower_ty_alloc(
@@ -1097,7 +1089,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
10971089
add_ty_alias_where_clause(&mut generics, after_where_clause, false);
10981090
let (generics, kind) = self.lower_generics(
10991091
&generics,
1100-
i.id,
11011092
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
11021093
|this| {
11031094
let ty = ty.as_ref().map(|x| {
@@ -1109,7 +1100,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11091100
hir::TraitItemKind::Type(
11101101
this.lower_param_bounds(
11111102
bounds,
1112-
RelaxedBoundPolicy::Allowed,
1103+
RelaxedBoundPolicy::Allowed(&mut Default::default()),
11131104
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
11141105
),
11151106
ty,
@@ -1260,7 +1251,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
12601251
*ident,
12611252
self.lower_generics(
12621253
generics,
1263-
i.id,
12641254
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
12651255
|this| {
12661256
let ty = this.lower_ty_alloc(
@@ -1305,7 +1295,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
13051295
*ident,
13061296
self.lower_generics(
13071297
&generics,
1308-
i.id,
13091298
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
13101299
|this| match ty {
13111300
None => {
@@ -1744,7 +1733,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17441733
) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) {
17451734
let header = self.lower_fn_header(sig.header, hir::Safety::Safe, attrs);
17461735
let itctx = ImplTraitContext::Universal;
1747-
let (generics, decl) = self.lower_generics(generics, id, itctx, |this| {
1736+
let (generics, decl) = self.lower_generics(generics, itctx, |this| {
17481737
this.lower_fn_decl(&sig.decl, id, sig.span, kind, coroutine_kind)
17491738
});
17501739
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
@@ -1903,14 +1892,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
19031892
fn lower_generics<T>(
19041893
&mut self,
19051894
generics: &Generics,
1906-
parent_node_id: NodeId,
19071895
itctx: ImplTraitContext,
19081896
f: impl FnOnce(&mut Self) -> T,
19091897
) -> (&'hir hir::Generics<'hir>, T) {
19101898
assert!(self.impl_trait_defs.is_empty());
19111899
assert!(self.impl_trait_bounds.is_empty());
19121900

19131901
let mut predicates: SmallVec<[hir::WherePredicate<'hir>; 4]> = SmallVec::new();
1902+
// We need to make sure that generic params don't have multiple relaxed bounds for the same trait
1903+
// across generic param bounds and where bounds.
1904+
let mut dedup_map: IndexMap<LocalDefId, _> = Default::default();
19141905
predicates.extend(generics.params.iter().filter_map(|param| {
19151906
self.lower_generic_bound_predicate(
19161907
param.ident,
@@ -1919,25 +1910,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
19191910
&param.bounds,
19201911
param.colon_span,
19211912
generics.span,
1922-
RelaxedBoundPolicy::Allowed,
1913+
RelaxedBoundPolicy::Allowed(
1914+
dedup_map.entry(self.local_def_id(param.id)).or_default(),
1915+
),
19231916
itctx,
19241917
PredicateOrigin::GenericParam,
19251918
)
19261919
}));
1927-
predicates.extend(
1928-
generics
1929-
.where_clause
1930-
.predicates
1931-
.iter()
1932-
.map(|predicate| self.lower_where_predicate(predicate, &generics.params)),
1933-
);
1920+
predicates.extend(generics.where_clause.predicates.iter().map(|predicate| {
1921+
self.lower_where_predicate(predicate, &generics.params, &mut dedup_map)
1922+
}));
19341923

19351924
let mut params: SmallVec<[hir::GenericParam<'hir>; 4]> = self
19361925
.lower_generic_params_mut(&generics.params, hir::GenericParamSource::Generics)
19371926
.collect();
19381927

19391928
// Introduce extra lifetimes if late resolution tells us to.
1940-
let extra_lifetimes = self.resolver.extra_lifetime_params(parent_node_id);
1929+
let extra_lifetimes = self.resolver.extra_lifetime_params(self.owner.id);
19411930
params.extend(extra_lifetimes.into_iter().map(|&(ident, node_id, kind)| {
19421931
self.lifetime_res_to_generic_param(
19431932
ident,
@@ -2006,7 +1995,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20061995
bounds: &[GenericBound],
20071996
colon_span: Option<Span>,
20081997
parent_span: Span,
2009-
rbp: RelaxedBoundPolicy,
1998+
rbp: RelaxedBoundPolicy<'_>,
20101999
itctx: ImplTraitContext,
20112000
origin: PredicateOrigin,
20122001
) -> Option<hir::WherePredicate<'hir>> {
@@ -2071,6 +2060,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20712060
&mut self,
20722061
pred: &WherePredicate,
20732062
params: &[ast::GenericParam],
2063+
dedup_map: &mut IndexMap<LocalDefId, IndexMap<DefId, Span>>,
20742064
) -> hir::WherePredicate<'hir> {
20752065
let hir_id = self.lower_node_id(pred.id);
20762066
let span = self.lower_span(pred.span);
@@ -2087,7 +2077,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
20872077
&& let Res::Def(DefKind::TyParam, def_id) = res
20882078
&& params.iter().any(|p| def_id == self.local_def_id(p.id).to_def_id())
20892079
{
2090-
RelaxedBoundPolicy::Allowed
2080+
RelaxedBoundPolicy::Allowed(dedup_map.entry(def_id.expect_local()).or_default())
20912081
} else {
20922082
RelaxedBoundPolicy::Forbidden(RelaxedBoundForbiddenReason::WhereBound)
20932083
};
@@ -2117,7 +2107,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21172107
),
21182108
bounds: self.lower_param_bounds(
21192109
bounds,
2120-
RelaxedBoundPolicy::Allowed,
2110+
RelaxedBoundPolicy::Allowed(&mut Default::default()),
21212111
ImplTraitContext::Disallowed(ImplTraitPosition::Bound),
21222112
),
21232113
in_where_clause: true,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ use rustc_ast::visit::Visitor;
4343
use rustc_ast::{self as ast, *};
4444
use rustc_attr_parsing::{AttributeParser, OmitDoc, Recovery, ShouldEmit};
4545
use rustc_data_structures::fingerprint::Fingerprint;
46-
use rustc_data_structures::fx::FxIndexSet;
46+
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
4747
use rustc_data_structures::sorted_map::SortedMap;
4848
use rustc_data_structures::stable_hash::{StableHash, StableHasher};
4949
use rustc_data_structures::steal::Steal;
5050
use rustc_data_structures::tagged_ptr::TaggedRef;
51+
use rustc_errors::codes::*;
5152
use rustc_errors::{DiagArgFromDisplay, DiagCtxtHandle};
5253
use rustc_hir::def::{DefKind, LifetimeRes, Namespace, PartialRes, PerNS, Res};
53-
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE, LocalDefId};
54+
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE, LocalDefId};
5455
use rustc_hir::definitions::PerParentDisambiguatorState;
5556
use rustc_hir::lints::DelayedLint;
5657
use rustc_hir::{
@@ -319,11 +320,20 @@ impl<'tcx> ResolverAstLowering<'tcx> {
319320
///
320321
/// Relaxed bounds should only be allowed in places where we later
321322
/// (namely during HIR ty lowering) perform *sized elaboration*.
322-
#[derive(Clone, Copy, Debug)]
323-
enum RelaxedBoundPolicy {
324-
Allowed,
323+
#[derive(Debug)]
324+
enum RelaxedBoundPolicy<'a> {
325+
/// The `DefId` refers to the trait that is being relaxed.
326+
Allowed(&'a mut FxIndexMap<DefId, Span>),
325327
Forbidden(RelaxedBoundForbiddenReason),
326328
}
329+
impl RelaxedBoundPolicy<'_> {
330+
fn reborrow(&mut self) -> RelaxedBoundPolicy<'_> {
331+
match self {
332+
RelaxedBoundPolicy::Allowed(m) => RelaxedBoundPolicy::Allowed(m),
333+
RelaxedBoundPolicy::Forbidden(reason) => RelaxedBoundPolicy::Forbidden(*reason),
334+
}
335+
}
336+
}
327337

328338
#[derive(Clone, Copy, Debug)]
329339
enum RelaxedBoundForbiddenReason {
@@ -1551,9 +1561,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
15511561
}
15521562
path
15531563
}
1554-
ImplTraitContext::InBinding => hir::TyKind::TraitAscription(
1555-
self.lower_param_bounds(bounds, RelaxedBoundPolicy::Allowed, itctx),
1556-
),
1564+
ImplTraitContext::InBinding => {
1565+
hir::TyKind::TraitAscription(self.lower_param_bounds(
1566+
bounds,
1567+
RelaxedBoundPolicy::Allowed(&mut Default::default()),
1568+
itctx,
1569+
))
1570+
}
15571571
ImplTraitContext::FeatureGated(position, feature) => {
15581572
let guar = self
15591573
.tcx
@@ -1676,7 +1690,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
16761690
let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None);
16771691

16781692
self.lower_opaque_inner(opaque_ty_node_id, origin, opaque_ty_span, |this| {
1679-
this.lower_param_bounds(bounds, RelaxedBoundPolicy::Allowed, itctx)
1693+
this.lower_param_bounds(
1694+
bounds,
1695+
RelaxedBoundPolicy::Allowed(&mut Default::default()),
1696+
itctx,
1697+
)
16801698
})
16811699
}
16821700

@@ -1970,7 +1988,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19701988
fn lower_param_bound(
19711989
&mut self,
19721990
tpb: &GenericBound,
1973-
rbp: RelaxedBoundPolicy,
1991+
rbp: RelaxedBoundPolicy<'_>,
19741992
itctx: ImplTraitContext,
19751993
) -> hir::GenericBound<'hir> {
19761994
match tpb {
@@ -2209,7 +2227,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22092227
fn lower_poly_trait_ref(
22102228
&mut self,
22112229
PolyTraitRef { bound_generic_params, modifiers, trait_ref, span, parens: _ }: &PolyTraitRef,
2212-
rbp: RelaxedBoundPolicy,
2230+
rbp: RelaxedBoundPolicy<'_>,
22132231
itctx: ImplTraitContext,
22142232
) -> hir::PolyTraitRef<'hir> {
22152233
let bound_generic_params =
@@ -2233,7 +2251,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22332251
&self,
22342252
trait_ref: hir::TraitRef<'_>,
22352253
span: Span,
2236-
rbp: RelaxedBoundPolicy,
2254+
rbp: RelaxedBoundPolicy<'_>,
22372255
) {
22382256
// Even though feature `more_maybe_bounds` enables the user to relax all default bounds
22392257
// other than `Sized` in a lot more positions (thereby bypassing the given policy), we don't
@@ -2245,7 +2263,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
22452263
// question: E.g., `?Sized` & `?Move` most likely won't be allowed in all the same places).
22462264

22472265
match rbp {
2248-
RelaxedBoundPolicy::Allowed => return,
2266+
RelaxedBoundPolicy::Allowed(dedup_map) => {
2267+
// `trait_def_id` only returns `None` for errors during resolution.
2268+
let Some(trait_def_id) = trait_ref.trait_def_id() else { return };
2269+
let tcx = self.tcx;
2270+
let err = |s| {
2271+
let name = tcx.item_name(trait_def_id);
2272+
tcx.dcx()
2273+
.struct_span_err(
2274+
vec![span, s],
2275+
format!("duplicate relaxed `{name}` bounds"),
2276+
)
2277+
.with_code(E0203)
2278+
.emit();
2279+
};
2280+
dedup_map.entry(trait_def_id).and_modify(|&mut s| err(s)).or_insert(span);
2281+
return;
2282+
}
22492283
RelaxedBoundPolicy::Forbidden(reason) => {
22502284
let gate = |context, subject| {
22512285
let extended = self.tcx.features().more_maybe_bounds();
@@ -2307,7 +2341,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
23072341
fn lower_param_bounds(
23082342
&mut self,
23092343
bounds: &[GenericBound],
2310-
rbp: RelaxedBoundPolicy,
2344+
rbp: RelaxedBoundPolicy<'_>,
23112345
itctx: ImplTraitContext,
23122346
) -> hir::GenericBounds<'hir> {
23132347
self.arena.alloc_from_iter(self.lower_param_bounds_mut(bounds, rbp, itctx))
@@ -2316,10 +2350,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
23162350
fn lower_param_bounds_mut(
23172351
&mut self,
23182352
bounds: &[GenericBound],
2319-
rbp: RelaxedBoundPolicy,
2353+
mut rbp: RelaxedBoundPolicy<'_>,
23202354
itctx: ImplTraitContext,
23212355
) -> impl Iterator<Item = hir::GenericBound<'hir>> {
2322-
bounds.iter().map(move |bound| self.lower_param_bound(bound, rbp, itctx))
2356+
bounds.iter().map(move |bound| self.lower_param_bound(bound, rbp.reborrow(), itctx))
23232357
}
23242358

23252359
#[instrument(level = "debug", skip(self), ret)]
@@ -2353,7 +2387,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
23532387
bounds,
23542388
/* colon_span */ None,
23552389
span,
2356-
RelaxedBoundPolicy::Allowed,
2390+
RelaxedBoundPolicy::Allowed(&mut Default::default()),
23572391
ImplTraitContext::Universal,
23582392
hir::PredicateOrigin::ImplTrait,
23592393
);

0 commit comments

Comments
 (0)