Skip to content

Commit d109bb0

Browse files
authored
Rollup merge of #158394 - aerooneqq:mgca-synth-arg-ice, r=petrochenkov
Generate synthetic generic args only for delegation's child segment Fixes #158152 by generating synthetic generic args only for delegation's child segment. r? @petrochenkov cc @fmease
2 parents 35f0aa9 + 080c5d2 commit d109bb0

5 files changed

Lines changed: 39 additions & 8 deletions

File tree

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::{
66
};
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::DefId;
9-
use rustc_hir::{self as hir, DelegationInfo, GenericArg};
9+
use rustc_hir::{self as hir, GenericArg};
1010
use rustc_middle::ty::{
1111
self, GenericArgsRef, GenericParamDef, GenericParamDefKind, IsSuggestable, Ty,
1212
};
@@ -431,15 +431,12 @@ pub(crate) fn check_generic_arg_count(
431431
}
432432

433433
let tcx = cx.tcx();
434-
let parent_def = tcx.hir_get_parent_item(seg.hir_id).def_id;
435434

436435
// Suppress this warning for delegations as it is compiler generated and lifetimes are
437436
// propagated while late-bound lifetimes may be present.
438-
let explicit_late_bound = match tcx.hir_opt_delegation_info(parent_def) {
439-
Some(DelegationInfo { child_seg_id, .. }) if seg.hir_id == *child_seg_id => {
440-
ExplicitLateBound::No
441-
}
442-
_ => prohibit_explicit_late_bound_lifetimes(cx, gen_params, gen_args, gen_pos),
437+
let explicit_late_bound = match tcx.hir_is_delegation_child_segment(seg) {
438+
true => ExplicitLateBound::No,
439+
false => prohibit_explicit_late_bound_lifetimes(cx, gen_params, gen_args, gen_pos),
443440
};
444441

445442
let mut invalid_args = vec![];

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
716716
generic_args: &'a GenericArgs<'tcx>,
717717
span: Span,
718718
infer_args: bool,
719+
create_synth_args: bool,
719720
incorrect_args: &'a Result<(), GenericArgCountMismatch>,
720721
}
721722

@@ -828,7 +829,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
828829
.instantiate(tcx, preceding_args)
829830
.skip_norm_wip()
830831
.into()
831-
} else if synthetic {
832+
} else if self.create_synth_args && synthetic {
832833
Ty::new_param(tcx, param.index, param.name).into()
833834
} else if infer_args {
834835
self.lowerer.ty_infer(Some(param), self.span).into()
@@ -868,6 +869,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
868869
span,
869870
generic_args: segment.args(),
870871
infer_args: segment.infer_args,
872+
create_synth_args: tcx.hir_is_delegation_child_segment(segment),
871873
incorrect_args: &arg_count.correct,
872874
};
873875

compiler/rustc_middle/src/hir/map.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,13 @@ impl<'tcx> TyCtxt<'tcx> {
879879
self.hir_opt_delegation_info(delegation_id).expect("processing delegation")
880880
}
881881

882+
pub fn hir_is_delegation_child_segment(self, segment: &PathSegment<'_>) -> bool {
883+
let parent_def = self.hir_get_parent_item(segment.hir_id).def_id;
884+
885+
self.hir_opt_delegation_info(parent_def)
886+
.is_some_and(|info| info.child_seg_id == segment.hir_id)
887+
}
888+
882889
#[inline]
883890
fn hir_opt_ident(self, id: HirId) -> Option<Ident> {
884891
match self.hir_node(id) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(min_generic_const_args)]
2+
3+
trait A<T> {}
4+
trait Trait<const N: usize> {}
5+
6+
impl A<[usize; fn_item]> for () {}
7+
8+
fn fn_item(_: impl Trait<usize>) {}
9+
//~^ ERROR: type provided when a constant was expected
10+
11+
fn main() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0747]: type provided when a constant was expected
2+
--> $DIR/synth-gen-arg-ice-158152.rs:8:26
3+
|
4+
LL | fn fn_item(_: impl Trait<usize>) {}
5+
| ^^^^^
6+
|
7+
help: if this generic argument was intended as a const parameter, surround it with braces
8+
|
9+
LL | fn fn_item(_: impl Trait<{ usize }>) {}
10+
| + +
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0747`.

0 commit comments

Comments
 (0)