Skip to content

Commit 8c74a5d

Browse files
committed
Auto merge of rust-lang#112625 - matthiaskrgr:rollup-jcobj3g, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - rust-lang#112584 (loongarch64-none*: Remove environment component from llvm target) - rust-lang#112600 (Introduce a `Stable` trait to translate MIR to SMIR) - rust-lang#112605 (Improve docs/clean up negative overlap functions) - rust-lang#112611 (Error on unconstrained lifetime in RPITIT) - rust-lang#112612 (Fix explicit-outlives-requirements lint span) - rust-lang#112613 (Fix rustdoc-gui tests on Windows) - rust-lang#112620 (Fix small typo) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 0b475c7 + 3616388 commit 8c74a5d

25 files changed

+532
-407
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+38-21
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ pub(super) fn compare_impl_method<'tcx>(
4545
debug!("compare_impl_method(impl_trait_ref={:?})", impl_trait_ref);
4646

4747
let _: Result<_, ErrorGuaranteed> = try {
48-
compare_self_type(tcx, impl_m, trait_m, impl_trait_ref)?;
49-
compare_number_of_generics(tcx, impl_m, trait_m, false)?;
50-
compare_generic_param_kinds(tcx, impl_m, trait_m, false)?;
51-
compare_number_of_method_arguments(tcx, impl_m, trait_m)?;
52-
compare_synthetic_generics(tcx, impl_m, trait_m)?;
53-
compare_asyncness(tcx, impl_m, trait_m)?;
48+
check_method_is_structurally_compatible(tcx, impl_m, trait_m, impl_trait_ref, false)?;
5449
compare_method_predicate_entailment(
5550
tcx,
5651
impl_m,
@@ -61,6 +56,26 @@ pub(super) fn compare_impl_method<'tcx>(
6156
};
6257
}
6358

59+
/// Checks a bunch of different properties of the impl/trait methods for
60+
/// compatibility, such as asyncness, number of argument, self receiver kind,
61+
/// and number of early- and late-bound generics.
62+
fn check_method_is_structurally_compatible<'tcx>(
63+
tcx: TyCtxt<'tcx>,
64+
impl_m: ty::AssocItem,
65+
trait_m: ty::AssocItem,
66+
impl_trait_ref: ty::TraitRef<'tcx>,
67+
delay: bool,
68+
) -> Result<(), ErrorGuaranteed> {
69+
compare_self_type(tcx, impl_m, trait_m, impl_trait_ref, delay)?;
70+
compare_number_of_generics(tcx, impl_m, trait_m, delay)?;
71+
compare_generic_param_kinds(tcx, impl_m, trait_m, delay)?;
72+
compare_number_of_method_arguments(tcx, impl_m, trait_m, delay)?;
73+
compare_synthetic_generics(tcx, impl_m, trait_m, delay)?;
74+
compare_asyncness(tcx, impl_m, trait_m, delay)?;
75+
check_region_bounds_on_impl_item(tcx, impl_m, trait_m, delay)?;
76+
Ok(())
77+
}
78+
6479
/// This function is best explained by example. Consider a trait with it's implementation:
6580
///
6681
/// ```rust
@@ -177,9 +192,6 @@ fn compare_method_predicate_entailment<'tcx>(
177192
let impl_m_predicates = tcx.predicates_of(impl_m.def_id);
178193
let trait_m_predicates = tcx.predicates_of(trait_m.def_id);
179194

180-
// Check region bounds.
181-
check_region_bounds_on_impl_item(tcx, impl_m, trait_m, false)?;
182-
183195
// Create obligations for each predicate declared by the impl
184196
// definition in the context of the trait's parameter
185197
// environment. We can't just use `impl_env.caller_bounds`,
@@ -534,6 +546,7 @@ fn compare_asyncness<'tcx>(
534546
tcx: TyCtxt<'tcx>,
535547
impl_m: ty::AssocItem,
536548
trait_m: ty::AssocItem,
549+
delay: bool,
537550
) -> Result<(), ErrorGuaranteed> {
538551
if tcx.asyncness(trait_m.def_id) == hir::IsAsync::Async {
539552
match tcx.fn_sig(impl_m.def_id).skip_binder().skip_binder().output().kind() {
@@ -544,11 +557,14 @@ fn compare_asyncness<'tcx>(
544557
// We don't know if it's ok, but at least it's already an error.
545558
}
546559
_ => {
547-
return Err(tcx.sess.emit_err(crate::errors::AsyncTraitImplShouldBeAsync {
548-
span: tcx.def_span(impl_m.def_id),
549-
method_name: trait_m.name,
550-
trait_item_span: tcx.hir().span_if_local(trait_m.def_id),
551-
}));
560+
return Err(tcx
561+
.sess
562+
.create_err(crate::errors::AsyncTraitImplShouldBeAsync {
563+
span: tcx.def_span(impl_m.def_id),
564+
method_name: trait_m.name,
565+
trait_item_span: tcx.hir().span_if_local(trait_m.def_id),
566+
})
567+
.emit_unless(delay));
552568
}
553569
};
554570
}
@@ -602,9 +618,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
602618

603619
// First, check a few of the same things as `compare_impl_method`,
604620
// just so we don't ICE during substitution later.
605-
compare_number_of_generics(tcx, impl_m, trait_m, true)?;
606-
compare_generic_param_kinds(tcx, impl_m, trait_m, true)?;
607-
check_region_bounds_on_impl_item(tcx, impl_m, trait_m, true)?;
621+
check_method_is_structurally_compatible(tcx, impl_m, trait_m, impl_trait_ref, true)?;
608622

609623
let trait_to_impl_substs = impl_trait_ref.substs;
610624

@@ -1097,6 +1111,7 @@ fn compare_self_type<'tcx>(
10971111
impl_m: ty::AssocItem,
10981112
trait_m: ty::AssocItem,
10991113
impl_trait_ref: ty::TraitRef<'tcx>,
1114+
delay: bool,
11001115
) -> Result<(), ErrorGuaranteed> {
11011116
// Try to give more informative error messages about self typing
11021117
// mismatches. Note that any mismatch will also be detected
@@ -1145,7 +1160,7 @@ fn compare_self_type<'tcx>(
11451160
} else {
11461161
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
11471162
}
1148-
return Err(err.emit());
1163+
return Err(err.emit_unless(delay));
11491164
}
11501165

11511166
(true, false) => {
@@ -1166,7 +1181,7 @@ fn compare_self_type<'tcx>(
11661181
err.note_trait_signature(trait_m.name, trait_m.signature(tcx));
11671182
}
11681183

1169-
return Err(err.emit());
1184+
return Err(err.emit_unless(delay));
11701185
}
11711186
}
11721187

@@ -1352,6 +1367,7 @@ fn compare_number_of_method_arguments<'tcx>(
13521367
tcx: TyCtxt<'tcx>,
13531368
impl_m: ty::AssocItem,
13541369
trait_m: ty::AssocItem,
1370+
delay: bool,
13551371
) -> Result<(), ErrorGuaranteed> {
13561372
let impl_m_fty = tcx.fn_sig(impl_m.def_id);
13571373
let trait_m_fty = tcx.fn_sig(trait_m.def_id);
@@ -1422,7 +1438,7 @@ fn compare_number_of_method_arguments<'tcx>(
14221438
),
14231439
);
14241440

1425-
return Err(err.emit());
1441+
return Err(err.emit_unless(delay));
14261442
}
14271443

14281444
Ok(())
@@ -1432,6 +1448,7 @@ fn compare_synthetic_generics<'tcx>(
14321448
tcx: TyCtxt<'tcx>,
14331449
impl_m: ty::AssocItem,
14341450
trait_m: ty::AssocItem,
1451+
delay: bool,
14351452
) -> Result<(), ErrorGuaranteed> {
14361453
// FIXME(chrisvittal) Clean up this function, list of FIXME items:
14371454
// 1. Better messages for the span labels
@@ -1551,7 +1568,7 @@ fn compare_synthetic_generics<'tcx>(
15511568
}
15521569
_ => unreachable!(),
15531570
}
1554-
error_found = Some(err.emit());
1571+
error_found = Some(err.emit_unless(delay));
15551572
}
15561573
}
15571574
if let Some(reported) = error_found { Err(reported) } else { Ok(()) }

compiler/rustc_hir_analysis/src/impl_wf_check.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,23 @@ fn enforce_impl_params_are_constrained(tcx: TyCtxt<'_>, impl_def_id: LocalDefId)
106106
if item.defaultness(tcx).has_value() {
107107
cgp::parameters_for(&tcx.type_of(def_id).subst_identity(), true)
108108
} else {
109-
Vec::new()
109+
vec![]
110110
}
111111
}
112-
ty::AssocKind::Fn | ty::AssocKind::Const => Vec::new(),
112+
ty::AssocKind::Fn => {
113+
if !tcx.lower_impl_trait_in_trait_to_assoc_ty()
114+
&& item.defaultness(tcx).has_value()
115+
&& tcx.impl_method_has_trait_impl_trait_tys(item.def_id)
116+
&& let Ok(table) = tcx.collect_return_position_impl_trait_in_trait_tys(def_id)
117+
{
118+
table.values().copied().flat_map(|ty| {
119+
cgp::parameters_for(&ty.subst_identity(), true)
120+
}).collect()
121+
} else {
122+
vec![]
123+
}
124+
}
125+
ty::AssocKind::Const => vec![],
113126
}
114127
})
115128
.collect();

compiler/rustc_lint/src/builtin.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -2124,12 +2124,16 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
21242124
}
21252125

21262126
let ty_generics = cx.tcx.generics_of(def_id);
2127+
let num_where_predicates = hir_generics
2128+
.predicates
2129+
.iter()
2130+
.filter(|predicate| predicate.in_where_clause())
2131+
.count();
21272132

21282133
let mut bound_count = 0;
21292134
let mut lint_spans = Vec::new();
21302135
let mut where_lint_spans = Vec::new();
2131-
let mut dropped_predicate_count = 0;
2132-
let num_predicates = hir_generics.predicates.len();
2136+
let mut dropped_where_predicate_count = 0;
21332137
for (i, where_predicate) in hir_generics.predicates.iter().enumerate() {
21342138
let (relevant_lifetimes, bounds, predicate_span, in_where_clause) =
21352139
match where_predicate {
@@ -2186,8 +2190,8 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
21862190
bound_count += bound_spans.len();
21872191

21882192
let drop_predicate = bound_spans.len() == bounds.len();
2189-
if drop_predicate {
2190-
dropped_predicate_count += 1;
2193+
if drop_predicate && in_where_clause {
2194+
dropped_where_predicate_count += 1;
21912195
}
21922196

21932197
if drop_predicate {
@@ -2196,7 +2200,7 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
21962200
} else if predicate_span.from_expansion() {
21972201
// Don't try to extend the span if it comes from a macro expansion.
21982202
where_lint_spans.push(predicate_span);
2199-
} else if i + 1 < num_predicates {
2203+
} else if i + 1 < num_where_predicates {
22002204
// If all the bounds on a predicate were inferable and there are
22012205
// further predicates, we want to eat the trailing comma.
22022206
let next_predicate_span = hir_generics.predicates[i + 1].span();
@@ -2224,9 +2228,10 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitOutlivesRequirements {
22242228
}
22252229
}
22262230

2227-
// If all predicates are inferable, drop the entire clause
2231+
// If all predicates in where clause are inferable, drop the entire clause
22282232
// (including the `where`)
2229-
if hir_generics.has_where_clause_predicates && dropped_predicate_count == num_predicates
2233+
if hir_generics.has_where_clause_predicates
2234+
&& dropped_where_predicate_count == num_where_predicates
22302235
{
22312236
let where_span = hir_generics.where_clause_span;
22322237
// Extend the where clause back to the closing `>` of the

0 commit comments

Comments
 (0)