@@ -419,6 +419,74 @@ of a loop. Without a loop to break out of or continue in, no sensible action can
419
419
be taken.
420
420
"## ,
421
421
422
+ E0282 : r##"
423
+ This error indicates that type inference did not result in one unique possible
424
+ type, and extra information is required. In most cases this can be provided
425
+ by adding a type annotation. Sometimes you need to specify a generic type
426
+ parameter manually.
427
+
428
+ A common example is the `collect` method on `Iterator`. It has a generic type
429
+ parameter with a `FromIterator` bound, which for a `char` iterator is
430
+ implemented by `Vec` and `String` among others. Consider the following snippet
431
+ that reverses the characters of a string:
432
+
433
+ ```
434
+ let x = "hello".chars().rev().collect();
435
+ ```
436
+
437
+ In this case, the compiler cannot infer what the type of `x` should be:
438
+ `Vec<char>` and `String` are both suitable candidates. To specify which type to
439
+ use, you can use a type annotation on `x`:
440
+
441
+ ```
442
+ let x: Vec<char> = "hello".chars().rev().collect();
443
+ ```
444
+
445
+ It is not necessary to annotate the full type. Once the ambiguity is resolved,
446
+ the compiler can infer the rest:
447
+
448
+ ```
449
+ let x: Vec<_> = "hello".chars().rev().collect();
450
+ ```
451
+
452
+ Another way to provide the compiler with enough information, is to specify the
453
+ generic type parameter:
454
+
455
+ ```
456
+ let x = "hello".chars().rev().collect::<Vec<char>>();
457
+ ```
458
+
459
+ Again, you need not specify the full type if the compiler can infer it:
460
+
461
+ ```
462
+ let x = "hello".chars().rev().collect::<Vec<_>>();
463
+ ```
464
+
465
+ Apart from a method or function with a generic type parameter, this error can
466
+ occur when a type parameter of a struct or trait cannot be inferred. In that
467
+ case it is not always possible to use a type annotation, because all candidates
468
+ have the same return type. For instance:
469
+
470
+ ```
471
+ struct Foo<T> {
472
+ // Some fields omitted.
473
+ }
474
+
475
+ impl<T> Foo<T> {
476
+ fn bar() -> i32 {
477
+ 0
478
+ }
479
+
480
+ fn baz() {
481
+ let number = Foo::bar();
482
+ }
483
+ }
484
+ ```
485
+
486
+ This will fail because the compiler does not know which instance of `Foo` to
487
+ call `bar` on. Change `Foo::bar()` to `Foo::<T>::bar()` to resolve the error.
488
+ "## ,
489
+
422
490
E0296 : r##"
423
491
This error indicates that the given recursion limit could not be parsed. Ensure
424
492
that the value provided is a positive integer between quotes, like so:
@@ -524,10 +592,65 @@ number cannot be negative.
524
592
E0307 : r##"
525
593
The length of an array is part of its type. For this reason, this length must be
526
594
a compile-time constant.
595
+ "## ,
596
+
597
+ E0308 : r##"
598
+ This error occurs when the compiler was unable to infer the concrete type of a
599
+ variable. This error can occur for several cases, the most common of which is a
600
+ mismatch in the expected type that the compiler inferred for a variable's
601
+ initializing expression, and the actual type explicitly assigned to the
602
+ variable.
603
+
604
+ For example:
605
+
606
+ let x: i32 = "I am not a number!";
607
+ // ~~~ ~~~~~~~~~~~~~~~~~~~~
608
+ // | |
609
+ // | initializing expression;
610
+ // | compiler infers type `&str`
611
+ // |
612
+ // type `i32` assigned to variable `x`
613
+ "## ,
614
+
615
+ E0309 : r##"
616
+ Types in type definitions have lifetimes associated with them that represent
617
+ how long the data stored within them is guaranteed to be live. This lifetime
618
+ must be as long as the data needs to be alive, and missing the constraint that
619
+ denotes this will cause this error.
620
+
621
+ // This won't compile because T is not constrained, meaning the data
622
+ // stored in it is not guaranteed to last as long as the reference
623
+ struct Foo<'a, T> {
624
+ foo: &'a T
625
+ }
626
+
627
+ // This will compile, because it has the constraint on the type parameter
628
+ struct Foo<'a, T: 'a> {
629
+ foo: &'a T
630
+ }
631
+ "## ,
632
+
633
+ E0310 : r##"
634
+ Types in type definitions have lifetimes associated with them that represent
635
+ how long the data stored within them is guaranteed to be live. This lifetime
636
+ must be as long as the data needs to be alive, and missing the constraint that
637
+ denotes this will cause this error.
638
+
639
+ // This won't compile because T is not constrained to the static lifetime
640
+ // the reference needs
641
+ struct Foo<T> {
642
+ foo: &'static T
643
+ }
644
+
645
+ // This will compile, because it has the constraint on the type parameter
646
+ struct Foo<T: 'static> {
647
+ foo: &'static T
648
+ }
527
649
"##
528
650
529
651
}
530
652
653
+
531
654
register_diagnostics ! {
532
655
E0011 ,
533
656
E0012 ,
@@ -562,7 +685,6 @@ register_diagnostics! {
562
685
E0279 , // requirement is not satisfied
563
686
E0280 , // requirement is not satisfied
564
687
E0281 , // type implements trait but other trait is required
565
- E0282 , // unable to infer enough type information about
566
688
E0283 , // cannot resolve type
567
689
E0284 , // cannot resolve type
568
690
E0285 , // overflow evaluation builtin bounds
@@ -571,9 +693,6 @@ register_diagnostics! {
571
693
E0300 , // unexpanded macro
572
694
E0304 , // expected signed integer constant
573
695
E0305 , // expected constant
574
- E0308 ,
575
- E0309 , // thing may not live long enough
576
- E0310 , // thing may not live long enough
577
696
E0311 , // thing may not live long enough
578
697
E0312 , // lifetime of reference outlives lifetime of borrowed content
579
698
E0313 , // lifetime of borrowed pointer outlives lifetime of captured variable
0 commit comments