Skip to content

Commit 1d34cb4

Browse files
committed
Auto merge of rust-lang#89550 - lcnr:coherence-specialization, r=nikomatsakis
do not emit overlap errors for impls failing the orphan check this should finally allow us to merge rust-lang#86986, see rust-lang#86986 (comment) for more details. r? `@nikomatsakis` cc `@eddyb`
2 parents 9dbbbb1 + f55ff41 commit 1d34cb4

28 files changed

+501
-497
lines changed

compiler/rustc_middle/src/query/mod.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,14 @@ rustc_queries! {
822822
desc { "check for overlap between inherent impls defined in this crate" }
823823
}
824824

825+
/// Checks whether all impls in the crate pass the overlap check, returning
826+
/// which impls fail it. If all impls are correct, the returned slice is empty.
827+
query orphan_check_crate(_: ()) -> &'tcx [LocalDefId] {
828+
desc {
829+
"checking whether the immpl in the this crate follow the orphan rules",
830+
}
831+
}
832+
825833
/// Check whether the function has any recursion that could cause the inliner to trigger
826834
/// a cycle. Returns the call stack causing the cycle. The call stack does not contain the
827835
/// current function, just all intermediate functions.
@@ -1056,11 +1064,6 @@ rustc_queries! {
10561064
}
10571065

10581066
/// Return all `impl` blocks in the current crate.
1059-
///
1060-
/// To allow caching this between crates, you must pass in [`LOCAL_CRATE`] as the crate number.
1061-
/// Passing in any other crate will cause an ICE.
1062-
///
1063-
/// [`LOCAL_CRATE`]: rustc_hir::def_id::LOCAL_CRATE
10641067
query all_local_trait_impls(_: ()) -> &'tcx BTreeMap<DefId, Vec<LocalDefId>> {
10651068
desc { "local trait impls" }
10661069
}

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ pub(super) fn specialization_graph_provider(
291291
sg
292292
}
293293

294+
// This function is only used when
295+
// encountering errors and inlining
296+
// it negatively impacts perf.
297+
#[cold]
298+
#[inline(never)]
294299
fn report_overlap_conflict(
295300
tcx: TyCtxt<'_>,
296301
overlap: OverlapError,
@@ -443,8 +448,12 @@ fn report_conflicting_impls(
443448
match used_to_be_allowed {
444449
None => {
445450
sg.has_errored = true;
446-
let err = struct_span_err!(tcx.sess, impl_span, E0119, "");
447-
decorate(LintDiagnosticBuilder::new(err));
451+
if overlap.with_impl.is_local() || !tcx.orphan_check_crate(()).contains(&impl_def_id) {
452+
let err = struct_span_err!(tcx.sess, impl_span, E0119, "");
453+
decorate(LintDiagnosticBuilder::new(err));
454+
} else {
455+
tcx.sess.delay_span_bug(impl_span, "impl should have failed the orphan check");
456+
}
448457
}
449458
Some(kind) => {
450459
let lint = match kind {

compiler/rustc_typeck/src/coherence/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,15 @@ pub fn provide(providers: &mut Providers) {
168168
use self::builtin::coerce_unsized_info;
169169
use self::inherent_impls::{crate_inherent_impls, inherent_impls};
170170
use self::inherent_impls_overlap::crate_inherent_impls_overlap_check;
171+
use self::orphan::orphan_check_crate;
171172

172173
*providers = Providers {
173174
coherent_trait,
174175
crate_inherent_impls,
175176
inherent_impls,
176177
crate_inherent_impls_overlap_check,
177178
coerce_unsized_info,
179+
orphan_check_crate,
178180
..*providers
179181
};
180182
}
@@ -195,13 +197,13 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) {
195197
}
196198

197199
pub fn check_coherence(tcx: TyCtxt<'_>) {
200+
tcx.sess.time("unsafety_checking", || unsafety::check(tcx));
201+
tcx.ensure().orphan_check_crate(());
202+
198203
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
199204
tcx.ensure().coherent_trait(trait_def_id);
200205
}
201206

202-
tcx.sess.time("unsafety_checking", || unsafety::check(tcx));
203-
tcx.sess.time("orphan_checking", || orphan::check(tcx));
204-
205207
// these queries are executed for side-effects (error reporting):
206208
tcx.ensure().crate_inherent_impls(());
207209
tcx.ensure().crate_inherent_impls_overlap_check(());

0 commit comments

Comments
 (0)