From f2fd2d8c7080f7a7d770b3e3d27e525250c182dc Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sun, 31 Mar 2024 17:20:00 -0400 Subject: [PATCH] Make sure to insert Sized bound first into clauses list --- compiler/rustc_hir_analysis/src/bounds.rs | 10 +++++-- .../incremental/hashes/function_interfaces.rs | 2 +- ...verflow-due-to-sized-predicate-ordering.rs | 30 +++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/ui/methods/probe-overflow-due-to-sized-predicate-ordering.rs diff --git a/compiler/rustc_hir_analysis/src/bounds.rs b/compiler/rustc_hir_analysis/src/bounds.rs index d5465bb5dd5..d3f51195dfb 100644 --- a/compiler/rustc_hir_analysis/src/bounds.rs +++ b/compiler/rustc_hir_analysis/src/bounds.rs @@ -54,14 +54,20 @@ fn push_trait_bound_inner( span: Span, polarity: ty::PredicatePolarity, ) { - self.clauses.push(( + let clause = ( trait_ref .map_bound(|trait_ref| { ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, polarity }) }) .to_predicate(tcx), span, - )); + ); + // FIXME(-Znext-solver): We can likely remove this hack once the new trait solver lands. + if tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) { + self.clauses.insert(0, clause); + } else { + self.clauses.push(clause); + } } pub fn push_projection_bound( diff --git a/tests/incremental/hashes/function_interfaces.rs b/tests/incremental/hashes/function_interfaces.rs index 3be30fab99c..4fa2762099c 100644 --- a/tests/incremental/hashes/function_interfaces.rs +++ b/tests/incremental/hashes/function_interfaces.rs @@ -217,7 +217,7 @@ pub fn second_trait_bound() {} pub fn second_builtin_bound() {} #[cfg(not(any(cfail1,cfail4)))] -#[rustc_clean(cfg = "cfail2", except = "opt_hir_owner_nodes, predicates_of")] +#[rustc_clean(cfg = "cfail2", except = "opt_hir_owner_nodes")] #[rustc_clean(cfg = "cfail3")] #[rustc_clean(cfg = "cfail5", except = "opt_hir_owner_nodes, predicates_of")] #[rustc_clean(cfg = "cfail6")] diff --git a/tests/ui/methods/probe-overflow-due-to-sized-predicate-ordering.rs b/tests/ui/methods/probe-overflow-due-to-sized-predicate-ordering.rs new file mode 100644 index 00000000000..08e15117c4b --- /dev/null +++ b/tests/ui/methods/probe-overflow-due-to-sized-predicate-ordering.rs @@ -0,0 +1,30 @@ +//@ check-pass +// Regression test due to #123279 + +pub trait Job: AsJob { + fn run_once(&self); +} + +impl Job for F { + fn run_once(&self) { + todo!() + } +} + +pub trait AsJob {} + +// Ensure that `T: Sized + Job` by reordering the explicit `Sized` to where +// the implicit sized pred would go. +impl AsJob for T {} + +pub struct LoopingJobService { + job: Box, +} + +impl Job for LoopingJobService { + fn run_once(&self) { + self.job.run_once() + } +} + +fn main() {}