You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rollup merge of #135044 - compiler-errors:better-infer-suggestions-in-const, r=oli-obk
Improve infer (`_`) suggestions in `const`s and `static`s
Fixes#135010.
This PR does a few things to (imo) greatly improve the error message when users write something like `static FOO: [i32; _] = [1, 2, 3]`.
Firstly, it adapts the recovery code for when we encounter `_` in a const/static to work a bit more like `fn foo() -> _`, and removes the somewhat redundant query `diagnostic_only_typeck`.
Secondly, it changes the lowering for `[T; _]` to always lower under the `feature(generic_arg_infer)` logic to `ConstArgKind::Infer`. We still issue the feature error, so it's not doing anything *observable* on the good path, but it does mean that we no longer erroneously interpret `[T; _]`'s array length as a `_` **wildcard expression** (à la destructuring assignment, like `(_, y) = expr`).
Lastly it makes the suggestions verbose and fixes (well, suppresses) a bug with stashing and suggestions.
r? oli-obk
| help: replace with an appropriate return type: `impl Iterator<Item = usize>`
588
621
589
622
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
590
-
--> $DIR/typeck_type_placeholder_item.rs:230:10
623
+
--> $DIR/typeck_type_placeholder_item.rs:231:10
591
624
|
592
625
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
593
626
| ^ not allowed in type signatures
594
627
|
595
-
note: however, the inferred type `Map<Filter<Range<i32>, {closure@typeck_type_placeholder_item.rs:230:29}>, {closure@typeck_type_placeholder_item.rs:230:49}>` cannot be named
596
-
--> $DIR/typeck_type_placeholder_item.rs:230:14
628
+
note: however, the inferred type `Map<Filter<Range<i32>, {closure@typeck_type_placeholder_item.rs:231:29}>, {closure@typeck_type_placeholder_item.rs:231:49}>` cannot be named
629
+
--> $DIR/typeck_type_placeholder_item.rs:231:14
597
630
|
598
631
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
599
632
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -668,23 +701,31 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
668
701
LL | type F: std::ops::Fn(_);
669
702
| ^ not allowed in type signatures
670
703
671
-
error[E0015]: cannot call non-const method `<std::ops::Range<i32> as Iterator>::filter::<{closure@$DIR/typeck_type_placeholder_item.rs:230:29: 230:32}>` in constants
672
-
--> $DIR/typeck_type_placeholder_item.rs:230:22
704
+
error[E0015]: cannot call non-const function `map::<u8>` in constants
705
+
--> $DIR/typeck_type_placeholder_item.rs:222:22
706
+
|
707
+
LL | const _: Option<_> = map(value);
708
+
| ^^^^^^^^^^
709
+
|
710
+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
711
+
712
+
error[E0015]: cannot call non-const method `<std::ops::Range<i32> as Iterator>::filter::<{closure@$DIR/typeck_type_placeholder_item.rs:231:29: 231:32}>` in constants
713
+
--> $DIR/typeck_type_placeholder_item.rs:231:22
673
714
|
674
715
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
675
716
| ^^^^^^^^^^^^^^^^^^^^^^
676
717
|
677
718
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
678
719
679
-
error[E0015]: cannot call non-const method `<Filter<std::ops::Range<i32>, {closure@$DIR/typeck_type_placeholder_item.rs:230:29: 230:32}> as Iterator>::map::<i32, {closure@$DIR/typeck_type_placeholder_item.rs:230:49: 230:52}>` in constants
680
-
--> $DIR/typeck_type_placeholder_item.rs:230:45
720
+
error[E0015]: cannot call non-const method `<Filter<std::ops::Range<i32>, {closure@$DIR/typeck_type_placeholder_item.rs:231:29: 231:32}> as Iterator>::map::<i32, {closure@$DIR/typeck_type_placeholder_item.rs:231:49: 231:52}>` in constants
721
+
--> $DIR/typeck_type_placeholder_item.rs:231:45
681
722
|
682
723
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
683
724
| ^^^^^^^^^^^^^^
684
725
|
685
726
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
686
727
687
-
error: aborting due to 74 previous errors
728
+
error: aborting due to 75 previous errors
688
729
689
730
Some errors have detailed explanations: E0015, E0046, E0121, E0282, E0403.
690
731
For more information about an error, try `rustc --explain E0015`.
0 commit comments