Skip to content

Commit 4b132ee

Browse files
committed
Initial commit
1 parent 08deb86 commit 4b132ee

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
367367
}
368368

369369
if position != GenericArgPosition::Type && !args.bindings.is_empty() {
370-
Self::prohibit_assoc_ty_binding(tcx, args.bindings[0].span);
370+
AstConv::prohibit_assoc_ty_binding(tcx, args.bindings[0].span);
371371
}
372372

373373
let explicit_late_bound =
@@ -392,7 +392,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
392392
}
393393

394394
if silent {
395-
return Err(true);
395+
return Err((0i32, None));
396396
}
397397

398398
// Unfortunately lifetime and type parameter mismatches are typically styled
@@ -441,54 +441,84 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
441441
for span in spans {
442442
err.span_label(span, label.as_str());
443443
}
444-
err.emit();
445444

446-
Err(true)
445+
assert_ne!(bound, provided);
446+
Err((bound as i32 - provided as i32, Some(err)))
447447
};
448+
let emit_correct =
449+
|correct: Result<(), (_, Option<rustc_errors::DiagnosticBuilder<'_>>)>| match correct {
450+
Ok(()) => Ok(()),
451+
Err((v, None)) => Err(v == 0),
452+
Err((v, Some(mut err))) => {
453+
err.emit();
454+
Err(v == 0)
455+
}
456+
};
448457

449-
let mut arg_count_correct = Ok(());
450458
let mut unexpected_spans = vec![];
451459

460+
let mut lifetime_count_correct = Ok(());
452461
if !infer_lifetimes || arg_counts.lifetimes > param_counts.lifetimes {
453-
arg_count_correct = check_kind_count(
462+
lifetime_count_correct = check_kind_count(
454463
"lifetime",
455464
param_counts.lifetimes,
456465
param_counts.lifetimes,
457466
arg_counts.lifetimes,
458467
0,
459468
&mut unexpected_spans,
460469
explicit_late_bound == ExplicitLateBound::Yes,
461-
)
462-
.and(arg_count_correct);
470+
);
463471
}
472+
464473
// FIXME(const_generics:defaults)
474+
let mut const_count_correct = Ok(());
465475
if !infer_args || arg_counts.consts > param_counts.consts {
466-
arg_count_correct = check_kind_count(
476+
const_count_correct = check_kind_count(
467477
"const",
468478
param_counts.consts,
469479
param_counts.consts,
470480
arg_counts.consts,
471481
arg_counts.lifetimes + arg_counts.types,
472482
&mut unexpected_spans,
473483
false,
474-
)
475-
.and(arg_count_correct);
484+
);
476485
}
486+
477487
// Note that type errors are currently be emitted *after* const errors.
488+
let mut type_count_correct = Ok(());
478489
if !infer_args || arg_counts.types > param_counts.types - defaults.types - has_self as usize
479490
{
480-
arg_count_correct = check_kind_count(
491+
type_count_correct = check_kind_count(
481492
"type",
482493
param_counts.types - defaults.types - has_self as usize,
483494
param_counts.types - has_self as usize,
484495
arg_counts.types,
485496
arg_counts.lifetimes,
486497
&mut unexpected_spans,
487498
false,
488-
)
489-
.and(arg_count_correct);
499+
);
490500
}
491501

502+
// Emit a help message if it's possible that a type could be surrounded in braces
503+
if let Err((c_mismatch, Some(ref mut _const_err))) = &mut const_count_correct {
504+
if let Err((t_mismatch, Some(ref mut type_err))) = &mut type_count_correct {
505+
if *c_mismatch == -*t_mismatch && *t_mismatch < 0 {
506+
for i in 0..*c_mismatch as usize {
507+
// let t_span = unexpected_type_spans[i].clone();
508+
let ident = args.args[arg_counts.lifetimes + i].id();
509+
type_err.help(&format!(
510+
"For more complex types, surround with braces: `{{ {} }}`",
511+
ident,
512+
));
513+
}
514+
}
515+
}
516+
}
517+
518+
let arg_count_correct = emit_correct(lifetime_count_correct)
519+
.and(emit_correct(const_count_correct))
520+
.and(emit_correct(type_count_correct));
521+
492522
GenericArgCountResult {
493523
explicit_late_bound,
494524
correct: arg_count_correct.map_err(|reported_err| GenericArgCountMismatch {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(const_generics)]
2+
#![allow(incomplete_features)]
3+
4+
#[derive(PartialEq, Eq)]
5+
enum CompileFlag {
6+
A,
7+
B,
8+
}
9+
10+
pub fn test<const CF: CompileFlag>() {}
11+
12+
pub fn main() {
13+
test::<CompileFlag::A>();
14+
//~^ ERROR: expected type, found variant
15+
//~| ERROR: wrong number of const arguments
16+
//~| ERROR: wrong number of type arguments
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0573]: expected type, found variant `CompileFlag::A`
2+
--> $DIR/invalid-enum.rs:13:10
3+
|
4+
LL | test::<CompileFlag::A>();
5+
| ^^^^^^^^^^^^^^
6+
| |
7+
| not a type
8+
| help: try using the variant's enum: `CompileFlag`
9+
10+
error[E0107]: wrong number of const arguments: expected 1, found 0
11+
--> $DIR/invalid-enum.rs:13:3
12+
|
13+
LL | test::<CompileFlag::A>();
14+
| ^^^^^^^^^^^^^^^^^^^^^^ expected 1 const argument
15+
16+
error[E0107]: wrong number of type arguments: expected 0, found 1
17+
--> $DIR/invalid-enum.rs:13:10
18+
|
19+
LL | test::<CompileFlag::A>();
20+
| ^^^^^^^^^^^^^^ unexpected type argument
21+
|
22+
= help: For more complex types, surround with braces: `{ HirId { owner: DefId(0:5 ~ invalid_enum[317d]::main[0]), local_id: 1 } }`
23+
24+
error: aborting due to 3 previous errors
25+
26+
Some errors have detailed explanations: E0107, E0573.
27+
For more information about an error, try `rustc --explain E0107`.

src/test/ui/const-generics/issues/issue-62878.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ error[E0107]: wrong number of type arguments: expected 0, found 1
2424
|
2525
LL | foo::<_, {[1]}>();
2626
| ^ unexpected type argument
27+
|
28+
= help: For more complex types, surround with braces: `{ HirId { owner: DefId(0:7 ~ issue_62878[317d]::main[0]), local_id: 1 } }`
2729

2830
error[E0308]: mismatched types
2931
--> $DIR/issue-62878.rs:7:15

0 commit comments

Comments
 (0)