Skip to content

Commit ed5205f

Browse files
committed
Avoid unwrap diag.code directly
1 parent 21e6de7 commit ed5205f

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
182182
diag.span_label(p_span, format!("{expected}this type parameter"));
183183
}
184184
diag.help("type parameters must be constrained to match other types");
185-
if tcx.sess.teach(diag.code.unwrap()) {
185+
if diag.code.is_some_and(|code| tcx.sess.teach(code)) {
186186
diag.help(
187187
"given a type parameter `T` and a method `foo`:
188188
```
@@ -663,7 +663,7 @@ impl<T> Trait<T> for X {
663663
https://doc.rust-lang.org/book/ch19-03-advanced-traits.html",
664664
);
665665
}
666-
if tcx.sess.teach(diag.code.unwrap()) {
666+
if diag.code.is_some_and(|code| tcx.sess.teach(code)) {
667667
diag.help(
668668
"given an associated type `T` and a method `foo`:
669669
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(specialization)]
2+
#![allow(incomplete_features)]
3+
4+
trait Trait {
5+
type Type;
6+
}
7+
8+
impl Trait for i32 {
9+
default type Type = i32;
10+
}
11+
12+
struct Wrapper<const C: <i32 as Trait>::Type> {}
13+
//~^ ERROR `<i32 as Trait>::Type` is forbidden as the type of a const generic parameter
14+
15+
impl<const C: usize> Wrapper<C> {}
16+
//~^ ERROR the constant `C` is not of type `<i32 as Trait>::Type`
17+
//~^^ ERROR mismatched types
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: `<i32 as Trait>::Type` is forbidden as the type of a const generic parameter
2+
--> $DIR/default-proj-ty-as-type-of-const-issue-125757.rs:12:25
3+
|
4+
LL | struct Wrapper<const C: <i32 as Trait>::Type> {}
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: the only supported types are integers, `bool` and `char`
8+
9+
error: the constant `C` is not of type `<i32 as Trait>::Type`
10+
--> $DIR/default-proj-ty-as-type-of-const-issue-125757.rs:15:22
11+
|
12+
LL | impl<const C: usize> Wrapper<C> {}
13+
| ^^^^^^^^^^ expected associated type, found `usize`
14+
|
15+
= help: consider constraining the associated type `<i32 as Trait>::Type` to `usize`
16+
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
17+
note: required by a bound in `Wrapper`
18+
--> $DIR/default-proj-ty-as-type-of-const-issue-125757.rs:12:16
19+
|
20+
LL | struct Wrapper<const C: <i32 as Trait>::Type> {}
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Wrapper`
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/default-proj-ty-as-type-of-const-issue-125757.rs:15:30
25+
|
26+
LL | impl<const C: usize> Wrapper<C> {}
27+
| ^ expected associated type, found `usize`
28+
|
29+
= note: expected associated type `<i32 as Trait>::Type`
30+
found type `usize`
31+
= help: consider constraining the associated type `<i32 as Trait>::Type` to `usize`
32+
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
33+
34+
error: aborting due to 3 previous errors
35+
36+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)