Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unwrap diag.code directly in note_and_explain_type_err #125774

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
diag.span_label(p_span, format!("{expected}this type parameter"));
}
diag.help("type parameters must be constrained to match other types");
if tcx.sess.teach(diag.code.unwrap()) {
if diag.code.is_some_and(|code| tcx.sess.teach(code)) {
diag.help(
"given a type parameter `T` and a method `foo`:
```
Expand Down Expand Up @@ -663,7 +663,7 @@ impl<T> Trait<T> for X {
https://doc.rust-lang.org/book/ch19-03-advanced-traits.html",
);
}
if tcx.sess.teach(diag.code.unwrap()) {
if diag.code.is_some_and(|code| tcx.sess.teach(code)) {
diag.help(
"given an associated type `T` and a method `foo`:
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![feature(specialization)]
#![allow(incomplete_features)]

trait Trait {
type Type;
}

impl Trait for i32 {
default type Type = i32;
}

struct Wrapper<const C: <i32 as Trait>::Type> {}
//~^ ERROR `<i32 as Trait>::Type` is forbidden as the type of a const generic parameter

impl<const C: usize> Wrapper<C> {}
//~^ ERROR the constant `C` is not of type `<i32 as Trait>::Type`
//~^^ ERROR mismatched types

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error: `<i32 as Trait>::Type` is forbidden as the type of a const generic parameter
--> $DIR/default-proj-ty-as-type-of-const-issue-125757.rs:12:25
|
LL | struct Wrapper<const C: <i32 as Trait>::Type> {}
| ^^^^^^^^^^^^^^^^^^^^
|
= note: the only supported types are integers, `bool` and `char`

error: the constant `C` is not of type `<i32 as Trait>::Type`
--> $DIR/default-proj-ty-as-type-of-const-issue-125757.rs:15:22
|
LL | impl<const C: usize> Wrapper<C> {}
| ^^^^^^^^^^ expected associated type, found `usize`
|
= help: consider constraining the associated type `<i32 as Trait>::Type` to `usize`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
note: required by a bound in `Wrapper`
--> $DIR/default-proj-ty-as-type-of-const-issue-125757.rs:12:16
|
LL | struct Wrapper<const C: <i32 as Trait>::Type> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Wrapper`

error[E0308]: mismatched types
--> $DIR/default-proj-ty-as-type-of-const-issue-125757.rs:15:30
|
LL | impl<const C: usize> Wrapper<C> {}
| ^ expected associated type, found `usize`
|
= note: expected associated type `<i32 as Trait>::Type`
found type `usize`
= help: consider constraining the associated type `<i32 as Trait>::Type` to `usize`
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0308`.
Loading