Skip to content

Commit

Permalink
Auto merge of rust-lang#89550 - lcnr:coherence-specialization, r=niko…
Browse files Browse the repository at this point in the history
…matsakis

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`
  • Loading branch information
bors committed Nov 11, 2021
2 parents 9dbbbb1 + f55ff41 commit 1d34cb4
Show file tree
Hide file tree
Showing 28 changed files with 501 additions and 497 deletions.
13 changes: 8 additions & 5 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,14 @@ rustc_queries! {
desc { "check for overlap between inherent impls defined in this crate" }
}

/// Checks whether all impls in the crate pass the overlap check, returning
/// which impls fail it. If all impls are correct, the returned slice is empty.
query orphan_check_crate(_: ()) -> &'tcx [LocalDefId] {
desc {
"checking whether the immpl in the this crate follow the orphan rules",
}
}

/// Check whether the function has any recursion that could cause the inliner to trigger
/// a cycle. Returns the call stack causing the cycle. The call stack does not contain the
/// current function, just all intermediate functions.
Expand Down Expand Up @@ -1056,11 +1064,6 @@ rustc_queries! {
}

/// Return all `impl` blocks in the current crate.
///
/// To allow caching this between crates, you must pass in [`LOCAL_CRATE`] as the crate number.
/// Passing in any other crate will cause an ICE.
///
/// [`LOCAL_CRATE`]: rustc_hir::def_id::LOCAL_CRATE
query all_local_trait_impls(_: ()) -> &'tcx BTreeMap<DefId, Vec<LocalDefId>> {
desc { "local trait impls" }
}
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_trait_selection/src/traits/specialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ pub(super) fn specialization_graph_provider(
sg
}

// This function is only used when
// encountering errors and inlining
// it negatively impacts perf.
#[cold]
#[inline(never)]
fn report_overlap_conflict(
tcx: TyCtxt<'_>,
overlap: OverlapError,
Expand Down Expand Up @@ -443,8 +448,12 @@ fn report_conflicting_impls(
match used_to_be_allowed {
None => {
sg.has_errored = true;
let err = struct_span_err!(tcx.sess, impl_span, E0119, "");
decorate(LintDiagnosticBuilder::new(err));
if overlap.with_impl.is_local() || !tcx.orphan_check_crate(()).contains(&impl_def_id) {
let err = struct_span_err!(tcx.sess, impl_span, E0119, "");
decorate(LintDiagnosticBuilder::new(err));
} else {
tcx.sess.delay_span_bug(impl_span, "impl should have failed the orphan check");
}
}
Some(kind) => {
let lint = match kind {
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_typeck/src/coherence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ pub fn provide(providers: &mut Providers) {
use self::builtin::coerce_unsized_info;
use self::inherent_impls::{crate_inherent_impls, inherent_impls};
use self::inherent_impls_overlap::crate_inherent_impls_overlap_check;
use self::orphan::orphan_check_crate;

*providers = Providers {
coherent_trait,
crate_inherent_impls,
inherent_impls,
crate_inherent_impls_overlap_check,
coerce_unsized_info,
orphan_check_crate,
..*providers
};
}
Expand All @@ -195,13 +197,13 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) {
}

pub fn check_coherence(tcx: TyCtxt<'_>) {
tcx.sess.time("unsafety_checking", || unsafety::check(tcx));
tcx.ensure().orphan_check_crate(());

for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
tcx.ensure().coherent_trait(trait_def_id);
}

tcx.sess.time("unsafety_checking", || unsafety::check(tcx));
tcx.sess.time("orphan_checking", || orphan::check(tcx));

// these queries are executed for side-effects (error reporting):
tcx.ensure().crate_inherent_impls(());
tcx.ensure().crate_inherent_impls_overlap_check(());
Expand Down
Loading

0 comments on commit 1d34cb4

Please sign in to comment.