Skip to content

Commit f1babe7

Browse files
authored
Unrolled build for rust-lang#125608
Rollup merge of rust-lang#125608 - oli-obk:subsequent_lifetime_errors, r=BoxyUwU Avoid follow-up errors if the number of generic parameters already doesn't match fixes rust-lang#125604 best reviewed commit-by-commit
2 parents 27529d5 + d498eb5 commit f1babe7

21 files changed

+226
-194
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
702702
pub(crate) fn complain_about_missing_assoc_tys(
703703
&self,
704704
associated_types: FxIndexMap<Span, FxIndexSet<DefId>>,
705-
potential_assoc_types: Vec<Span>,
705+
potential_assoc_types: Vec<usize>,
706706
trait_bounds: &[hir::PolyTraitRef<'_>],
707707
) {
708708
if associated_types.values().all(|v| v.is_empty()) {

compiler/rustc_hir_analysis/src/hir_ty_lowering/generics.rs

+9-19
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, _)
@@ -281,7 +282,7 @@ pub fn lower_generic_args<'tcx: 'a, 'a>(
281282
GenericParamDefKind::Const { .. },
282283
_,
283284
) => {
284-
args.push(ctx.provided_kind(param, arg));
285+
args.push(ctx.provided_kind(&args, param, arg));
285286
args_iter.next();
286287
params.next();
287288
}
@@ -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

@@ -474,16 +475,9 @@ pub(crate) fn check_generic_arg_count(
474475
return Ok(());
475476
}
476477

477-
if provided_args > max_expected_args {
478-
invalid_args.extend(
479-
gen_args.args[max_expected_args..provided_args].iter().map(|arg| arg.span()),
480-
);
481-
};
478+
invalid_args.extend(min_expected_args..provided_args);
482479

483480
let gen_args_info = if provided_args > min_expected_args {
484-
invalid_args.extend(
485-
gen_args.args[min_expected_args..provided_args].iter().map(|arg| arg.span()),
486-
);
487481
let num_redundant_args = provided_args - min_expected_args;
488482
GenericArgsInfo::ExcessLifetimes { num_redundant_args }
489483
} else {
@@ -538,11 +532,7 @@ pub(crate) fn check_generic_arg_count(
538532
let num_default_params = expected_max - expected_min;
539533

540534
let gen_args_info = if provided > expected_max {
541-
invalid_args.extend(
542-
gen_args.args[args_offset + expected_max..args_offset + provided]
543-
.iter()
544-
.map(|arg| arg.span()),
545-
);
535+
invalid_args.extend((expected_max..provided).map(|i| i + args_offset));
546536
let num_redundant_args = provided - expected_max;
547537

548538
// Provide extra note if synthetic arguments like `impl Trait` are specified.
@@ -610,7 +600,7 @@ pub(crate) fn check_generic_arg_count(
610600
explicit_late_bound,
611601
correct: lifetimes_correct
612602
.and(args_correct)
613-
.map_err(|reported| GenericArgCountMismatch { reported: Some(reported), invalid_args }),
603+
.map_err(|reported| GenericArgCountMismatch { reported, invalid_args }),
614604
}
615605
}
616606

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+47-31
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,11 @@ pub(crate) enum GenericArgPosition {
215215

216216
/// A marker denoting that the generic arguments that were
217217
/// provided did not match the respective generic parameters.
218-
#[derive(Clone, Default, Debug)]
218+
#[derive(Clone, Debug)]
219219
pub struct GenericArgCountMismatch {
220-
/// Indicates whether a fatal error was reported (`Some`), or just a lint (`None`).
221-
pub reported: Option<ErrorGuaranteed>,
222-
/// A list of spans of arguments provided that were not valid.
223-
pub invalid_args: Vec<Span>,
220+
pub reported: ErrorGuaranteed,
221+
/// A list of indices of arguments provided that were not valid.
222+
pub invalid_args: Vec<usize>,
224223
}
225224

226225
/// Decorates the result of a generic argument count mismatch
@@ -240,13 +239,14 @@ pub trait GenericArgsLowerer<'a, 'tcx> {
240239

241240
fn provided_kind(
242241
&mut self,
242+
preceding_args: &[ty::GenericArg<'tcx>],
243243
param: &ty::GenericParamDef,
244244
arg: &GenericArg<'tcx>,
245245
) -> ty::GenericArg<'tcx>;
246246

247247
fn inferred_kind(
248248
&mut self,
249-
args: Option<&[ty::GenericArg<'tcx>]>,
249+
preceding_args: &[ty::GenericArg<'tcx>],
250250
param: &ty::GenericParamDef,
251251
infer_args: bool,
252252
) -> ty::GenericArg<'tcx>;
@@ -404,10 +404,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
404404
self_ty.is_some(),
405405
);
406406

407-
if let Err(err) = &arg_count.correct
408-
&& let Some(reported) = err.reported
409-
{
410-
self.set_tainted_by_errors(reported);
407+
if let Err(err) = &arg_count.correct {
408+
self.set_tainted_by_errors(err.reported);
411409
}
412410

413411
// Skip processing if type has no generic parameters.
@@ -425,6 +423,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
425423
span: Span,
426424
inferred_params: Vec<Span>,
427425
infer_args: bool,
426+
incorrect_args: &'a Result<(), GenericArgCountMismatch>,
428427
}
429428

430429
impl<'a, 'tcx> GenericArgsLowerer<'a, 'tcx> for GenericArgsCtxt<'a, 'tcx> {
@@ -439,11 +438,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
439438

440439
fn provided_kind(
441440
&mut self,
441+
preceding_args: &[ty::GenericArg<'tcx>],
442442
param: &ty::GenericParamDef,
443443
arg: &GenericArg<'tcx>,
444444
) -> ty::GenericArg<'tcx> {
445445
let tcx = self.lowerer.tcx();
446446

447+
if let Err(incorrect) = self.incorrect_args {
448+
if incorrect.invalid_args.contains(&(param.index as usize)) {
449+
return param.to_error(tcx, preceding_args);
450+
}
451+
}
452+
447453
let mut handle_ty_args = |has_default, ty: &hir::Ty<'tcx>| {
448454
if has_default {
449455
tcx.check_optional_stability(
@@ -506,11 +512,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
506512

507513
fn inferred_kind(
508514
&mut self,
509-
args: Option<&[ty::GenericArg<'tcx>]>,
515+
preceding_args: &[ty::GenericArg<'tcx>],
510516
param: &ty::GenericParamDef,
511517
infer_args: bool,
512518
) -> ty::GenericArg<'tcx> {
513519
let tcx = self.lowerer.tcx();
520+
521+
if let Err(incorrect) = self.incorrect_args {
522+
if incorrect.invalid_args.contains(&(param.index as usize)) {
523+
return param.to_error(tcx, preceding_args);
524+
}
525+
}
514526
match param.kind {
515527
GenericParamDefKind::Lifetime => self
516528
.lowerer
@@ -529,15 +541,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
529541
GenericParamDefKind::Type { has_default, .. } => {
530542
if !infer_args && has_default {
531543
// No type parameter provided, but a default exists.
532-
let args = args.unwrap();
533-
if args.iter().any(|arg| match arg.unpack() {
534-
GenericArgKind::Type(ty) => ty.references_error(),
535-
_ => false,
536-
}) {
544+
if let Some(prev) =
545+
preceding_args.iter().find_map(|arg| match arg.unpack() {
546+
GenericArgKind::Type(ty) => ty.error_reported().err(),
547+
_ => None,
548+
})
549+
{
537550
// Avoid ICE #86756 when type error recovery goes awry.
538-
return Ty::new_misc_error(tcx).into();
551+
return Ty::new_error(tcx, prev).into();
539552
}
540-
tcx.at(self.span).type_of(param.def_id).instantiate(tcx, args).into()
553+
tcx.at(self.span)
554+
.type_of(param.def_id)
555+
.instantiate(tcx, preceding_args)
556+
.into()
541557
} else if infer_args {
542558
self.lowerer.ty_infer(Some(param), self.span).into()
543559
} else {
@@ -557,7 +573,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
557573
// FIXME(effects) see if we should special case effect params here
558574
if !infer_args && has_default {
559575
tcx.const_param_default(param.def_id)
560-
.instantiate(tcx, args.unwrap())
576+
.instantiate(tcx, preceding_args)
561577
.into()
562578
} else {
563579
if infer_args {
@@ -571,6 +587,17 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
571587
}
572588
}
573589
}
590+
if let ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst = constness
591+
&& generics.has_self
592+
&& !tcx.has_attr(def_id, sym::const_trait)
593+
{
594+
let reported = tcx.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
595+
span,
596+
modifier: constness.as_str(),
597+
});
598+
self.set_tainted_by_errors(reported);
599+
arg_count.correct = Err(GenericArgCountMismatch { reported, invalid_args: vec![] });
600+
}
574601

575602
let mut args_ctx = GenericArgsCtxt {
576603
lowerer: self,
@@ -579,19 +606,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
579606
generic_args: segment.args(),
580607
inferred_params: vec![],
581608
infer_args: segment.infer_args,
609+
incorrect_args: &arg_count.correct,
582610
};
583-
if let ty::BoundConstness::Const | ty::BoundConstness::ConstIfConst = constness
584-
&& generics.has_self
585-
&& !tcx.has_attr(def_id, sym::const_trait)
586-
{
587-
let e = tcx.dcx().emit_err(crate::errors::ConstBoundForNonConstTrait {
588-
span,
589-
modifier: constness.as_str(),
590-
});
591-
self.set_tainted_by_errors(e);
592-
arg_count.correct =
593-
Err(GenericArgCountMismatch { reported: Some(e), invalid_args: vec![] });
594-
}
595611
let args = lower_generic_args(
596612
tcx,
597613
def_id,

compiler/rustc_hir_analysis/src/impl_wf_check.rs

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ fn enforce_impl_params_are_constrained(
8686
let impl_predicates = tcx.predicates_of(impl_def_id);
8787
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).map(ty::EarlyBinder::instantiate_identity);
8888

89+
impl_trait_ref.error_reported()?;
90+
8991
let mut input_parameters = cgp::parameters_for_impl(tcx, impl_self_ty, impl_trait_ref);
9092
cgp::identify_constrained_generic_params(
9193
tcx,

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11181118
// to add defaults. If the user provided *too many* types, that's
11191119
// a problem.
11201120

1121-
let mut infer_args_for_err = FxHashSet::default();
1121+
let mut infer_args_for_err = None;
11221122

11231123
let mut explicit_late_bound = ExplicitLateBound::No;
11241124
for &GenericPathSegment(def_id, index) in &generic_segments {
@@ -1136,9 +1136,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11361136
explicit_late_bound = ExplicitLateBound::Yes;
11371137
}
11381138

1139-
if let Err(GenericArgCountMismatch { reported: Some(e), .. }) = arg_count.correct {
1140-
infer_args_for_err.insert(index);
1141-
self.set_tainted_by_errors(e); // See issue #53251.
1139+
if let Err(GenericArgCountMismatch { reported, .. }) = arg_count.correct {
1140+
infer_args_for_err
1141+
.get_or_insert_with(|| (reported, FxHashSet::default()))
1142+
.1
1143+
.insert(index);
1144+
self.set_tainted_by_errors(reported); // See issue #53251.
11421145
}
11431146
}
11441147

@@ -1232,15 +1235,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12321235
};
12331236
let def_id = res.def_id();
12341237

1235-
let arg_count = GenericArgCountResult {
1236-
explicit_late_bound,
1237-
correct: if infer_args_for_err.is_empty() {
1238-
Ok(())
1239-
} else {
1240-
Err(GenericArgCountMismatch::default())
1241-
},
1238+
let (correct, infer_args_for_err) = match infer_args_for_err {
1239+
Some((reported, args)) => {
1240+
(Err(GenericArgCountMismatch { reported, invalid_args: vec![] }), args)
1241+
}
1242+
None => (Ok(()), Default::default()),
12421243
};
12431244

1245+
let arg_count = GenericArgCountResult { explicit_late_bound, correct };
1246+
12441247
struct CtorGenericArgsCtxt<'a, 'tcx> {
12451248
fcx: &'a FnCtxt<'a, 'tcx>,
12461249
span: Span,
@@ -1272,6 +1275,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12721275

12731276
fn provided_kind(
12741277
&mut self,
1278+
_preceding_args: &[ty::GenericArg<'tcx>],
12751279
param: &ty::GenericParamDef,
12761280
arg: &GenericArg<'tcx>,
12771281
) -> ty::GenericArg<'tcx> {
@@ -1314,7 +1318,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13141318

13151319
fn inferred_kind(
13161320
&mut self,
1317-
args: Option<&[ty::GenericArg<'tcx>]>,
1321+
preceding_args: &[ty::GenericArg<'tcx>],
13181322
param: &ty::GenericParamDef,
13191323
infer_args: bool,
13201324
) -> ty::GenericArg<'tcx> {
@@ -1328,7 +1332,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13281332
// If we have a default, then it doesn't matter that we're not
13291333
// inferring the type arguments: we provide the default where any
13301334
// is missing.
1331-
tcx.type_of(param.def_id).instantiate(tcx, args.unwrap()).into()
1335+
tcx.type_of(param.def_id).instantiate(tcx, preceding_args).into()
13321336
} else {
13331337
// If no type arguments were provided, we have to infer them.
13341338
// This case also occurs as a result of some malformed input, e.g.
@@ -1353,7 +1357,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13531357
} else if !infer_args {
13541358
return tcx
13551359
.const_param_default(param.def_id)
1356-
.instantiate(tcx, args.unwrap())
1360+
.instantiate(tcx, preceding_args)
13571361
.into();
13581362
}
13591363
}

compiler/rustc_hir_typeck/src/method/confirm.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
383383

384384
fn provided_kind(
385385
&mut self,
386+
_preceding_args: &[ty::GenericArg<'tcx>],
386387
param: &ty::GenericParamDef,
387388
arg: &GenericArg<'tcx>,
388389
) -> ty::GenericArg<'tcx> {
@@ -419,7 +420,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
419420

420421
fn inferred_kind(
421422
&mut self,
422-
_args: Option<&[ty::GenericArg<'tcx>]>,
423+
_preceding_args: &[ty::GenericArg<'tcx>],
423424
param: &ty::GenericParamDef,
424425
_infer_args: bool,
425426
) -> ty::GenericArg<'tcx> {

tests/crashes/121134.rs

-20
This file was deleted.

0 commit comments

Comments
 (0)