Skip to content

Commit d30c392

Browse files
committedOct 5, 2024
Auto merge of rust-lang#131275 - workingjubilee:rollup-4yxqio3, r=workingjubilee
Rollup of 9 pull requests Successful merges: - rust-lang#129517 (Compute array length from type for unconditional panic lint. ) - rust-lang#130367 (Check elaborated projections from dyn don't mention unconstrained late bound lifetimes) - rust-lang#130403 (Stabilize `const_slice_from_raw_parts_mut`) - rust-lang#130633 (Add support for reborrowing pinned method receivers) - rust-lang#131105 (update `Literal`'s intro) - rust-lang#131194 (Fix needless_lifetimes in stable_mir) - rust-lang#131260 (rustdoc: cleaner errors on disambiguator/namespace mismatches) - rust-lang#131267 (Stabilize `BufRead::skip_until`) - rust-lang#131273 (Account for `impl Trait {` when `impl Trait for Type {` was intended) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 495f75a + 08689af commit d30c392

Some content is hidden

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

43 files changed

+559
-94
lines changed
 

‎compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_compatibility.rs

+64-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::ty::{
1313
use rustc_span::{ErrorGuaranteed, Span};
1414
use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
1515
use rustc_trait_selection::traits::{self, hir_ty_lowering_dyn_compatibility_violations};
16+
use rustc_type_ir::elaborate::ClauseWithSupertraitSpan;
1617
use smallvec::{SmallVec, smallvec};
1718
use tracing::{debug, instrument};
1819

@@ -124,16 +125,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
124125
.into_iter()
125126
.filter(|(trait_ref, _)| !tcx.trait_is_auto(trait_ref.def_id()));
126127

127-
for (base_trait_ref, span) in regular_traits_refs_spans {
128+
for (base_trait_ref, original_span) in regular_traits_refs_spans {
128129
let base_pred: ty::Predicate<'tcx> = base_trait_ref.upcast(tcx);
129-
for pred in traits::elaborate(tcx, [base_pred]).filter_only_self() {
130+
for ClauseWithSupertraitSpan { pred, original_span, supertrait_span } in
131+
traits::elaborate(tcx, [ClauseWithSupertraitSpan::new(base_pred, original_span)])
132+
.filter_only_self()
133+
{
130134
debug!("observing object predicate `{pred:?}`");
131135

132136
let bound_predicate = pred.kind();
133137
match bound_predicate.skip_binder() {
134138
ty::PredicateKind::Clause(ty::ClauseKind::Trait(pred)) => {
135139
let pred = bound_predicate.rebind(pred);
136-
associated_types.entry(span).or_default().extend(
140+
associated_types.entry(original_span).or_default().extend(
137141
tcx.associated_items(pred.def_id())
138142
.in_definition_order()
139143
.filter(|item| item.kind == ty::AssocKind::Type)
@@ -172,8 +176,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
172176
// the discussion in #56288 for alternatives.
173177
if !references_self {
174178
// Include projections defined on supertraits.
175-
projection_bounds.push((pred, span));
179+
projection_bounds.push((pred, original_span));
176180
}
181+
182+
self.check_elaborated_projection_mentions_input_lifetimes(
183+
pred,
184+
original_span,
185+
supertrait_span,
186+
);
177187
}
178188
_ => (),
179189
}
@@ -360,6 +370,56 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
360370

361371
Ty::new_dynamic(tcx, existential_predicates, region_bound, representation)
362372
}
373+
374+
/// Check that elaborating the principal of a trait ref doesn't lead to projections
375+
/// that are unconstrained. This can happen because an otherwise unconstrained
376+
/// *type variable* can be substituted with a type that has late-bound regions. See
377+
/// `elaborated-predicates-unconstrained-late-bound.rs` for a test.
378+
fn check_elaborated_projection_mentions_input_lifetimes(
379+
&self,
380+
pred: ty::PolyProjectionPredicate<'tcx>,
381+
span: Span,
382+
supertrait_span: Span,
383+
) {
384+
let tcx = self.tcx();
385+
386+
// Find any late-bound regions declared in `ty` that are not
387+
// declared in the trait-ref or assoc_item. These are not well-formed.
388+
//
389+
// Example:
390+
//
391+
// for<'a> <T as Iterator>::Item = &'a str // <-- 'a is bad
392+
// for<'a> <T as FnMut<(&'a u32,)>>::Output = &'a str // <-- 'a is ok
393+
let late_bound_in_projection_term =
394+
tcx.collect_constrained_late_bound_regions(pred.map_bound(|pred| pred.projection_term));
395+
let late_bound_in_term =
396+
tcx.collect_referenced_late_bound_regions(pred.map_bound(|pred| pred.term));
397+
debug!(?late_bound_in_projection_term);
398+
debug!(?late_bound_in_term);
399+
400+
// FIXME: point at the type params that don't have appropriate lifetimes:
401+
// struct S1<F: for<'a> Fn(&i32, &i32) -> &'a i32>(F);
402+
// ---- ---- ^^^^^^^
403+
// NOTE(associated_const_equality): This error should be impossible to trigger
404+
// with associated const equality constraints.
405+
self.validate_late_bound_regions(
406+
late_bound_in_projection_term,
407+
late_bound_in_term,
408+
|br_name| {
409+
let item_name = tcx.item_name(pred.projection_def_id());
410+
struct_span_code_err!(
411+
self.dcx(),
412+
span,
413+
E0582,
414+
"binding for associated type `{}` references {}, \
415+
which does not appear in the trait input types",
416+
item_name,
417+
br_name
418+
)
419+
.with_span_label(supertrait_span, "due to this supertrait")
420+
},
421+
);
422+
}
363423
}
364424

365425
fn replace_dummy_self_with_error<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(

‎compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
108108
let tcx = self.tcx();
109109
let parent_id = tcx.hir().get_parent_item(self_ty.hir_id).def_id;
110110
if let hir::Node::Item(hir::Item {
111-
kind:
112-
hir::ItemKind::Impl(hir::Impl {
113-
self_ty: impl_self_ty,
114-
of_trait: Some(of_trait_ref),
115-
generics,
116-
..
117-
}),
111+
kind: hir::ItemKind::Impl(hir::Impl { self_ty: impl_self_ty, of_trait, generics, .. }),
118112
..
119113
}) = tcx.hir_node_by_def_id(parent_id)
120114
&& self_ty.hir_id == impl_self_ty.hir_id
121115
{
116+
let Some(of_trait_ref) = of_trait else {
117+
diag.span_suggestion_verbose(
118+
impl_self_ty.span.shrink_to_hi(),
119+
"you might have intended to implement this trait for a given type",
120+
format!(" for /* Type */"),
121+
Applicability::HasPlaceholders,
122+
);
123+
return;
124+
};
122125
if !of_trait_ref.trait_def_id().is_some_and(|def_id| def_id.is_local()) {
123126
return;
124127
}

0 commit comments

Comments
 (0)