Skip to content

Commit aa3b5c5

Browse files
committed
Fix diagnostic regression
1 parent ae81fc6 commit aa3b5c5

22 files changed

+129
-133
lines changed

src/librustc_passes/ast_validation.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,9 @@ impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
539539
fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) {
540540
match *generic_args {
541541
GenericArgs::AngleBracketed(ref data) => {
542-
data.args.iter().for_each(|arg| self.visit_generic_arg(arg));
542+
for arg in &data.args {
543+
self.visit_generic_arg(arg)
544+
}
543545
for type_binding in &data.bindings {
544546
// Type bindings such as `Item=impl Debug` in `Iterator<Item=Debug>`
545547
// are allowed to contain nested `impl Trait`.

src/librustc_typeck/astconv.rs

+36-37
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ struct ConvertedBinding<'tcx> {
9494

9595
#[derive(PartialEq)]
9696
enum GenericArgPosition {
97-
Datatype,
98-
Function,
99-
Method,
97+
Type,
98+
Value, // e.g. functions
99+
MethodCall,
100100
}
101101

102+
// FIXME(#53525): these error codes should all be unified.
102103
struct GenericArgMismatchErrorCode {
103104
lifetimes: (&'static str, &'static str),
104105
types: (&'static str, &'static str),
@@ -255,9 +256,9 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
255256
&empty_args
256257
},
257258
if is_method_call {
258-
GenericArgPosition::Method
259+
GenericArgPosition::MethodCall
259260
} else {
260-
GenericArgPosition::Function
261+
GenericArgPosition::Value
261262
},
262263
def.parent.is_none() && def.has_self, // `has_self`
263264
seg.infer_types || suppress_mismatch, // `infer_types`
@@ -285,7 +286,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
285286
// arguments in order to validate them with respect to the generic parameters.
286287
let param_counts = def.own_counts();
287288
let arg_counts = args.own_counts();
288-
let infer_lifetimes = position != GenericArgPosition::Datatype && arg_counts.lifetimes == 0;
289+
let infer_lifetimes = position != GenericArgPosition::Type && arg_counts.lifetimes == 0;
289290

290291
let mut defaults: ty::GenericParamCount = Default::default();
291292
for param in &def.params {
@@ -297,7 +298,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
297298
};
298299
}
299300

300-
if position != GenericArgPosition::Datatype && !args.bindings.is_empty() {
301+
if position != GenericArgPosition::Type && !args.bindings.is_empty() {
301302
AstConv::prohibit_assoc_ty_binding(tcx, args.bindings[0].span);
302303
}
303304

@@ -308,7 +309,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
308309
if late bound lifetime parameters are present";
309310
let note = "the late bound lifetime parameter is introduced here";
310311
let span = args.args[0].span();
311-
if position == GenericArgPosition::Function
312+
if position == GenericArgPosition::Value
312313
&& arg_counts.lifetimes != param_counts.lifetimes {
313314
let mut err = tcx.sess.struct_span_err(span, msg);
314315
err.span_note(span_late, note);
@@ -328,7 +329,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
328329
kind,
329330
required,
330331
permitted,
331-
provided| {
332+
provided,
333+
offset| {
332334
// We enforce the following: `required` <= `provided` <= `permitted`.
333335
// For kinds without defaults (i.e. lifetimes), `required == permitted`.
334336
// For other kinds (i.e. types), `permitted` may be greater than `required`.
@@ -348,8 +350,15 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
348350
(required, "")
349351
};
350352

353+
let mut span = span;
351354
let label = if required == permitted && provided > permitted {
352355
let diff = provided - permitted;
356+
if diff == 1 {
357+
// In the case when the user has provided too many arguments,
358+
// we want to point to the first unexpected argument.
359+
let first_superfluous_arg: &GenericArg = &args.args[offset + permitted];
360+
span = first_superfluous_arg.span();
361+
}
353362
format!(
354363
"{}unexpected {} argument{}",
355364
if diff != 1 { format!("{} ", diff) } else { String::new() },
@@ -394,6 +403,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
394403
param_counts.lifetimes,
395404
param_counts.lifetimes,
396405
arg_counts.lifetimes,
406+
0,
397407
);
398408
}
399409
if !infer_types
@@ -404,6 +414,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
404414
param_counts.types - defaults.types - has_self as usize,
405415
param_counts.types - has_self as usize,
406416
arg_counts.types,
417+
arg_counts.lifetimes,
407418
)
408419
} else {
409420
false
@@ -491,59 +502,50 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
491502
// provided, matching them with the generic parameters we expect.
492503
// Mismatches can occur as a result of elided lifetimes, or for malformed
493504
// input. We try to handle both sensibly.
494-
let mut progress_arg = true;
495505
match (args.peek(), params.peek()) {
496506
(Some(&arg), Some(&param)) => {
497507
match (arg, &param.kind) {
498-
(GenericArg::Lifetime(_), GenericParamDefKind::Lifetime) => {
508+
(GenericArg::Lifetime(_), GenericParamDefKind::Lifetime)
509+
| (GenericArg::Type(_), GenericParamDefKind::Type { .. }) => {
499510
push_kind(&mut substs, provided_kind(param, arg));
511+
args.next();
500512
params.next();
501513
}
502514
(GenericArg::Lifetime(_), GenericParamDefKind::Type { .. }) => {
503515
// We expected a type argument, but got a lifetime
504516
// argument. This is an error, but we need to handle it
505517
// gracefully so we can report sensible errors. In this
506-
// case, we're simply going to infer the remaining
507-
// arguments.
508-
args.by_ref().for_each(drop); // Exhaust the iterator.
509-
}
510-
(GenericArg::Type(_), GenericParamDefKind::Type { .. }) => {
511-
push_kind(&mut substs, provided_kind(param, arg));
512-
params.next();
518+
// case, we're simply going to infer this argument.
519+
args.next();
513520
}
514521
(GenericArg::Type(_), GenericParamDefKind::Lifetime) => {
515522
// We expected a lifetime argument, but got a type
516523
// argument. That means we're inferring the lifetimes.
517524
push_kind(&mut substs, inferred_kind(None, param, infer_types));
518525
params.next();
519-
progress_arg = false;
520526
}
521527
}
522528
}
523529
(Some(_), None) => {
524530
// We should never be able to reach this point with well-formed input.
525531
// Getting to this point means the user supplied more arguments than
526532
// there are parameters.
533+
args.next();
527534
}
528535
(None, Some(&param)) => {
529536
// If there are fewer arguments than parameters, it means
530537
// we're inferring the remaining arguments.
531538
match param.kind {
532-
GenericParamDefKind::Lifetime => {
533-
push_kind(&mut substs, inferred_kind(None, param, infer_types));
534-
}
535-
GenericParamDefKind::Type { .. } => {
539+
GenericParamDefKind::Lifetime | GenericParamDefKind::Type { .. } => {
536540
let kind = inferred_kind(Some(&substs), param, infer_types);
537541
push_kind(&mut substs, kind);
538542
}
539543
}
544+
args.next();
540545
params.next();
541546
}
542547
(None, None) => break,
543548
}
544-
if progress_arg {
545-
args.next();
546-
}
547549
}
548550
}
549551

@@ -582,12 +584,12 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
582584
span,
583585
&generic_params,
584586
&generic_args,
585-
GenericArgPosition::Datatype,
587+
GenericArgPosition::Type,
586588
has_self,
587589
infer_types,
588590
GenericArgMismatchErrorCode {
589591
lifetimes: ("E0107", "E0107"),
590-
types: ("E0243", "E0244"), // FIXME: E0243 and E0244 should be unified.
592+
types: ("E0243", "E0244"),
591593
},
592594
);
593595

@@ -616,17 +618,14 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
616618
|_| (Some(generic_args), infer_types),
617619
// Provide substitutions for parameters for which (valid) arguments have been provided.
618620
|param, arg| {
619-
match param.kind {
620-
GenericParamDefKind::Lifetime => match arg {
621-
GenericArg::Lifetime(lt) => {
622-
self.ast_region_to_region(&lt, Some(param)).into()
623-
}
624-
_ => unreachable!(),
621+
match (&param.kind, arg) {
622+
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
623+
self.ast_region_to_region(&lt, Some(param)).into()
625624
}
626-
GenericParamDefKind::Type { .. } => match arg {
627-
GenericArg::Type(ty) => self.ast_ty_to_ty(&ty).into(),
628-
_ => unreachable!(),
625+
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
626+
self.ast_ty_to_ty(&ty).into()
629627
}
628+
_ => unreachable!(),
630629
}
631630
},
632631
// Provide substitutions for parameters for which arguments are inferred.

src/librustc_typeck/check/method/confirm.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,14 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
344344
},
345345
// Provide substitutions for parameters for which (valid) arguments have been provided.
346346
|param, arg| {
347-
match param.kind {
348-
GenericParamDefKind::Lifetime => match arg {
349-
GenericArg::Lifetime(lt) => {
350-
AstConv::ast_region_to_region(self.fcx, lt, Some(param)).into()
351-
}
352-
_ => unreachable!(),
347+
match (&param.kind, arg) {
348+
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
349+
AstConv::ast_region_to_region(self.fcx, lt, Some(param)).into()
353350
}
354-
GenericParamDefKind::Type { .. } => match arg {
355-
GenericArg::Type(ty) => self.to_ty(ty).into(),
356-
_ => unreachable!(),
351+
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
352+
self.to_ty(ty).into()
357353
}
354+
_ => unreachable!(),
358355
}
359356
},
360357
// Provide substitutions for parameters for which arguments are inferred.

src/librustc_typeck/check/mod.rs

+18-20
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ use session::{CompileIncomplete, config, Session};
109109
use TypeAndSubsts;
110110
use lint;
111111
use util::common::{ErrorReported, indenter};
112-
use util::nodemap::{DefIdMap, DefIdSet, FxHashMap, NodeMap};
112+
use util::nodemap::{DefIdMap, DefIdSet, FxHashMap, FxHashSet, NodeMap};
113113

114114
use std::cell::{Cell, RefCell, Ref, RefMut};
115115
use rustc_data_structures::sync::Lrc;
116-
use std::collections::{hash_map::Entry, HashSet};
116+
use std::collections::hash_map::Entry;
117117
use std::cmp;
118118
use std::fmt::Display;
119119
use std::iter;
@@ -4908,7 +4908,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
49084908
// provided (if any) into their appropriate spaces. We'll also report
49094909
// errors if type parameters are provided in an inappropriate place.
49104910

4911-
let mut generic_segs = HashSet::new();
4911+
let mut generic_segs = FxHashSet::default();
49124912
for PathSeg(_, index) in &path_segs {
49134913
generic_segs.insert(index);
49144914
}
@@ -4937,24 +4937,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
49374937
// to add defaults. If the user provided *too many* types, that's
49384938
// a problem.
49394939

4940-
let mut suppress_errors = FxHashMap();
4940+
let mut infer_args_for_err = FxHashSet::default();
49414941
for &PathSeg(def_id, index) in &path_segs {
49424942
let seg = &segments[index];
49434943
let generics = self.tcx.generics_of(def_id);
4944-
// `impl Trait` is treated as a normal generic parameter internally,
4945-
// but we don't allow users to specify the parameter's value
4946-
// explicitly, so we have to do some error-checking here.
4947-
let suppress = AstConv::check_generic_arg_count_for_call(
4944+
// Argument-position `impl Trait` is treated as a normal generic
4945+
// parameter internally, but we don't allow users to specify the
4946+
// parameter's value explicitly, so we have to do some error-
4947+
// checking here.
4948+
let suppress_errors = AstConv::check_generic_arg_count_for_call(
49484949
self.tcx,
49494950
span,
49504951
&generics,
49514952
&seg,
49524953
false, // `is_method_call`
49534954
);
4954-
if suppress {
4955+
if suppress_errors {
4956+
infer_args_for_err.insert(index);
49554957
self.set_tainted_by_errors(); // See issue #53251.
49564958
}
4957-
suppress_errors.insert(index, suppress);
49584959
}
49594960

49604961
let has_self = path_segs.last().map(|PathSeg(def_id, _)| {
@@ -4976,7 +4977,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
49764977
}) {
49774978
// If we've encountered an `impl Trait`-related error, we're just
49784979
// going to infer the arguments for better error messages.
4979-
if !suppress_errors[&index] {
4980+
if !infer_args_for_err.contains(&index) {
49804981
// Check whether the user has provided generic arguments.
49814982
if let Some(ref data) = segments[index].args {
49824983
return (Some(data), segments[index].infer_types);
@@ -4989,17 +4990,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
49894990
},
49904991
// Provide substitutions for parameters for which (valid) arguments have been provided.
49914992
|param, arg| {
4992-
match param.kind {
4993-
GenericParamDefKind::Lifetime => match arg {
4994-
GenericArg::Lifetime(lt) => {
4995-
AstConv::ast_region_to_region(self, lt, Some(param)).into()
4996-
}
4997-
_ => unreachable!(),
4993+
match (&param.kind, arg) {
4994+
(GenericParamDefKind::Lifetime, GenericArg::Lifetime(lt)) => {
4995+
AstConv::ast_region_to_region(self, lt, Some(param)).into()
49984996
}
4999-
GenericParamDefKind::Type { .. } => match arg {
5000-
GenericArg::Type(ty) => self.to_ty(ty).into(),
5001-
_ => unreachable!(),
4997+
(GenericParamDefKind::Type { .. }, GenericArg::Type(ty)) => {
4998+
self.to_ty(ty).into()
50024999
}
5000+
_ => unreachable!(),
50035001
}
50045002
},
50055003
// Provide substitutions for parameters for which arguments are inferred.

src/test/ui/bad/bad-mid-path-type-params.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
error[E0087]: wrong number of type arguments: expected 1, found 2
2-
--> $DIR/bad-mid-path-type-params.rs:40:13
2+
--> $DIR/bad-mid-path-type-params.rs:40:28
33
|
44
LL | let _ = S::new::<isize,f64>(1, 1.0);
5-
| ^^^^^^^^^^^^^^^^^^^ unexpected type argument
5+
| ^^^ unexpected type argument
66

77
error[E0107]: wrong number of lifetime arguments: expected 0, found 1
8-
--> $DIR/bad-mid-path-type-params.rs:43:13
8+
--> $DIR/bad-mid-path-type-params.rs:43:17
99
|
1010
LL | let _ = S::<'a,isize>::new::<f64>(1, 1.0);
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected lifetime argument
11+
| ^^ unexpected lifetime argument
1212

1313
error[E0087]: wrong number of type arguments: expected 1, found 2
14-
--> $DIR/bad-mid-path-type-params.rs:46:17
14+
--> $DIR/bad-mid-path-type-params.rs:46:36
1515
|
1616
LL | let _: S2 = Trait::new::<isize,f64>(1, 1.0);
17-
| ^^^^^^^^^^^^^^^^^^^^^^^ unexpected type argument
17+
| ^^^ unexpected type argument
1818

1919
error[E0088]: wrong number of lifetime arguments: expected 0, found 1
20-
--> $DIR/bad-mid-path-type-params.rs:49:17
20+
--> $DIR/bad-mid-path-type-params.rs:49:25
2121
|
2222
LL | let _: S2 = Trait::<'a,isize>::new::<f64>(1, 1.0);
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected lifetime argument
23+
| ^^ unexpected lifetime argument
2424

2525
error: aborting due to 4 previous errors
2626

src/test/ui/constructor-lifetime-args.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | S::<'static>(&0, &0);
55
| ^^^^^^^^^^^^ expected 2 lifetime arguments
66

77
error[E0088]: wrong number of lifetime arguments: expected 2, found 3
8-
--> $DIR/constructor-lifetime-args.rs:29:5
8+
--> $DIR/constructor-lifetime-args.rs:29:27
99
|
1010
LL | S::<'static, 'static, 'static>(&0, &0);
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected lifetime argument
11+
| ^^^^^^^ unexpected lifetime argument
1212

1313
error[E0090]: wrong number of lifetime arguments: expected 2, found 1
1414
--> $DIR/constructor-lifetime-args.rs:32:5
@@ -17,10 +17,10 @@ LL | E::V::<'static>(&0);
1717
| ^^^^^^^^^^^^^^^ expected 2 lifetime arguments
1818

1919
error[E0088]: wrong number of lifetime arguments: expected 2, found 3
20-
--> $DIR/constructor-lifetime-args.rs:34:5
20+
--> $DIR/constructor-lifetime-args.rs:34:30
2121
|
2222
LL | E::V::<'static, 'static, 'static>(&0);
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ unexpected lifetime argument
23+
| ^^^^^^^ unexpected lifetime argument
2424

2525
error: aborting due to 4 previous errors
2626

0 commit comments

Comments
 (0)