Skip to content

Commit 108a1e5

Browse files
committed
Always provide previous generic arguments
1 parent 063b26a commit 108a1e5

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,11 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
214214
if let Some(&param) = params.peek() {
215215
if param.index == 0 {
216216
if let GenericParamDefKind::Type { .. } = param.kind {
217+
assert_eq!(&args[..], &[]);
217218
args.push(
218219
self_ty
219220
.map(|ty| ty.into())
220-
.unwrap_or_else(|| ctx.inferred_kind(None, param, true)),
221+
.unwrap_or_else(|| ctx.inferred_kind(&args, param, true)),
221222
);
222223
params.next();
223224
}
@@ -267,7 +268,7 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
267268
// Since this is a const impl, we need to insert a host arg at the end of
268269
// `PartialEq`'s generics, but this errors since `Rhs` isn't specified.
269270
// To work around this, we infer all arguments until we reach the host param.
270-
args.push(ctx.inferred_kind(Some(&args), param, infer_args));
271+
args.push(ctx.inferred_kind(&args, param, infer_args));
271272
params.next();
272273
}
273274
(GenericArg::Lifetime(_), GenericParamDefKind::Lifetime, _)
@@ -292,7 +293,7 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
292293
) => {
293294
// We expected a lifetime argument, but got a type or const
294295
// argument. That means we're inferring the lifetimes.
295-
args.push(ctx.inferred_kind(None, param, infer_args));
296+
args.push(ctx.inferred_kind(&args, param, infer_args));
296297
force_infer_lt = Some((arg, param));
297298
params.next();
298299
}
@@ -388,7 +389,7 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
388389
(None, Some(&param)) => {
389390
// If there are fewer arguments than parameters, it means
390391
// we're inferring the remaining arguments.
391-
args.push(ctx.inferred_kind(Some(&args), param, infer_args));
392+
args.push(ctx.inferred_kind(&args, param, infer_args));
392393
params.next();
393394
}
394395

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+15-26
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub trait GenericArgsLowerer<'a, 'tcx> {
245245

246246
fn inferred_kind(
247247
&mut self,
248-
args: Option<&[ty::GenericArg<'tcx>]>,
248+
preceding_args: &[ty::GenericArg<'tcx>],
249249
param: &ty::GenericParamDef,
250250
infer_args: bool,
251251
) -> ty::GenericArg<'tcx>;
@@ -525,30 +525,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
525525

526526
fn inferred_kind(
527527
&mut self,
528-
args: Option<&[ty::GenericArg<'tcx>]>,
528+
preceding_args: &[ty::GenericArg<'tcx>],
529529
param: &ty::GenericParamDef,
530530
infer_args: bool,
531531
) -> ty::GenericArg<'tcx> {
532532
let tcx = self.lowerer.tcx();
533533

534534
if let Err(incorrect) = self.incorrect_args {
535535
if incorrect.invalid_args.contains(&(param.index as usize)) {
536-
// FIXME: use `param.to_error` once `inferred_kind` is supplied a list of
537-
// all previous generic args.
538-
return match param.kind {
539-
GenericParamDefKind::Lifetime => {
540-
ty::Region::new_error(tcx, incorrect.reported).into()
541-
}
542-
GenericParamDefKind::Type { .. } => {
543-
Ty::new_error(tcx, incorrect.reported).into()
544-
}
545-
GenericParamDefKind::Const { .. } => ty::Const::new_error(
546-
tcx,
547-
incorrect.reported,
548-
Ty::new_error(tcx, incorrect.reported),
549-
)
550-
.into(),
551-
};
536+
return param.to_error(tcx, preceding_args);
552537
}
553538
}
554539
match param.kind {
@@ -569,15 +554,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
569554
GenericParamDefKind::Type { has_default, .. } => {
570555
if !infer_args && has_default {
571556
// No type parameter provided, but a default exists.
572-
let args = args.unwrap();
573-
if args.iter().any(|arg| match arg.unpack() {
574-
GenericArgKind::Type(ty) => ty.references_error(),
575-
_ => false,
576-
}) {
557+
if let Some(prev) =
558+
preceding_args.iter().find_map(|arg| match arg.unpack() {
559+
GenericArgKind::Type(ty) => ty.error_reported().err(),
560+
_ => None,
561+
})
562+
{
577563
// Avoid ICE #86756 when type error recovery goes awry.
578-
return Ty::new_misc_error(tcx).into();
564+
return Ty::new_error(tcx, prev).into();
579565
}
580-
tcx.at(self.span).type_of(param.def_id).instantiate(tcx, args).into()
566+
tcx.at(self.span)
567+
.type_of(param.def_id)
568+
.instantiate(tcx, preceding_args)
569+
.into()
581570
} else if infer_args {
582571
self.lowerer.ty_infer(Some(param), self.span).into()
583572
} else {
@@ -597,7 +586,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
597586
// FIXME(effects) see if we should special case effect params here
598587
if !infer_args && has_default {
599588
tcx.const_param_default(param.def_id)
600-
.instantiate(tcx, args.unwrap())
589+
.instantiate(tcx, preceding_args)
601590
.into()
602591
} else {
603592
if infer_args {

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13171317

13181318
fn inferred_kind(
13191319
&mut self,
1320-
args: Option<&[ty::GenericArg<'tcx>]>,
1320+
preceding_args: &[ty::GenericArg<'tcx>],
13211321
param: &ty::GenericParamDef,
13221322
infer_args: bool,
13231323
) -> ty::GenericArg<'tcx> {
@@ -1331,7 +1331,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13311331
// If we have a default, then it doesn't matter that we're not
13321332
// inferring the type arguments: we provide the default where any
13331333
// is missing.
1334-
tcx.type_of(param.def_id).instantiate(tcx, args.unwrap()).into()
1334+
tcx.type_of(param.def_id).instantiate(tcx, preceding_args).into()
13351335
} else {
13361336
// If no type arguments were provided, we have to infer them.
13371337
// This case also occurs as a result of some malformed input, e.g.
@@ -1356,7 +1356,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13561356
} else if !infer_args {
13571357
return tcx
13581358
.const_param_default(param.def_id)
1359-
.instantiate(tcx, args.unwrap())
1359+
.instantiate(tcx, preceding_args)
13601360
.into();
13611361
}
13621362
}

compiler/rustc_hir_typeck/src/method/confirm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
419419

420420
fn inferred_kind(
421421
&mut self,
422-
_args: Option<&[ty::GenericArg<'tcx>]>,
422+
_preceding_args: &[ty::GenericArg<'tcx>],
423423
param: &ty::GenericParamDef,
424424
_infer_args: bool,
425425
) -> ty::GenericArg<'tcx> {

0 commit comments

Comments
 (0)