Skip to content

Commit 1c580bc

Browse files
committed
Auto merge of rust-lang#122139 - GuillaumeGomez:rollup-37vtwsc, r=GuillaumeGomez
Rollup of 10 pull requests Successful merges: - rust-lang#121863 (silence mismatched types errors for implied projections) - rust-lang#122043 (Apply `EarlyBinder` only to `TraitRef` in `ImplTraitHeader`) - rust-lang#122066 (Add proper cfgs for struct HirIdValidator used only with debug-assert) - rust-lang#122104 (Rust is a proper name: rust → Rust) - rust-lang#122110 (Make `x t miri` respect `MIRI_TEMP`) - rust-lang#122114 (Make not finding core a fatal error) - rust-lang#122115 (Cancel parsing ever made during recovery) - rust-lang#122123 (Don't require specifying unrelated assoc types when trait alias is in `dyn` type) - rust-lang#122126 (Fix `tidy --bless` on ̶X̶e̶n̶i̶x̶ Windows) - rust-lang#122129 (Set `RustcDocs` to only run on host) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 52f8aec + ce9a6ad commit 1c580bc

File tree

62 files changed

+547
-315
lines changed

Some content is hidden

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

62 files changed

+547
-315
lines changed

Diff for: compiler/rustc_hir_analysis/src/astconv/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1612,10 +1612,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16121612
.any(|impl_def_id| {
16131613
let impl_header = tcx.impl_trait_header(impl_def_id);
16141614
impl_header.is_some_and(|header| {
1615-
let header = header.instantiate(
1615+
let trait_ref = header.trait_ref.instantiate(
16161616
tcx,
16171617
infcx.fresh_args_for_item(DUMMY_SP, impl_def_id),
16181618
);
1619+
16191620
let value = tcx.fold_regions(qself_ty, |_, _| tcx.lifetimes.re_erased);
16201621
// FIXME: Don't bother dealing with non-lifetime binders here...
16211622
if value.has_escaping_bound_vars() {
@@ -1624,7 +1625,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16241625
infcx
16251626
.can_eq(
16261627
ty::ParamEnv::empty(),
1627-
header.trait_ref.self_ty(),
1628+
trait_ref.self_ty(),
16281629
value,
16291630
) && header.polarity != ty::ImplPolarity::Negative
16301631
})
@@ -1677,9 +1678,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
16771678
.filter(|header| {
16781679
// Consider only accessible traits
16791680
tcx.visibility(trait_def_id).is_accessible_from(self.item_def_id(), tcx)
1680-
&& header.skip_binder().polarity != ty::ImplPolarity::Negative
1681+
&& header.polarity != ty::ImplPolarity::Negative
16811682
})
1682-
.map(|header| header.instantiate_identity().trait_ref.self_ty())
1683+
.map(|header| header.trait_ref.instantiate_identity().self_ty())
16831684
// We don't care about blanket impls.
16841685
.filter(|self_ty| !self_ty.has_non_region_param())
16851686
.map(|self_ty| tcx.erase_regions(self_ty).to_string())

Diff for: compiler/rustc_hir_analysis/src/astconv/object_safety.rs

+36-44
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
4545
dummy_self,
4646
&mut bounds,
4747
false,
48-
// FIXME: This should be `true`, but we don't really handle
49-
// associated type bounds or type aliases in objects in a way
50-
// that makes this meaningful, I think.
51-
OnlySelfBounds(false),
48+
// True so we don't populate `bounds` with associated type bounds, even
49+
// though they're disallowed from object types.
50+
OnlySelfBounds(true),
5251
) {
5352
potential_assoc_types.extend(cur_potential_assoc_types);
5453
}
@@ -83,9 +82,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
8382
let expanded_traits =
8483
traits::expand_trait_aliases(tcx, trait_bounds.iter().map(|&(a, b)| (a, b)));
8584

86-
let (mut auto_traits, regular_traits): (Vec<_>, Vec<_>) = expanded_traits
87-
.filter(|i| i.trait_ref().self_ty().skip_binder() == dummy_self)
88-
.partition(|i| tcx.trait_is_auto(i.trait_ref().def_id()));
85+
let (mut auto_traits, regular_traits): (Vec<_>, Vec<_>) =
86+
expanded_traits.partition(|i| tcx.trait_is_auto(i.trait_ref().def_id()));
8987
if regular_traits.len() > 1 {
9088
let first_trait = &regular_traits[0];
9189
let additional_trait = &regular_traits[1];
@@ -158,7 +156,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
158156

159157
for (base_trait_ref, span) in regular_traits_refs_spans {
160158
let base_pred: ty::Predicate<'tcx> = base_trait_ref.to_predicate(tcx);
161-
for pred in traits::elaborate(tcx, [base_pred]) {
159+
for pred in traits::elaborate(tcx, [base_pred]).filter_only_self() {
162160
debug!("conv_object_ty_poly_trait_ref: observing object predicate `{:?}`", pred);
163161

164162
let bound_predicate = pred.kind();
@@ -312,45 +310,39 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
312310
})
313311
});
314312

315-
let existential_projections = projection_bounds
316-
.iter()
317-
// We filter out traits that don't have `Self` as their self type above,
318-
// we need to do the same for projections.
319-
.filter(|(bound, _)| bound.skip_binder().self_ty() == dummy_self)
320-
.map(|(bound, _)| {
321-
bound.map_bound(|mut b| {
322-
assert_eq!(b.projection_ty.self_ty(), dummy_self);
313+
let existential_projections = projection_bounds.iter().map(|(bound, _)| {
314+
bound.map_bound(|mut b| {
315+
assert_eq!(b.projection_ty.self_ty(), dummy_self);
323316

324-
// Like for trait refs, verify that `dummy_self` did not leak inside default type
325-
// parameters.
326-
let references_self = b.projection_ty.args.iter().skip(1).any(|arg| {
327-
if arg.walk().any(|arg| arg == dummy_self.into()) {
328-
return true;
329-
}
330-
false
331-
});
332-
if references_self {
333-
let guar = tcx.dcx().span_delayed_bug(
334-
span,
335-
"trait object projection bounds reference `Self`",
336-
);
337-
let args: Vec<_> = b
338-
.projection_ty
339-
.args
340-
.iter()
341-
.map(|arg| {
342-
if arg.walk().any(|arg| arg == dummy_self.into()) {
343-
return Ty::new_error(tcx, guar).into();
344-
}
345-
arg
346-
})
347-
.collect();
348-
b.projection_ty.args = tcx.mk_args(&args);
317+
// Like for trait refs, verify that `dummy_self` did not leak inside default type
318+
// parameters.
319+
let references_self = b.projection_ty.args.iter().skip(1).any(|arg| {
320+
if arg.walk().any(|arg| arg == dummy_self.into()) {
321+
return true;
349322
}
323+
false
324+
});
325+
if references_self {
326+
let guar = tcx
327+
.dcx()
328+
.span_delayed_bug(span, "trait object projection bounds reference `Self`");
329+
let args: Vec<_> = b
330+
.projection_ty
331+
.args
332+
.iter()
333+
.map(|arg| {
334+
if arg.walk().any(|arg| arg == dummy_self.into()) {
335+
return Ty::new_error(tcx, guar).into();
336+
}
337+
arg
338+
})
339+
.collect();
340+
b.projection_ty.args = tcx.mk_args(&args);
341+
}
350342

351-
ty::ExistentialProjection::erase_self_ty(tcx, b)
352-
})
353-
});
343+
ty::ExistentialProjection::erase_self_ty(tcx, b)
344+
})
345+
});
354346

355347
let regular_trait_predicates = existential_trait_refs
356348
.map(|trait_ref| trait_ref.map_bound(ty::ExistentialPredicate::Trait));

Diff for: compiler/rustc_hir_analysis/src/check/check.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -530,11 +530,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
530530
}
531531
DefKind::Impl { of_trait } => {
532532
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {
533-
check_impl_items_against_trait(
534-
tcx,
535-
def_id,
536-
impl_trait_header.instantiate_identity(),
537-
);
533+
check_impl_items_against_trait(tcx, def_id, impl_trait_header);
538534
check_on_unimplemented(tcx, def_id);
539535
}
540536
}
@@ -725,10 +721,11 @@ fn check_impl_items_against_trait<'tcx>(
725721
impl_id: LocalDefId,
726722
impl_trait_header: ty::ImplTraitHeader<'tcx>,
727723
) {
724+
let trait_ref = impl_trait_header.trait_ref.instantiate_identity();
728725
// If the trait reference itself is erroneous (so the compilation is going
729726
// to fail), skip checking the items here -- the `impl_item` table in `tcx`
730727
// isn't populated for such impls.
731-
if impl_trait_header.references_error() {
728+
if trait_ref.references_error() {
732729
return;
733730
}
734731

@@ -752,7 +749,7 @@ fn check_impl_items_against_trait<'tcx>(
752749
}
753750
}
754751

755-
let trait_def = tcx.trait_def(impl_trait_header.trait_ref.def_id);
752+
let trait_def = tcx.trait_def(trait_ref.def_id);
756753

757754
for &impl_item in impl_item_refs {
758755
let ty_impl_item = tcx.associated_item(impl_item);
@@ -771,10 +768,10 @@ fn check_impl_items_against_trait<'tcx>(
771768
));
772769
}
773770
ty::AssocKind::Fn => {
774-
compare_impl_method(tcx, ty_impl_item, ty_trait_item, impl_trait_header.trait_ref);
771+
compare_impl_method(tcx, ty_impl_item, ty_trait_item, trait_ref);
775772
}
776773
ty::AssocKind::Type => {
777-
compare_impl_ty(tcx, ty_impl_item, ty_trait_item, impl_trait_header.trait_ref);
774+
compare_impl_ty(tcx, ty_impl_item, ty_trait_item, trait_ref);
778775
}
779776
}
780777

@@ -794,7 +791,7 @@ fn check_impl_items_against_trait<'tcx>(
794791
let mut must_implement_one_of: Option<&[Ident]> =
795792
trait_def.must_implement_one_of.as_deref();
796793

797-
for &trait_item_id in tcx.associated_item_def_ids(impl_trait_header.trait_ref.def_id) {
794+
for &trait_item_id in tcx.associated_item_def_ids(trait_ref.def_id) {
798795
let leaf_def = ancestors.leaf_def(tcx, trait_item_id);
799796

800797
let is_implemented = leaf_def
@@ -872,7 +869,7 @@ fn check_impl_items_against_trait<'tcx>(
872869

873870
if let Some(missing_items) = must_implement_one_of {
874871
let attr_span = tcx
875-
.get_attr(impl_trait_header.trait_ref.def_id, sym::rustc_must_implement_one_of)
872+
.get_attr(trait_ref.def_id, sym::rustc_must_implement_one_of)
876873
.map(|attr| attr.span);
877874

878875
missing_items_must_implement_one_of_err(

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+30-20
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
247247
hir::ItemKind::Impl(impl_) => {
248248
let header = tcx.impl_trait_header(def_id);
249249
let is_auto = header
250-
.is_some_and(|header| tcx.trait_is_auto(header.skip_binder().trait_ref.def_id));
250+
.is_some_and(|header| tcx.trait_is_auto(header.trait_ref.skip_binder().def_id));
251251

252252
crate::impl_wf_check::check_impl_wf(tcx, def_id)?;
253253
let mut res = Ok(());
@@ -261,7 +261,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
261261
.emit());
262262
}
263263
// We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
264-
match header.map(|h| h.skip_binder().polarity) {
264+
match header.map(|h| h.polarity) {
265265
// `None` means this is an inherent impl
266266
Some(ty::ImplPolarity::Positive) | None => {
267267
res = res.and(check_impl(tcx, item, impl_.self_ty, &impl_.of_trait));
@@ -298,31 +298,31 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
298298
hir::ItemKind::Const(ty, ..) => {
299299
check_item_type(tcx, def_id, ty.span, UnsizedHandling::Forbid)
300300
}
301-
hir::ItemKind::Struct(_, ast_generics) => {
301+
hir::ItemKind::Struct(_, hir_generics) => {
302302
let res = check_type_defn(tcx, item, false);
303-
check_variances_for_type_defn(tcx, item, ast_generics);
303+
check_variances_for_type_defn(tcx, item, hir_generics);
304304
res
305305
}
306-
hir::ItemKind::Union(_, ast_generics) => {
306+
hir::ItemKind::Union(_, hir_generics) => {
307307
let res = check_type_defn(tcx, item, true);
308-
check_variances_for_type_defn(tcx, item, ast_generics);
308+
check_variances_for_type_defn(tcx, item, hir_generics);
309309
res
310310
}
311-
hir::ItemKind::Enum(_, ast_generics) => {
311+
hir::ItemKind::Enum(_, hir_generics) => {
312312
let res = check_type_defn(tcx, item, true);
313-
check_variances_for_type_defn(tcx, item, ast_generics);
313+
check_variances_for_type_defn(tcx, item, hir_generics);
314314
res
315315
}
316316
hir::ItemKind::Trait(..) => check_trait(tcx, item),
317317
hir::ItemKind::TraitAlias(..) => check_trait(tcx, item),
318318
// `ForeignItem`s are handled separately.
319319
hir::ItemKind::ForeignMod { .. } => Ok(()),
320-
hir::ItemKind::TyAlias(hir_ty, ast_generics) => {
320+
hir::ItemKind::TyAlias(hir_ty, hir_generics) => {
321321
if tcx.type_alias_is_lazy(item.owner_id) {
322322
// Bounds of lazy type aliases and of eager ones that contain opaque types are respected.
323323
// E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`.
324324
let res = check_item_type(tcx, def_id, hir_ty.span, UnsizedHandling::Allow);
325-
check_variances_for_type_defn(tcx, item, ast_generics);
325+
check_variances_for_type_defn(tcx, item, hir_generics);
326326
res
327327
} else {
328328
Ok(())
@@ -1277,25 +1277,26 @@ fn check_item_type(
12771277
})
12781278
}
12791279

1280-
#[instrument(level = "debug", skip(tcx, ast_self_ty, ast_trait_ref))]
1280+
#[instrument(level = "debug", skip(tcx, hir_self_ty, hir_trait_ref))]
12811281
fn check_impl<'tcx>(
12821282
tcx: TyCtxt<'tcx>,
12831283
item: &'tcx hir::Item<'tcx>,
1284-
ast_self_ty: &hir::Ty<'_>,
1285-
ast_trait_ref: &Option<hir::TraitRef<'_>>,
1284+
hir_self_ty: &hir::Ty<'_>,
1285+
hir_trait_ref: &Option<hir::TraitRef<'_>>,
12861286
) -> Result<(), ErrorGuaranteed> {
12871287
enter_wf_checking_ctxt(tcx, item.span, item.owner_id.def_id, |wfcx| {
1288-
match ast_trait_ref {
1289-
Some(ast_trait_ref) => {
1288+
match hir_trait_ref {
1289+
Some(hir_trait_ref) => {
12901290
// `#[rustc_reservation_impl]` impls are not real impls and
12911291
// therefore don't need to be WF (the trait's `Self: Trait` predicate
12921292
// won't hold).
12931293
let trait_ref = tcx.impl_trait_ref(item.owner_id).unwrap().instantiate_identity();
12941294
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
12951295
// other `Foo` impls are incoherent.
12961296
tcx.ensure().coherent_trait(trait_ref.def_id)?;
1297+
let trait_span = hir_trait_ref.path.span;
12971298
let trait_ref = wfcx.normalize(
1298-
ast_trait_ref.path.span,
1299+
trait_span,
12991300
Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
13001301
trait_ref,
13011302
);
@@ -1306,14 +1307,23 @@ fn check_impl<'tcx>(
13061307
wfcx.param_env,
13071308
wfcx.body_def_id,
13081309
trait_pred,
1309-
ast_trait_ref.path.span,
1310+
trait_span,
13101311
item,
13111312
);
13121313
for obligation in &mut obligations {
1314+
if obligation.cause.span != trait_span {
1315+
// We already have a better span.
1316+
continue;
1317+
}
13131318
if let Some(pred) = obligation.predicate.to_opt_poly_trait_pred()
1314-
&& pred.self_ty().skip_binder() == trait_ref.self_ty()
1319+
&& pred.skip_binder().self_ty() == trait_ref.self_ty()
1320+
{
1321+
obligation.cause.span = hir_self_ty.span;
1322+
}
1323+
if let Some(pred) = obligation.predicate.to_opt_poly_projection_pred()
1324+
&& pred.skip_binder().self_ty() == trait_ref.self_ty()
13151325
{
1316-
obligation.cause.span = ast_self_ty.span;
1326+
obligation.cause.span = hir_self_ty.span;
13171327
}
13181328
}
13191329
debug!(?obligations);
@@ -1327,7 +1337,7 @@ fn check_impl<'tcx>(
13271337
self_ty,
13281338
);
13291339
wfcx.register_wf_obligation(
1330-
ast_self_ty.span,
1340+
hir_self_ty.span,
13311341
Some(WellFormedLoc::Ty(item.hir_id().expect_owner().def_id)),
13321342
self_ty.into(),
13331343
);

Diff for: compiler/rustc_hir_analysis/src/coherence/builtin.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_trait_selection::traits::ObligationCtxt;
2525
use rustc_trait_selection::traits::{self, ObligationCause};
2626
use std::collections::BTreeMap;
2727

28-
pub fn check_trait<'tcx>(
28+
pub(super) fn check_trait<'tcx>(
2929
tcx: TyCtxt<'tcx>,
3030
trait_def_id: DefId,
3131
impl_def_id: LocalDefId,
@@ -66,10 +66,9 @@ impl<'tcx> Checker<'tcx> {
6666

6767
fn visit_implementation_of_drop(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
6868
let tcx = checker.tcx;
69-
let header = checker.impl_header;
7069
let impl_did = checker.impl_def_id;
7170
// Destructors only work on local ADT types.
72-
match header.trait_ref.self_ty().kind() {
71+
match checker.impl_header.trait_ref.instantiate_identity().self_ty().kind() {
7372
ty::Adt(def, _) if def.did().is_local() => return Ok(()),
7473
ty::Error(_) => return Ok(()),
7574
_ => {}
@@ -86,7 +85,7 @@ fn visit_implementation_of_copy(checker: &Checker<'_>) -> Result<(), ErrorGuaran
8685
let impl_did = checker.impl_def_id;
8786
debug!("visit_implementation_of_copy: impl_did={:?}", impl_did);
8887

89-
let self_type = impl_header.trait_ref.self_ty();
88+
let self_type = impl_header.trait_ref.instantiate_identity().self_ty();
9089
debug!("visit_implementation_of_copy: self_type={:?} (bound)", self_type);
9190

9291
let param_env = tcx.param_env(impl_did);
@@ -120,7 +119,7 @@ fn visit_implementation_of_const_param_ty(checker: &Checker<'_>) -> Result<(), E
120119
let tcx = checker.tcx;
121120
let header = checker.impl_header;
122121
let impl_did = checker.impl_def_id;
123-
let self_type = header.trait_ref.self_ty();
122+
let self_type = header.trait_ref.instantiate_identity().self_ty();
124123
assert!(!self_type.has_escaping_bound_vars());
125124

126125
let param_env = tcx.param_env(impl_did);
@@ -157,9 +156,8 @@ fn visit_implementation_of_coerce_unsized(checker: &Checker<'_>) -> Result<(), E
157156

158157
fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<(), ErrorGuaranteed> {
159158
let tcx = checker.tcx;
160-
let header = checker.impl_header;
161159
let impl_did = checker.impl_def_id;
162-
let trait_ref = header.trait_ref;
160+
let trait_ref = checker.impl_header.trait_ref.instantiate_identity();
163161
debug!("visit_implementation_of_dispatch_from_dyn: impl_did={:?}", impl_did);
164162

165163
let span = tcx.def_span(impl_did);

0 commit comments

Comments
 (0)