-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #85339 - FabianWolff:issue-83893, r=varkor
Report an error if a lang item has the wrong number of generic arguments This pull request fixes #83893. The issue is that the lang item code currently checks whether the lang item has the correct item kind (e.g. a `#[lang="add"]` has to be a trait), but not whether the item has the correct number of generic arguments. This can lead to an "index out of bounds" ICE when the compiler tries to create more substitutions than there are suitable types available (if the lang item was declared with too many generic arguments). For instance, here is a reduced ("reduced" in the sense that it does not trigger additional errors) version of the example given in #83893: ```rust #![feature(lang_items,no_core)] #![no_core] #![crate_type="lib"] #[lang = "sized"] trait MySized {} #[lang = "add"] trait MyAdd<'a, T> {} fn ice() { let r = 5; let a = 6; r + a } ``` On current nightly, this immediately causes an ICE without any warnings or errors emitted. With the changes in this PR, however, I get no ICE and two errors: ``` error[E0718]: `add` language item must be applied to a trait with 1 generic argument --> pr-ex.rs:8:1 | 8 | #[lang = "add"] | ^^^^^^^^^^^^^^^ 9 | trait MyAdd<'a, T> {} | ------- this trait has 2 generic arguments, not 1 error[E0369]: cannot add `{integer}` to `{integer}` --> pr-ex.rs:14:7 | 14 | r + a | - ^ - {integer} | | | {integer} error: aborting due to 2 previous errors Some errors have detailed explanations: E0369, E0718. For more information about an error, try `rustc --explain E0369`. ```
- Loading branch information
Showing
13 changed files
with
263 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Checks whether declaring a lang item with the wrong number | ||
// of generic arguments crashes the compiler (issue #83893). | ||
|
||
#![feature(lang_items,no_core)] | ||
#![no_core] | ||
#![crate_type="lib"] | ||
|
||
#[lang = "sized"] | ||
trait MySized {} | ||
|
||
#[lang = "add"] | ||
trait MyAdd<'a, T> {} | ||
//~^^ ERROR: `add` language item must be applied to a trait with 1 generic argument [E0718] | ||
|
||
fn ice() { | ||
let r = 5; | ||
let a = 6; | ||
r + a | ||
//~^ ERROR: cannot add `{integer}` to `{integer}` [E0369] | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/ui/lang-items/wrong-number-generic-args-add.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error[E0718]: `add` language item must be applied to a trait with 1 generic argument | ||
--> $DIR/wrong-number-generic-args-add.rs:11:1 | ||
| | ||
LL | #[lang = "add"] | ||
| ^^^^^^^^^^^^^^^ | ||
LL | trait MyAdd<'a, T> {} | ||
| ------- this trait has 2 generic arguments, not 1 | ||
|
||
error[E0369]: cannot add `{integer}` to `{integer}` | ||
--> $DIR/wrong-number-generic-args-add.rs:18:7 | ||
| | ||
LL | r + a | ||
| - ^ - {integer} | ||
| | | ||
| {integer} | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors have detailed explanations: E0369, E0718. | ||
For more information about an error, try `rustc --explain E0369`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Checks whether declaring a lang item with the wrong number | ||
// of generic arguments crashes the compiler (issue #83893). | ||
|
||
#![feature(lang_items,no_core)] | ||
#![no_core] | ||
#![crate_type="lib"] | ||
|
||
#[lang = "sized"] | ||
trait MySized {} | ||
|
||
#[lang = "index"] | ||
trait MyIndex<'a, T> {} | ||
//~^^ ERROR: `index` language item must be applied to a trait with 1 generic argument [E0718] | ||
|
||
fn ice() { | ||
let arr = [0; 5]; | ||
let _ = arr[2]; | ||
//~^ ERROR: cannot index into a value of type `[{integer}; 5]` [E0608] | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/ui/lang-items/wrong-number-generic-args-index.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
error[E0718]: `index` language item must be applied to a trait with 1 generic argument | ||
--> $DIR/wrong-number-generic-args-index.rs:11:1 | ||
| | ||
LL | #[lang = "index"] | ||
| ^^^^^^^^^^^^^^^^^ | ||
LL | trait MyIndex<'a, T> {} | ||
| ------- this trait has 2 generic arguments, not 1 | ||
|
||
error[E0608]: cannot index into a value of type `[{integer}; 5]` | ||
--> $DIR/wrong-number-generic-args-index.rs:17:13 | ||
| | ||
LL | let _ = arr[2]; | ||
| ^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
Some errors have detailed explanations: E0608, E0718. | ||
For more information about an error, try `rustc --explain E0608`. |