Skip to content

Register even erroneous impls #119868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ macro_rules! arena_types {
[] stripped_cfg_items: rustc_ast::expand::StrippedCfgItem,
[] mod_child: rustc_middle::metadata::ModChild,
[] features: rustc_feature::Features,
[decode] specialization_graph: rustc_middle::traits::specialization_graph::Graph,
]);
)
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,8 +1294,7 @@ rustc_queries! {
desc { |tcx| "finding trait impls of `{}`", tcx.def_path_str(trait_id) }
}

query specialization_graph_of(trait_id: DefId) -> &'tcx specialization_graph::Graph {
arena_cache
query specialization_graph_of(trait_id: DefId) -> Result<&'tcx specialization_graph::Graph, ErrorGuaranteed> {
desc { |tcx| "building specialization graph of trait `{}`", tcx.def_path_str(trait_id) }
cache_on_disk_if { true }
}
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,15 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for &'tcx [rustc_ast::InlineAsm
}
}

impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>>
for &'tcx crate::traits::specialization_graph::Graph
{
#[inline]
fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self {
RefDecodable::decode(d)
}
}

macro_rules! impl_ref_decoder {
(<$tcx:tt> $($ty:ty,)*) => {
$(impl<'a, $tcx> Decodable<CacheDecoder<'a, $tcx>> for &$tcx [$ty] {
Expand Down
14 changes: 4 additions & 10 deletions compiler/rustc_middle/src/traits/specialization_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,16 @@ pub struct Graph {

/// The "root" impls are found by looking up the trait's def_id.
pub children: DefIdMap<Children>,

/// Whether an error was emitted while constructing the graph.
pub has_errored: Option<ErrorGuaranteed>,
}

impl Graph {
pub fn new() -> Graph {
Graph { parent: Default::default(), children: Default::default(), has_errored: None }
Graph { parent: Default::default(), children: Default::default() }
}

/// The parent of a given impl, which is the `DefId` of the trait when the
/// impl is a "specialization root".
#[track_caller]
pub fn parent(&self, child: DefId) -> DefId {
*self.parent.get(&child).unwrap_or_else(|| panic!("Failed to get parent for {child:?}"))
}
Expand Down Expand Up @@ -255,13 +253,9 @@ pub fn ancestors(
trait_def_id: DefId,
start_from_impl: DefId,
) -> Result<Ancestors<'_>, ErrorGuaranteed> {
let specialization_graph = tcx.specialization_graph_of(trait_def_id);
let specialization_graph = tcx.specialization_graph_of(trait_def_id)?;

if let Some(reported) = specialization_graph.has_errored {
Err(reported)
} else if let Err(reported) =
tcx.type_of(start_from_impl).instantiate_identity().error_reported()
{
if let Err(reported) = tcx.type_of(start_from_impl).instantiate_identity().error_reported() {
Err(reported)
} else {
Ok(Ancestors {
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_middle/src/ty/fast_reject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub enum SimplifiedType {
CoroutineWitness(DefId),
Function(usize),
Placeholder,
Error,
}

/// Generic parameters are pretty much just bound variables, e.g.
Expand Down Expand Up @@ -153,7 +154,8 @@ pub fn simplify_type<'tcx>(
TreatParams::ForLookup | TreatParams::AsCandidateKey => None,
},
ty::Foreign(def_id) => Some(SimplifiedType::Foreign(def_id)),
ty::Bound(..) | ty::Infer(_) | ty::Error(_) => None,
ty::Error(_) => Some(SimplifiedType::Error),
ty::Bound(..) | ty::Infer(_) => None,
}
}

Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_middle/src/ty/trait_def.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::traits::specialization_graph;
use crate::ty::fast_reject::{self, SimplifiedType, TreatParams, TreatProjections};
use crate::ty::visit::TypeVisitableExt;
use crate::ty::{Ident, Ty, TyCtxt};
use hir::def_id::LOCAL_CRATE;
use rustc_hir as hir;
Expand Down Expand Up @@ -241,9 +240,6 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
let impl_def_id = impl_def_id.to_def_id();

let impl_self_ty = tcx.type_of(impl_def_id).instantiate_identity();
if impl_self_ty.references_error() {
continue;
}

if let Some(simplified_self_ty) =
fast_reject::simplify_type(tcx, impl_self_ty, TreatParams::AsCandidateKey)
Expand Down
68 changes: 34 additions & 34 deletions compiler/rustc_trait_selection/src/traits/specialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use rustc_middle::ty::{self, ImplSubject, Ty, TyCtxt, TypeVisitableExt};
use rustc_middle::ty::{GenericArgs, GenericArgsRef};
use rustc_session::lint::builtin::COHERENCE_LEAK_CHECK;
use rustc_session::lint::builtin::ORDER_DEPENDENT_TRAIT_OBJECTS;
use rustc_span::{Span, DUMMY_SP};
use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};

use super::util;
use super::SelectionContext;
Expand Down Expand Up @@ -258,7 +258,7 @@ fn fulfill_implication<'tcx>(
pub(super) fn specialization_graph_provider(
tcx: TyCtxt<'_>,
trait_id: DefId,
) -> specialization_graph::Graph {
) -> Result<&'_ specialization_graph::Graph, ErrorGuaranteed> {
let mut sg = specialization_graph::Graph::new();
let overlap_mode = specialization_graph::OverlapMode::get(tcx, trait_id);

Expand All @@ -271,6 +271,8 @@ pub(super) fn specialization_graph_provider(
trait_impls
.sort_unstable_by_key(|def_id| (-(def_id.krate.as_u32() as i64), def_id.index.index()));

let mut errored = Ok(());

for impl_def_id in trait_impls {
if let Some(impl_def_id) = impl_def_id.as_local() {
// This is where impl overlap checking happens:
Expand All @@ -283,15 +285,21 @@ pub(super) fn specialization_graph_provider(
};

if let Some(overlap) = overlap {
report_overlap_conflict(tcx, overlap, impl_def_id, used_to_be_allowed, &mut sg);
errored = errored.and(report_overlap_conflict(
tcx,
overlap,
impl_def_id,
used_to_be_allowed,
));
}
} else {
let parent = tcx.impl_parent(impl_def_id).unwrap_or(trait_id);
sg.record_impl_from_cstore(tcx, parent, impl_def_id)
}
}
errored?;

sg
Ok(tcx.arena.alloc(sg))
}

// This function is only used when
Expand All @@ -304,36 +312,31 @@ fn report_overlap_conflict<'tcx>(
overlap: OverlapError<'tcx>,
impl_def_id: LocalDefId,
used_to_be_allowed: Option<FutureCompatOverlapErrorKind>,
sg: &mut specialization_graph::Graph,
) {
) -> Result<(), ErrorGuaranteed> {
let impl_polarity = tcx.impl_polarity(impl_def_id.to_def_id());
let other_polarity = tcx.impl_polarity(overlap.with_impl);
match (impl_polarity, other_polarity) {
(ty::ImplPolarity::Negative, ty::ImplPolarity::Positive) => {
report_negative_positive_conflict(
Err(report_negative_positive_conflict(
tcx,
&overlap,
impl_def_id,
impl_def_id.to_def_id(),
overlap.with_impl,
sg,
);
))
}

(ty::ImplPolarity::Positive, ty::ImplPolarity::Negative) => {
report_negative_positive_conflict(
Err(report_negative_positive_conflict(
tcx,
&overlap,
impl_def_id,
overlap.with_impl,
impl_def_id.to_def_id(),
sg,
);
))
}

_ => {
report_conflicting_impls(tcx, overlap, impl_def_id, used_to_be_allowed, sg);
}
_ => report_conflicting_impls(tcx, overlap, impl_def_id, used_to_be_allowed),
}
}

Expand All @@ -343,25 +346,24 @@ fn report_negative_positive_conflict<'tcx>(
local_impl_def_id: LocalDefId,
negative_impl_def_id: DefId,
positive_impl_def_id: DefId,
sg: &mut specialization_graph::Graph,
) {
let err = tcx.dcx().create_err(NegativePositiveConflict {
impl_span: tcx.def_span(local_impl_def_id),
trait_desc: overlap.trait_ref,
self_ty: overlap.self_ty,
negative_impl_span: tcx.span_of_impl(negative_impl_def_id),
positive_impl_span: tcx.span_of_impl(positive_impl_def_id),
});
sg.has_errored = Some(err.emit());
) -> ErrorGuaranteed {
tcx.dcx()
.create_err(NegativePositiveConflict {
impl_span: tcx.def_span(local_impl_def_id),
trait_desc: overlap.trait_ref,
self_ty: overlap.self_ty,
negative_impl_span: tcx.span_of_impl(negative_impl_def_id),
positive_impl_span: tcx.span_of_impl(positive_impl_def_id),
})
.emit()
}

fn report_conflicting_impls<'tcx>(
tcx: TyCtxt<'tcx>,
overlap: OverlapError<'tcx>,
impl_def_id: LocalDefId,
used_to_be_allowed: Option<FutureCompatOverlapErrorKind>,
sg: &mut specialization_graph::Graph,
) {
) -> Result<(), ErrorGuaranteed> {
let impl_span = tcx.def_span(impl_def_id);

// Work to be done after we've built the DiagnosticBuilder. We have to define it
Expand Down Expand Up @@ -429,14 +431,11 @@ fn report_conflicting_impls<'tcx>(
let mut err = tcx.dcx().struct_span_err(impl_span, msg);
err.code(error_code!(E0119));
decorate(tcx, &overlap, impl_span, &mut err);
Some(err.emit())
err.emit()
} else {
Some(
tcx.dcx()
.span_delayed_bug(impl_span, "impl should have failed the orphan check"),
)
tcx.dcx().span_delayed_bug(impl_span, "impl should have failed the orphan check")
};
sg.has_errored = reported;
Err(reported)
}
Some(kind) => {
let lint = match kind {
Expand All @@ -452,8 +451,9 @@ fn report_conflicting_impls<'tcx>(
decorate(tcx, &overlap, impl_span, err);
},
);
Ok(())
}
};
}
}

/// Recovers the "impl X for Y" signature from `impl_def_id` and returns it as a
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/generic-associated-types/unknown-lifetime-ice-119827.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
trait Foo {
type Context<'c>
where
Self: 'c;
}

impl Foo for Box<dyn Foo> {}
//~^ ERROR `Foo` cannot be made into an object
//~| ERROR `Foo` cannot be made into an object
//~| ERROR cycle detected
//~| ERROR cycle detected
//~| ERROR cycle detected
//~| ERROR the trait bound `Box<(dyn Foo + 'static)>: Foo` is not satisfied
//~| ERROR not all trait items implemented

fn main() {}
Loading