Skip to content

Commit fc2c587

Browse files
authored
Rollup merge of #112867 - compiler-errors:more-impl-source-nits, r=lcnr
More `ImplSource` nits Even more clean-ups, I'll put this up in parallel with the `select_in_new_trait_solver` PR. r? ``@lcnr``
2 parents dabcbae + 7d0a5c3 commit fc2c587

File tree

8 files changed

+47
-106
lines changed

8 files changed

+47
-106
lines changed

compiler/rustc_middle/src/traits/mod.rs

-23
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,6 @@ pub enum ImplSource<'tcx, N> {
660660

661661
/// ImplSource for trait upcasting coercion
662662
TraitUpcasting(ImplSourceTraitUpcastingData<N>),
663-
664-
/// ImplSource for a trait alias.
665-
TraitAlias(ImplSourceTraitAliasData<'tcx, N>),
666663
}
667664

668665
impl<'tcx, N> ImplSource<'tcx, N> {
@@ -671,7 +668,6 @@ impl<'tcx, N> ImplSource<'tcx, N> {
671668
ImplSource::UserDefined(i) => i.nested,
672669
ImplSource::Param(n, _) | ImplSource::Builtin(n) => n,
673670
ImplSource::Object(d) => d.nested,
674-
ImplSource::TraitAlias(d) => d.nested,
675671
ImplSource::TraitUpcasting(d) => d.nested,
676672
}
677673
}
@@ -681,7 +677,6 @@ impl<'tcx, N> ImplSource<'tcx, N> {
681677
ImplSource::UserDefined(i) => &i.nested,
682678
ImplSource::Param(n, _) | ImplSource::Builtin(n) => &n,
683679
ImplSource::Object(d) => &d.nested,
684-
ImplSource::TraitAlias(d) => &d.nested,
685680
ImplSource::TraitUpcasting(d) => &d.nested,
686681
}
687682
}
@@ -691,7 +686,6 @@ impl<'tcx, N> ImplSource<'tcx, N> {
691686
ImplSource::UserDefined(i) => &mut i.nested,
692687
ImplSource::Param(n, _) | ImplSource::Builtin(n) => n,
693688
ImplSource::Object(d) => &mut d.nested,
694-
ImplSource::TraitAlias(d) => &mut d.nested,
695689
ImplSource::TraitUpcasting(d) => &mut d.nested,
696690
}
697691
}
@@ -709,15 +703,9 @@ impl<'tcx, N> ImplSource<'tcx, N> {
709703
ImplSource::Param(n, ct) => ImplSource::Param(n.into_iter().map(f).collect(), ct),
710704
ImplSource::Builtin(n) => ImplSource::Builtin(n.into_iter().map(f).collect()),
711705
ImplSource::Object(o) => ImplSource::Object(ImplSourceObjectData {
712-
upcast_trait_def_id: o.upcast_trait_def_id,
713706
vtable_base: o.vtable_base,
714707
nested: o.nested.into_iter().map(f).collect(),
715708
}),
716-
ImplSource::TraitAlias(d) => ImplSource::TraitAlias(ImplSourceTraitAliasData {
717-
alias_def_id: d.alias_def_id,
718-
substs: d.substs,
719-
nested: d.nested.into_iter().map(f).collect(),
720-
}),
721709
ImplSource::TraitUpcasting(d) => {
722710
ImplSource::TraitUpcasting(ImplSourceTraitUpcastingData {
723711
vtable_vptr_slot: d.vtable_vptr_slot,
@@ -761,9 +749,6 @@ pub struct ImplSourceTraitUpcastingData<N> {
761749
#[derive(PartialEq, Eq, Clone, TyEncodable, TyDecodable, HashStable, Lift)]
762750
#[derive(TypeFoldable, TypeVisitable)]
763751
pub struct ImplSourceObjectData<N> {
764-
/// `Foo` upcast to the obligation trait. This will be some supertrait of `Foo`.
765-
pub upcast_trait_def_id: DefId,
766-
767752
/// The vtable is formed by concatenating together the method lists of
768753
/// the base object trait and all supertraits, pointers to supertrait vtable will
769754
/// be provided when necessary; this is the start of `upcast_trait_ref`'s methods
@@ -773,14 +758,6 @@ pub struct ImplSourceObjectData<N> {
773758
pub nested: Vec<N>,
774759
}
775760

776-
#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)]
777-
#[derive(TypeFoldable, TypeVisitable)]
778-
pub struct ImplSourceTraitAliasData<'tcx, N> {
779-
pub alias_def_id: DefId,
780-
pub substs: SubstsRef<'tcx>,
781-
pub nested: Vec<N>,
782-
}
783-
784761
#[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)]
785762
pub enum ObjectSafetyViolation {
786763
/// `Self: Sized` declared on the trait.

compiler/rustc_middle/src/traits/structural_impls.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSource<'tcx, N> {
1717
write!(f, "ImplSourceParamData({:?}, {:?})", n, ct)
1818
}
1919

20-
super::ImplSource::TraitAlias(ref d) => write!(f, "{:?}", d),
21-
2220
super::ImplSource::TraitUpcasting(ref d) => write!(f, "{:?}", d),
2321
}
2422
}
@@ -48,18 +46,8 @@ impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceObjectData<N> {
4846
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4947
write!(
5048
f,
51-
"ImplSourceObjectData(upcast={:?}, vtable_base={}, nested={:?})",
52-
self.upcast_trait_def_id, self.vtable_base, self.nested
53-
)
54-
}
55-
}
56-
57-
impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceTraitAliasData<'tcx, N> {
58-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59-
write!(
60-
f,
61-
"ImplSourceTraitAliasData(alias_def_id={:?}, substs={:?}, nested={:?})",
62-
self.alias_def_id, self.substs, self.nested
49+
"ImplSourceObjectData(vtable_base={}, nested={:?})",
50+
self.vtable_base, self.nested
6351
)
6452
}
6553
}

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

+34-49
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ pub mod suggestions;
55
use super::{
66
FulfillmentError, FulfillmentErrorCode, MismatchedProjectionTypes, Obligation, ObligationCause,
77
ObligationCauseCode, ObligationCtxt, OutputTypeParameterMismatch, Overflow,
8-
PredicateObligation, SelectionContext, SelectionError, TraitNotObjectSafe,
8+
PredicateObligation, SelectionError, TraitNotObjectSafe,
99
};
1010
use crate::infer::error_reporting::{TyCategory, TypeAnnotationNeeded as ErrorCode};
1111
use crate::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
@@ -2272,55 +2272,40 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
22722272
)
22732273
};
22742274

2275-
let obligation = obligation.with(self.tcx, trait_ref);
2276-
let mut selcx = SelectionContext::new(&self);
2277-
match selcx.select_from_obligation(&obligation) {
2278-
Ok(None) => {
2279-
let ambiguities =
2280-
ambiguity::recompute_applicable_impls(self.infcx, &obligation);
2281-
let has_non_region_infer = trait_ref
2282-
.skip_binder()
2283-
.substs
2284-
.types()
2285-
.any(|t| !t.is_ty_or_numeric_infer());
2286-
// It doesn't make sense to talk about applicable impls if there are more
2287-
// than a handful of them.
2288-
if ambiguities.len() > 1 && ambiguities.len() < 10 && has_non_region_infer {
2289-
if self.tainted_by_errors().is_some() && subst.is_none() {
2290-
// If `subst.is_none()`, then this is probably two param-env
2291-
// candidates or impl candidates that are equal modulo lifetimes.
2292-
// Therefore, if we've already emitted an error, just skip this
2293-
// one, since it's not particularly actionable.
2294-
err.cancel();
2295-
return;
2296-
}
2297-
self.annotate_source_of_ambiguity(&mut err, &ambiguities, predicate);
2298-
} else {
2299-
if self.tainted_by_errors().is_some() {
2300-
err.cancel();
2301-
return;
2302-
}
2303-
err.note(format!("cannot satisfy `{}`", predicate));
2304-
let impl_candidates = self.find_similar_impl_candidates(
2305-
predicate.to_opt_poly_trait_pred().unwrap(),
2306-
);
2307-
if impl_candidates.len() < 10 {
2308-
self.report_similar_impl_candidates(
2309-
impl_candidates.as_slice(),
2310-
trait_ref,
2311-
obligation.cause.body_id,
2312-
&mut err,
2313-
false,
2314-
);
2315-
}
2316-
}
2275+
let ambiguities = ambiguity::recompute_applicable_impls(
2276+
self.infcx,
2277+
&obligation.with(self.tcx, trait_ref),
2278+
);
2279+
let has_non_region_infer =
2280+
trait_ref.skip_binder().substs.types().any(|t| !t.is_ty_or_numeric_infer());
2281+
// It doesn't make sense to talk about applicable impls if there are more
2282+
// than a handful of them.
2283+
if ambiguities.len() > 1 && ambiguities.len() < 10 && has_non_region_infer {
2284+
if self.tainted_by_errors().is_some() && subst.is_none() {
2285+
// If `subst.is_none()`, then this is probably two param-env
2286+
// candidates or impl candidates that are equal modulo lifetimes.
2287+
// Therefore, if we've already emitted an error, just skip this
2288+
// one, since it's not particularly actionable.
2289+
err.cancel();
2290+
return;
23172291
}
2318-
_ => {
2319-
if self.tainted_by_errors().is_some() {
2320-
err.cancel();
2321-
return;
2322-
}
2323-
err.note(format!("cannot satisfy `{}`", predicate));
2292+
self.annotate_source_of_ambiguity(&mut err, &ambiguities, predicate);
2293+
} else {
2294+
if self.tainted_by_errors().is_some() {
2295+
err.cancel();
2296+
return;
2297+
}
2298+
err.note(format!("cannot satisfy `{}`", predicate));
2299+
let impl_candidates = self
2300+
.find_similar_impl_candidates(predicate.to_opt_poly_trait_pred().unwrap());
2301+
if impl_candidates.len() < 10 {
2302+
self.report_similar_impl_candidates(
2303+
impl_candidates.as_slice(),
2304+
trait_ref,
2305+
obligation.cause.body_id,
2306+
&mut err,
2307+
false,
2308+
);
23242309
}
23252310
}
23262311

compiler/rustc_trait_selection/src/traits/project.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,6 @@ fn assemble_candidates_from_impls<'cx, 'tcx>(
17201720
};
17211721

17221722
let eligible = match &impl_source {
1723-
super::ImplSource::TraitAlias(_) => true,
17241723
super::ImplSource::UserDefined(impl_data) => {
17251724
// We have to be careful when projecting out of an
17261725
// impl because of specialization. If we are not in
@@ -2012,8 +2011,7 @@ fn confirm_select_candidate<'cx, 'tcx>(
20122011
}
20132012
super::ImplSource::Object(_)
20142013
| super::ImplSource::Param(..)
2015-
| super::ImplSource::TraitUpcasting(_)
2016-
| super::ImplSource::TraitAlias(..) => {
2014+
| super::ImplSource::TraitUpcasting(_) => {
20172015
// we don't create Select candidates with this kind of resolution
20182016
span_bug!(
20192017
obligation.cause.span,

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ use crate::traits::vtable::{
2727
};
2828
use crate::traits::{
2929
BuiltinDerivedObligation, ImplDerivedObligation, ImplDerivedObligationCause, ImplSource,
30-
ImplSourceObjectData, ImplSourceTraitAliasData, ImplSourceTraitUpcastingData,
31-
ImplSourceUserDefinedData, Normalized, Obligation, ObligationCause,
32-
OutputTypeParameterMismatch, PredicateObligation, Selection, SelectionError,
33-
TraitNotObjectSafe, TraitObligation, Unimplemented,
30+
ImplSourceObjectData, ImplSourceTraitUpcastingData, ImplSourceUserDefinedData, Normalized,
31+
Obligation, ObligationCause, OutputTypeParameterMismatch, PredicateObligation, Selection,
32+
SelectionError, TraitNotObjectSafe, TraitObligation, Unimplemented,
3433
};
3534

3635
use super::BuiltinImplConditions;
@@ -105,7 +104,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
105104

106105
TraitAliasCandidate => {
107106
let data = self.confirm_trait_alias_candidate(obligation);
108-
ImplSource::TraitAlias(data)
107+
ImplSource::Builtin(data)
109108
}
110109

111110
BuiltinObjectCandidate => {
@@ -652,11 +651,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
652651
(unnormalized_upcast_trait_ref, ty::Binder::dummy(object_trait_ref)),
653652
);
654653

655-
Ok(ImplSourceObjectData {
656-
upcast_trait_def_id: upcast_trait_ref.def_id(),
657-
vtable_base,
658-
nested,
659-
})
654+
Ok(ImplSourceObjectData { vtable_base, nested })
660655
}
661656

662657
fn confirm_fn_pointer_candidate(
@@ -721,10 +716,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
721716
fn confirm_trait_alias_candidate(
722717
&mut self,
723718
obligation: &TraitObligation<'tcx>,
724-
) -> ImplSourceTraitAliasData<'tcx, PredicateObligation<'tcx>> {
719+
) -> Vec<PredicateObligation<'tcx>> {
725720
debug!(?obligation, "confirm_trait_alias_candidate");
726721

727-
let alias_def_id = obligation.predicate.def_id();
728722
let predicate = self.infcx.instantiate_binder_with_placeholders(obligation.predicate);
729723
let trait_ref = predicate.trait_ref;
730724
let trait_def_id = trait_ref.def_id;
@@ -741,7 +735,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
741735

742736
debug!(?trait_def_id, ?trait_obligations, "trait alias obligations");
743737

744-
ImplSourceTraitAliasData { alias_def_id, substs, nested: trait_obligations }
738+
trait_obligations
745739
}
746740

747741
fn confirm_generator_candidate(

compiler/rustc_trait_selection/src/traits/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ pub fn get_vtable_index_of_object_method<'tcx, N>(
248248
) -> Option<usize> {
249249
// Count number of methods preceding the one we are selecting and
250250
// add them to the total offset.
251-
tcx.own_existential_vtable_entries(object.upcast_trait_def_id)
251+
tcx.own_existential_vtable_entries(tcx.parent(method_def_id))
252252
.iter()
253253
.copied()
254254
.position(|def_id| def_id == method_def_id)

compiler/rustc_ty_utils/src/instance.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,7 @@ fn resolve_associated_item<'tcx>(
293293
None
294294
}
295295
}
296-
traits::ImplSource::Param(..)
297-
| traits::ImplSource::TraitAlias(..)
298-
| traits::ImplSource::TraitUpcasting(_) => None,
296+
traits::ImplSource::Param(..) | traits::ImplSource::TraitUpcasting(_) => None,
299297
})
300298
}
301299

tests/ui/traits/new-solver/two-projection-param-candidates-are-ambiguous.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | needs_bar::<T>();
55
| ^^^^^^^^^^^^^^
66
|
77
= note: cannot satisfy `T: Bar`
8+
= help: the trait `Bar` is implemented for `T`
89
note: required by a bound in `needs_bar`
910
--> $DIR/two-projection-param-candidates-are-ambiguous.rs:23:17
1011
|

0 commit comments

Comments
 (0)