Skip to content
/ rust Public
forked from rust-lang/rust

Commit 28a1b56

Browse files
authored
Rollup merge of rust-lang#157625 - mu001999:fix-155834, r=JohnTitor
Use infer tys for synthetic params when lowering const paths point to fns ```rust #![feature(min_generic_const_args)] trait Trait {} impl<'t> Trait for [(); N] {} fn N(arg: impl Trait) {} ``` 1. `arg: impl Trait` implies a synthetic param `<impl Trait>` and the obligation `_: Trait`; 2. when matching `impl Trait for [(); N]`, lowering const arg `N` fills in that synthetic param, so the impl header contains `<[(); N::<impl Trait>] as Trait>`; 3. then instantiating this header with the impl's args causing a param/arg mismatch and ICE. This PR uses infer tys for synthetic params. Although this will emit the error in `ItemCtxt` because infer tys are not allowed here. I think that is reasonable, because `N` omits the synthetic parameter, though we cannot write the ty explicitly anywhere. Fixes rust-lang#155834
2 parents e3744bb + 0805fc2 commit 28a1b56

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2866,7 +2866,27 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
28662866
did,
28672867
path.segments.last().unwrap(),
28682868
);
2869-
ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, args))
2869+
2870+
if self.tcx().generics_of(did).own_synthetic_params_count() == 0 {
2871+
ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, args))
2872+
} else {
2873+
let tcx = self.tcx();
2874+
let generics = tcx.generics_of(did);
2875+
2876+
// Use infer tys for synthetic params; otherwise the impl header's trait ref may
2877+
// contain callee-owned synthetic params and fail when instantiated with impl args.
2878+
// See issue #155834
2879+
let args = args.iter().enumerate().map(|(index, arg)| {
2880+
let param = generics.param_at(index, tcx);
2881+
if param.kind.is_synthetic() {
2882+
self.ty_infer(Some(param), span).into()
2883+
} else {
2884+
arg
2885+
}
2886+
});
2887+
2888+
ty::Const::zero_sized(tcx, Ty::new_fn_def(tcx, did, args))
2889+
}
28702890
}
28712891

28722892
// Exhaustive match to be clear about what exactly we're considering to be
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test for issue #155834
2+
3+
#![expect(incomplete_features)]
4+
#![feature(min_generic_const_args)]
5+
6+
trait Trait {}
7+
8+
impl<'t> Trait for [(); N] {}
9+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for implementations
10+
11+
fn N(arg: impl Trait) {}
12+
13+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for implementations
2+
--> $DIR/bad-impl-trait-with-apit.rs:8:25
3+
|
4+
LL | impl<'t> Trait for [(); N] {}
5+
| ^ not allowed in type signatures
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0121`.

0 commit comments

Comments
 (0)