Skip to content

Commit

Permalink
Stop passing traitref/traitpredicate by ref
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Jun 7, 2024
1 parent 295d922 commit d2fa92b
Show file tree
Hide file tree
Showing 17 changed files with 89 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ impl<T> Trait<T> for X {
for pred in hir_generics.bounds_for_param(def_id) {
if self.constrain_generic_bound_associated_type_structured_suggestion(
diag,
&trait_ref,
trait_ref,
pred.bounds,
assoc,
assoc_args,
Expand Down Expand Up @@ -706,7 +706,7 @@ fn foo(&self) -> Self::T { String::new() }

self.constrain_generic_bound_associated_type_structured_suggestion(
diag,
&trait_ref,
trait_ref,
opaque_hir_ty.bounds,
assoc,
assoc_args,
Expand Down Expand Up @@ -860,7 +860,7 @@ fn foo(&self) -> Self::T { String::new() }
fn constrain_generic_bound_associated_type_structured_suggestion(
&self,
diag: &mut Diag<'_>,
trait_ref: &ty::TraitRef<'tcx>,
trait_ref: ty::TraitRef<'tcx>,
bounds: hir::GenericBounds<'_>,
assoc: ty::AssocItem,
assoc_args: &[ty::GenericArg<'tcx>],
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_infer/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
let obligations =
predicates.predicates.iter().enumerate().map(|(index, &(clause, span))| {
elaboratable.child_with_derived_cause(
clause
.instantiate_supertrait(tcx, &bound_clause.rebind(data.trait_ref)),
clause.instantiate_supertrait(tcx, bound_clause.rebind(data.trait_ref)),
span,
bound_clause.rebind(data),
index,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<'tcx> Elaborator<'tcx> {
let super_predicates =
self.tcx.super_predicates_of(trait_ref.def_id()).predicates.iter().filter_map(
|&(pred, _)| {
let clause = pred.instantiate_supertrait(self.tcx, &trait_ref);
let clause = pred.instantiate_supertrait(self.tcx, trait_ref);
self.visited.insert(clause).then_some(clause)
},
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/predicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl<'tcx> Clause<'tcx> {
pub fn instantiate_supertrait(
self,
tcx: TyCtxt<'tcx>,
trait_ref: &ty::PolyTraitRef<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
) -> Clause<'tcx> {
// The interaction between HRTB and supertraits is not entirely
// obvious. Let me walk you (and myself) through an example.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3597,7 +3597,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
&self,
obligation: &PredicateObligation<'tcx>,
err: &mut Diag<'_>,
trait_ref: &ty::PolyTraitRef<'tcx>,
trait_ref: ty::PolyTraitRef<'tcx>,
) {
let rhs_span = match obligation.cause.code() {
ObligationCauseCode::BinOp { rhs_span: Some(span), rhs_is_lit, .. } if *rhs_is_lit => {
Expand Down Expand Up @@ -4822,7 +4822,7 @@ impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
pub(super) fn get_explanation_based_on_obligation<'tcx>(
tcx: TyCtxt<'tcx>,
obligation: &PredicateObligation<'tcx>,
trait_predicate: &ty::PolyTraitPredicate<'tcx>,
trait_predicate: ty::PolyTraitPredicate<'tcx>,
pre_message: String,
) -> String {
if let ObligationCauseCode::MainFunctionType = obligation.cause.code() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
};

let err_msg = self.get_standard_error_message(
&main_trait_predicate,
main_trait_predicate,
message,
predicate_is_const,
append_const_msg,
Expand Down Expand Up @@ -601,7 +601,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
let explanation = get_explanation_based_on_obligation(
self.tcx,
&obligation,
&main_trait_predicate,
trait_predicate,
pre_message,
);

Expand Down Expand Up @@ -652,7 +652,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

let UnsatisfiedConst(unsatisfied_const) = self
.maybe_add_note_for_unsatisfied_const(
&trait_predicate,
trait_predicate,
&mut err,
span,
);
Expand All @@ -669,7 +669,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
err.span_label(tcx.def_span(body), s);
}

self.suggest_floating_point_literal(&obligation, &mut err, &main_trait_ref);
self.suggest_floating_point_literal(&obligation, &mut err, main_trait_ref);
self.suggest_dereferencing_index(&obligation, &mut err, trait_predicate);
suggested |= self.suggest_dereferences(&obligation, &mut err, trait_predicate);
suggested |= self.suggest_fn_call(&obligation, &mut err, trait_predicate);
Expand Down Expand Up @@ -704,7 +704,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
span,
trait_predicate,
);
self.note_version_mismatch(&mut err, &main_trait_ref);
self.note_version_mismatch(&mut err, main_trait_ref);
self.suggest_remove_await(&obligation, &mut err);
self.suggest_derive(&obligation, &mut err, trait_predicate);

Expand Down Expand Up @@ -752,7 +752,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

self.try_to_add_help_message(
&obligation,
&main_trait_predicate,
main_trait_predicate,
&mut err,
span,
is_fn_trait,
Expand Down Expand Up @@ -2224,11 +2224,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
/// If the `Self` type of the unsatisfied trait `trait_ref` implements a trait
/// with the same path as `trait_ref`, a help message about
/// a probable version mismatch is added to `err`
fn note_version_mismatch(
&self,
err: &mut Diag<'_>,
trait_ref: &ty::PolyTraitRef<'tcx>,
) -> bool {
fn note_version_mismatch(&self, err: &mut Diag<'_>, trait_ref: ty::PolyTraitRef<'tcx>) -> bool {
let get_trait_impls = |trait_def_id| {
let mut trait_impls = vec![];
self.tcx.for_each_relevant_impl(
Expand Down Expand Up @@ -3032,7 +3028,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

fn get_standard_error_message(
&self,
trait_predicate: &ty::PolyTraitPredicate<'tcx>,
trait_predicate: ty::PolyTraitPredicate<'tcx>,
message: Option<String>,
predicate_is_const: bool,
append_const_msg: Option<AppendConstMessage>,
Expand Down Expand Up @@ -3203,7 +3199,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
fn try_to_add_help_message(
&self,
obligation: &PredicateObligation<'tcx>,
trait_predicate: &ty::PolyTraitPredicate<'tcx>,
trait_predicate: ty::PolyTraitPredicate<'tcx>,
err: &mut Diag<'_>,
span: Span,
is_fn_trait: bool,
Expand Down Expand Up @@ -3235,7 +3231,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
params,
);
} else if !trait_predicate.has_non_region_infer()
&& self.predicate_can_apply(obligation.param_env, *trait_predicate)
&& self.predicate_can_apply(obligation.param_env, trait_predicate)
{
// If a where-clause may be useful, remind the
// user that they can add it.
Expand All @@ -3246,7 +3242,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
// which is somewhat confusing.
self.suggest_restricting_param_bound(
err,
*trait_predicate,
trait_predicate,
None,
obligation.cause.body_id,
);
Expand All @@ -3261,7 +3257,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
);
} else if !suggested && !unsatisfied_const {
// Can't show anything else useful, try to find similar impls.
let impl_candidates = self.find_similar_impl_candidates(*trait_predicate);
let impl_candidates = self.find_similar_impl_candidates(trait_predicate);
if !self.report_similar_impl_candidates(
&impl_candidates,
trait_predicate.to_poly_trait_ref(),
Expand All @@ -3272,7 +3268,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
) {
self.report_similar_impl_candidates_for_root_obligation(
obligation,
*trait_predicate,
trait_predicate,
body_def_id,
err,
);
Expand Down Expand Up @@ -3346,7 +3342,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {

fn maybe_add_note_for_unsatisfied_const(
&self,
_trait_predicate: &ty::PolyTraitPredicate<'tcx>,
_trait_predicate: ty::PolyTraitPredicate<'tcx>,
_err: &mut Diag<'_>,
_span: Span,
) -> UnsatisfiedConst {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/object_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ fn predicates_reference_self(
predicates
.predicates
.iter()
.map(|&(predicate, sp)| (predicate.instantiate_supertrait(tcx, &trait_ref), sp))
.map(|&(predicate, sp)| (predicate.instantiate_supertrait(tcx, trait_ref), sp))
.filter_map(|predicate| predicate_references_self(tcx, predicate))
.collect()
}
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
// the param_env so that it can be given the lowest priority. See
// #50825 for the motivation for this.
let is_global =
|cand: &ty::PolyTraitPredicate<'tcx>| cand.is_global() && !cand.has_bound_vars();
|cand: ty::PolyTraitPredicate<'tcx>| cand.is_global() && !cand.has_bound_vars();

// (*) Prefer `BuiltinCandidate { has_nested: false }`, `PointeeCandidate`,
// `DiscriminantKindCandidate`, `ConstDestructCandidate`
Expand Down Expand Up @@ -1909,7 +1909,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
}

(
ParamCandidate(ref other_cand),
ParamCandidate(other_cand),
ImplCandidate(..)
| AutoImplCandidate
| ClosureCandidate { .. }
Expand All @@ -1934,12 +1934,12 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
//
// Global bounds from the where clause should be ignored
// here (see issue #50825).
DropVictim::drop_if(!is_global(other_cand))
DropVictim::drop_if(!is_global(*other_cand))
}
(ObjectCandidate(_) | ProjectionCandidate(_), ParamCandidate(ref victim_cand)) => {
(ObjectCandidate(_) | ProjectionCandidate(_), ParamCandidate(victim_cand)) => {
// Prefer these to a global where-clause bound
// (see issue #50825).
if is_global(victim_cand) { DropVictim::Yes } else { DropVictim::No }
if is_global(*victim_cand) { DropVictim::Yes } else { DropVictim::No }
}
(
ImplCandidate(_)
Expand All @@ -1957,12 +1957,12 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
| TraitUpcastingUnsizeCandidate(_)
| BuiltinCandidate { has_nested: true }
| TraitAliasCandidate,
ParamCandidate(ref victim_cand),
ParamCandidate(victim_cand),
) => {
// Prefer these to a global where-clause bound
// (see issue #50825).
DropVictim::drop_if(
is_global(victim_cand) && other.evaluation.must_apply_modulo_regions(),
is_global(*victim_cand) && other.evaluation.must_apply_modulo_regions(),
)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl<'tcx> TraitAliasExpander<'tcx> {
debug!(?predicates);

let items = predicates.predicates.iter().rev().filter_map(|(pred, span)| {
pred.instantiate_supertrait(tcx, &trait_ref)
pred.instantiate_supertrait(tcx, trait_ref)
.as_trait_clause()
.map(|trait_ref| item.clone_and_push(trait_ref.map_bound(|t| t.trait_ref), *span))
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn prepare_vtable_segments_inner<'tcx, T>(
.predicates
.into_iter()
.filter_map(move |(pred, _)| {
pred.instantiate_supertrait(tcx, &inner_most_trait_ref).as_trait_clause()
pred.instantiate_supertrait(tcx, inner_most_trait_ref).as_trait_clause()
});

// Find an unvisited supertrait
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0277]: the trait bound `&'static u32: Defaulted` is not satisfied
--> $DIR/typeck-default-trait-impl-precedence.rs:19:20
|
LL | is_defaulted::<&'static u32>();
| ^^^^^^^^^^^^ the trait `Defaulted` is not implemented for `&'static u32`, which is required by `&'static u32: Defaulted`
| ^^^^^^^^^^^^ the trait `Signed` is not implemented for `u32`, which is required by `&'static u32: Defaulted`
|
note: required for `&'static u32` to implement `Defaulted`
--> $DIR/typeck-default-trait-impl-precedence.rs:10:19
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0277]: the trait bound `i32: Baz<Self>` is not satisfied
--> $DIR/assume-gat-normalization-for-nested-goals.rs:9:30
|
LL | type Bar<T>: Baz<Self> = i32;
| ^^^ the trait `Eq<i32>` is not implemented for `i32`, which is required by `i32: Baz<Self>`
| ^^^ the trait `Eq<i32>` is not implemented for `<Self as Foo>::Bar<()>`, which is required by `i32: Baz<Self>`
|
note: required for `i32` to implement `Baz<Self>`
--> $DIR/assume-gat-normalization-for-nested-goals.rs:16:23
Expand All @@ -14,10 +14,10 @@ note: required by a bound in `Foo::Bar`
|
LL | type Bar<T>: Baz<Self> = i32;
| ^^^^^^^^^ required by this bound in `Foo::Bar`
help: consider further restricting the associated type
help: consider introducing a `where` clause, but there might be an alternative better way to express this requirement
|
LL | trait Foo where <Self as Foo>::Bar<()>: Eq<i32> {
| +++++++++++++++++++++++++++++++++++++
LL | trait Foo where i32: Baz<Self> {
| ++++++++++++++++++++

error: aborting due to 1 previous error

Expand Down
58 changes: 48 additions & 10 deletions tests/ui/impl-trait/nested_impl_trait.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,67 @@ LL | fn bad_in_fn_syntax(x: fn() -> impl Into<impl Debug>) {}
|
= note: `impl Trait` is only allowed in arguments and return types of functions and methods

error[E0391]: cycle detected when computing type of `bad_in_ret_position::{opaque#0}`
--> $DIR/nested_impl_trait.rs:6:46
|
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^
|
note: ...which requires computing type of opaque `bad_in_ret_position::{opaque#0}`...
--> $DIR/nested_impl_trait.rs:6:46
|
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^
note: ...which requires type-checking `bad_in_ret_position`...
--> $DIR/nested_impl_trait.rs:6:1
|
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires computing type of `bad_in_ret_position::{opaque#0}`, completing the cycle
note: cycle used when checking that `bad_in_ret_position::{opaque#0}` is well-formed
--> $DIR/nested_impl_trait.rs:6:46
|
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error[E0277]: the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
--> $DIR/nested_impl_trait.rs:6:46
|
LL | fn bad_in_ret_position(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Debug` is not implemented for `impl Into<u32>`

error[E0391]: cycle detected when computing type of `<impl at $DIR/nested_impl_trait.rs:18:1: 18:7>::bad::{opaque#0}`
--> $DIR/nested_impl_trait.rs:19:34
|
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^
|
help: consider further restricting this bound
note: ...which requires computing type of opaque `<impl at $DIR/nested_impl_trait.rs:18:1: 18:7>::bad::{opaque#0}`...
--> $DIR/nested_impl_trait.rs:19:34
|
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^
note: ...which requires type-checking `<impl at $DIR/nested_impl_trait.rs:18:1: 18:7>::bad`...
--> $DIR/nested_impl_trait.rs:19:5
|
LL | fn bad_in_ret_position(x: impl Into<u32> + std::fmt::Debug) -> impl Into<impl Debug> { x }
| +++++++++++++++++
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires computing type of `<impl at $DIR/nested_impl_trait.rs:18:1: 18:7>::bad::{opaque#0}`, completing the cycle
note: cycle used when checking that `<impl at $DIR/nested_impl_trait.rs:18:1: 18:7>::bad::{opaque#0}` is well-formed
--> $DIR/nested_impl_trait.rs:19:34
|
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information

error[E0277]: the trait bound `impl Into<u32>: Into<impl Debug>` is not satisfied
--> $DIR/nested_impl_trait.rs:19:34
|
LL | fn bad(x: impl Into<u32>) -> impl Into<impl Debug> { x }
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Debug` is not implemented for `impl Into<u32>`
|
help: consider further restricting this bound
|
LL | fn bad(x: impl Into<u32> + std::fmt::Debug) -> impl Into<impl Debug> { x }
| +++++++++++++++++

error: aborting due to 7 previous errors
error: aborting due to 9 previous errors

Some errors have detailed explanations: E0277, E0562, E0666.
Some errors have detailed explanations: E0277, E0391, E0562, E0666.
For more information about an error, try `rustc --explain E0277`.
2 changes: 1 addition & 1 deletion tests/ui/kindck/kindck-send-object.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0277]: `&'static (dyn Dummy + 'static)` cannot be sent between threads sa
LL | assert_send::<&'static (dyn Dummy + 'static)>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `&'static (dyn Dummy + 'static)` cannot be sent between threads safely
|
= help: the trait `Sync` is not implemented for `&'static (dyn Dummy + 'static)`, which is required by `&'static (dyn Dummy + 'static): Send`
= help: the trait `Sync` is not implemented for `(dyn Dummy + 'static)`, which is required by `&'static (dyn Dummy + 'static): Send`
= note: required for `&'static (dyn Dummy + 'static)` to implement `Send`
note: required by a bound in `assert_send`
--> $DIR/kindck-send-object.rs:5:18
Expand Down
Loading

0 comments on commit d2fa92b

Please sign in to comment.