Skip to content

Commit a38dde9

Browse files
authored
Rollup merge of rust-lang#123302 - compiler-errors:sized-bound-first, r=estebank
Make sure to insert `Sized` bound first into clauses list rust-lang#120323 made it so that we don't insert an implicit `Sized` bound whenever we see an *explicit* `Sized` bound. However, since the code that inserts implicit sized bounds puts the bound as the *first* in the list, that means that it had the **side-effect** of possibly meaning we check `Sized` *after* checking other trait bounds. If those trait bounds result in ambiguity or overflow or something, it may change how we winnow candidates. (**edit: SEE** rust-lang#123303) This is likely the cause for the regression in rust-lang#123279 (comment), since the impl... ```rust impl<T: Job + Sized> AsJob for T { // <----- changing this to `Sized + Job` or just `Job` (which turns into `Sized + Job`) will FIX the issue. } ``` ...looks incredibly suspicious. Fixes [after beta-backport] rust-lang#123279. Alternative is to revert rust-lang#120323. I don't have a strong opinion about this, but think it may be nice to keep the diagnostic changes around.
2 parents 1b0e46f + f2fd2d8 commit a38dde9

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

compiler/rustc_hir_analysis/src/bounds.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@ impl<'tcx> Bounds<'tcx> {
5454
span: Span,
5555
polarity: ty::PredicatePolarity,
5656
) {
57-
self.clauses.push((
57+
let clause = (
5858
trait_ref
5959
.map_bound(|trait_ref| {
6060
ty::ClauseKind::Trait(ty::TraitPredicate { trait_ref, polarity })
6161
})
6262
.to_predicate(tcx),
6363
span,
64-
));
64+
);
65+
// FIXME(-Znext-solver): We can likely remove this hack once the new trait solver lands.
66+
if tcx.lang_items().sized_trait() == Some(trait_ref.def_id()) {
67+
self.clauses.insert(0, clause);
68+
} else {
69+
self.clauses.push(clause);
70+
}
6571
}
6672

6773
pub fn push_projection_bound(

tests/incremental/hashes/function_interfaces.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ pub fn second_trait_bound<T: Eq + Clone>() {}
217217
pub fn second_builtin_bound<T: Send >() {}
218218

219219
#[cfg(not(any(cfail1,cfail4)))]
220-
#[rustc_clean(cfg = "cfail2", except = "opt_hir_owner_nodes, predicates_of")]
220+
#[rustc_clean(cfg = "cfail2", except = "opt_hir_owner_nodes")]
221221
#[rustc_clean(cfg = "cfail3")]
222222
#[rustc_clean(cfg = "cfail5", except = "opt_hir_owner_nodes, predicates_of")]
223223
#[rustc_clean(cfg = "cfail6")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//@ check-pass
2+
// Regression test due to #123279
3+
4+
pub trait Job: AsJob {
5+
fn run_once(&self);
6+
}
7+
8+
impl<F: Fn()> Job for F {
9+
fn run_once(&self) {
10+
todo!()
11+
}
12+
}
13+
14+
pub trait AsJob {}
15+
16+
// Ensure that `T: Sized + Job` by reordering the explicit `Sized` to where
17+
// the implicit sized pred would go.
18+
impl<T: Job + Sized> AsJob for T {}
19+
20+
pub struct LoopingJobService {
21+
job: Box<dyn Job>,
22+
}
23+
24+
impl Job for LoopingJobService {
25+
fn run_once(&self) {
26+
self.job.run_once()
27+
}
28+
}
29+
30+
fn main() {}

0 commit comments

Comments
 (0)