Skip to content

Commit 1d62869

Browse files
committed
Fix unelided lifetime ICE, small refactoring of GenericArgPosition
1 parent bcded33 commit 1d62869

File tree

11 files changed

+236
-124
lines changed

11 files changed

+236
-124
lines changed

compiler/rustc_hir_analysis/src/delegation.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::ty::{
1414
use rustc_span::{ErrorGuaranteed, Span, kw};
1515

1616
use crate::collect::ItemCtxt;
17-
use crate::hir_ty_lowering::{GenericArgPosition, HirTyLowerer};
17+
use crate::hir_ty_lowering::HirTyLowerer;
1818

1919
type RemapTable = FxHashMap<u32, u32>;
2020

@@ -581,14 +581,7 @@ fn get_delegation_user_specified_args<'tcx>(
581581
let self_ty = get_delegation_self_ty(tcx, delegation_id);
582582

583583
lowerer
584-
.lower_generic_args_of_path(
585-
segment.ident.span,
586-
def_id,
587-
&[],
588-
segment,
589-
self_ty,
590-
GenericArgPosition::Type,
591-
)
584+
.lower_generic_args_of_path(segment.ident.span, def_id, &[], segment, self_ty)
592585
.0
593586
.as_slice()
594587
});
@@ -610,14 +603,7 @@ fn get_delegation_user_specified_args<'tcx>(
610603
};
611604

612605
let args = lowerer
613-
.lower_generic_args_of_path(
614-
segment.ident.span,
615-
def_id,
616-
parent_args,
617-
segment,
618-
None,
619-
GenericArgPosition::Value,
620-
)
606+
.lower_generic_args_of_path(segment.ident.span, def_id, parent_args, segment, None)
621607
.0;
622608

623609
&args[parent_args.len()..]

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,7 @@ pub fn check_generic_arg_count_for_call(
392392
seg: &hir::PathSegment<'_>,
393393
is_method_call: IsMethodCall,
394394
) -> GenericArgCountResult {
395-
let gen_pos = match is_method_call {
396-
IsMethodCall::Yes => GenericArgPosition::MethodCall,
397-
IsMethodCall::No => GenericArgPosition::Value,
398-
};
395+
let gen_pos = GenericArgPosition::Call(is_method_call);
399396
check_generic_arg_count(cx, def_id, seg, generics, gen_pos, generics.has_own_self())
400397
}
401398

@@ -649,7 +646,7 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes(
649646
let note = "the late bound lifetime parameter is introduced here";
650647
let span = args.args[0].span();
651648

652-
if position == GenericArgPosition::Value
649+
if position == GenericArgPosition::Call(IsMethodCall::No)
653650
&& args.num_lifetime_params() != param_counts.lifetimes
654651
{
655652
struct_span_code_err!(cx.dcx(), span, E0794, "{}", msg)

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ pub enum ExplicitLateBound {
313313
No,
314314
}
315315

316-
#[derive(Copy, Clone, PartialEq)]
316+
#[derive(Debug, Copy, Clone, PartialEq)]
317317
pub enum IsMethodCall {
318318
Yes,
319319
No,
@@ -324,8 +324,7 @@ pub enum IsMethodCall {
324324
#[derive(Debug, Copy, Clone, PartialEq)]
325325
pub(crate) enum GenericArgPosition {
326326
Type,
327-
Value, // e.g., functions
328-
MethodCall,
327+
Call(IsMethodCall),
329328
}
330329

331330
/// Whether to allow duplicate associated iten constraints in a trait ref, e.g.
@@ -556,14 +555,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
556555
def_id: DefId,
557556
item_segment: &hir::PathSegment<'tcx>,
558557
) -> GenericArgsRef<'tcx> {
559-
let (args, _) = self.lower_generic_args_of_path(
560-
span,
561-
def_id,
562-
&[],
563-
item_segment,
564-
None,
565-
GenericArgPosition::Type,
566-
);
558+
let (args, _) = self.lower_generic_args_of_path(span, def_id, &[], item_segment, None);
567559
if let Some(c) = item_segment.args().constraints.first() {
568560
prohibit_assoc_item_constraint(self, c, Some((def_id, item_segment, span)));
569561
}
@@ -612,7 +604,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
612604
parent_args: &[ty::GenericArg<'tcx>],
613605
segment: &hir::PathSegment<'tcx>,
614606
self_ty: Option<Ty<'tcx>>,
615-
pos: GenericArgPosition,
616607
) -> (GenericArgsRef<'tcx>, GenericArgCountResult) {
617608
// If the type is parameterized by this region, then replace this
618609
// region with the current anon region binding (in other words,
@@ -635,8 +626,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
635626
assert!(self_ty.is_none());
636627
}
637628

638-
let arg_count =
639-
check_generic_arg_count(self, def_id, segment, generics, pos, self_ty.is_some());
629+
let arg_count = check_generic_arg_count(
630+
self,
631+
def_id,
632+
segment,
633+
generics,
634+
GenericArgPosition::Type,
635+
self_ty.is_some(),
636+
);
640637

641638
// Skip processing if type has no generic parameters.
642639
// Traits always have `Self` as a generic parameter, which means they will not return early
@@ -821,14 +818,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
821818
item_segment: &hir::PathSegment<'tcx>,
822819
parent_args: GenericArgsRef<'tcx>,
823820
) -> GenericArgsRef<'tcx> {
824-
let (args, _) = self.lower_generic_args_of_path(
825-
span,
826-
item_def_id,
827-
parent_args,
828-
item_segment,
829-
None,
830-
GenericArgPosition::Type,
831-
);
821+
let (args, _) =
822+
self.lower_generic_args_of_path(span, item_def_id, parent_args, item_segment, None);
832823
if let Some(c) = item_segment.args().constraints.first() {
833824
prohibit_assoc_item_constraint(self, c, Some((item_def_id, item_segment, span)));
834825
}
@@ -940,7 +931,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
940931
&[],
941932
segment,
942933
Some(self_ty),
943-
GenericArgPosition::Type,
944934
);
945935

946936
let constraints = segment.args().constraints;
@@ -1116,14 +1106,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11161106
) -> ty::TraitRef<'tcx> {
11171107
self.report_internal_fn_trait(span, trait_def_id, trait_segment, is_impl);
11181108

1119-
let (generic_args, _) = self.lower_generic_args_of_path(
1120-
span,
1121-
trait_def_id,
1122-
&[],
1123-
trait_segment,
1124-
Some(self_ty),
1125-
GenericArgPosition::Type,
1126-
);
1109+
let (generic_args, _) =
1110+
self.lower_generic_args_of_path(span, trait_def_id, &[], trait_segment, Some(self_ty));
11271111
if let Some(c) = trait_segment.args().constraints.first() {
11281112
prohibit_assoc_item_constraint(self, c, Some((trait_def_id, trait_segment, span)));
11291113
}

tests/ui/delegation/generics/generics-gen-args-errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mod test_1 {
3636
//~| ERROR can't use generic parameters from outer item
3737
//~| ERROR can't use generic parameters from outer item
3838
//~| ERROR: unresolved item provided when a constant was expected
39+
//~| ERROR: function takes 2 lifetime arguments but 0 lifetime arguments were supplied
3940
}
4041
}
4142

@@ -47,6 +48,7 @@ mod test_2 {
4748

4849
reuse foo::<String, String> as bar2;
4950
//~^ ERROR: function takes 3 generic arguments but 2 generic arguments were supplied
51+
//~| ERROR: function takes 2 lifetime arguments but 0 lifetime arguments were supplied
5052

5153
reuse foo::<'static, _, 'asdasd, 'static, 'static, 'static, _> as bar3;
5254
//~^ ERROR: use of undeclared lifetime name `'asdasd`
@@ -58,10 +60,12 @@ mod test_2 {
5860

5961
reuse foo::<1, 2, _, 4, 5, _> as bar5;
6062
//~^ ERROR: function takes 3 generic arguments but 6 generic arguments were supplied
63+
//~| ERROR: function takes 2 lifetime arguments but 0 lifetime arguments were supplied
6164

6265
reuse foo::<1, 2,asd,String, { let x = 0; }> as bar6;
6366
//~^ ERROR: cannot find type `asd` in this scope
6467
//~| ERROR: function takes 3 generic arguments but 5 generic arguments were supplied
68+
//~| ERROR: function takes 2 lifetime arguments but 0 lifetime arguments were supplied
6569

6670
reuse foo::<"asdasd", asd, "askdn", 'static, 'a> as bar7;
6771
//~^ ERROR: use of undeclared lifetime name `'a`
@@ -70,6 +74,7 @@ mod test_2 {
7074

7175
reuse foo::<{}, {}, {}> as bar8;
7276
//~^ ERROR: constant provided when a type was expected
77+
//~| ERROR: function takes 2 lifetime arguments but 0 lifetime arguments were supplied
7378
}
7479

7580
mod test_3 {
@@ -107,12 +112,14 @@ mod test_3 {
107112
//~| ERROR: trait takes 3 lifetime arguments but 1 lifetime argument was supplied
108113
//~| ERROR: trait takes 2 generic arguments but 3 generic arguments were supplied
109114
//~| ERROR: method takes 2 generic arguments but 6 generic arguments were supplied
115+
//~| ERROR: method takes 1 lifetime argument but 0 lifetime arguments were supplied
110116

111117
reuse Trait::<Trait, Clone, _, 'static, dyn Send, _>::foo::<1, 2, 3, _, 6> as bar7;
112118
//~^ ERROR: missing lifetime specifiers [E0106]
113119
//~| ERROR: trait takes 3 lifetime arguments but 1 lifetime argument was supplied
114120
//~| ERROR: trait takes 2 generic arguments but 5 generic arguments were supplied
115121
//~| ERROR: method takes 2 generic arguments but 5 generic arguments were supplied
122+
//~| ERROR: method takes 1 lifetime argument but 0 lifetime arguments were supplied
116123
}
117124

118125
fn main() {}

0 commit comments

Comments
 (0)