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

Report an error if a lang item has the wrong number of generic arguments #85339

Merged
merged 3 commits into from
May 18, 2021

Conversation

FabianWolff
Copy link
Contributor

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:

#![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`.

@rust-highfive
Copy link
Collaborator

r? @varkor

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 15, 2021
Copy link
Member

@varkor varkor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks – this is looking pretty good. Just have a couple of comments.

compiler/rustc_passes/src/lang_items.rs Outdated Show resolved Hide resolved
compiler/rustc_passes/src/lang_items.rs Show resolved Hide resolved
compiler/rustc_typeck/src/check/op.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/place_op.rs Outdated Show resolved Hide resolved
compiler/rustc_passes/src/lang_items.rs Outdated Show resolved Hide resolved
Copy link
Member

@varkor varkor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could revert the change in collect_item and address the comment in has_expected_num_generic_args, I think this is good to go.

compiler/rustc_typeck/src/check/mod.rs Outdated Show resolved Hide resolved
@FabianWolff
Copy link
Contributor Author

If you could revert the change in collect_item and address the comment in has_expected_num_generic_args, I think this is good to go.

Done. Thanks for your suggestions and for taking the time to review this PR!

@varkor
Copy link
Member

varkor commented May 17, 2021

Thanks for fixing this, @FabianWolff!

@bors r+

@bors
Copy link
Contributor

bors commented May 17, 2021

📌 Commit 7b30198 has been approved by varkor

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 17, 2021
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 18, 2021
Report an error if a lang item has the wrong number of generic arguments

This pull request fixes rust-lang#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 rust-lang#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`.
```
bors added a commit to rust-lang-ci/rust that referenced this pull request May 18, 2021
…laumeGomez

Rollup of 7 pull requests

Successful merges:

 - rust-lang#84587 (rustdoc: Make "rust code block is empty" and "could not parse code block" warnings a lint (`INVALID_RUST_CODEBLOCKS`))
 - rust-lang#85280 (Toggle-wrap items differently than top-doc.)
 - rust-lang#85338 (Implement more Iterator methods on core::iter::Repeat)
 - rust-lang#85339 (Report an error if a lang item has the wrong number of generic arguments)
 - rust-lang#85369 (Suggest borrowing if a trait implementation is found for &/&mut <type>)
 - rust-lang#85393 (Suppress spurious errors inside `async fn`)
 - rust-lang#85415 (Clean up remnants of BorrowOfPackedField)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 1bfd987 into rust-lang:master May 18, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
5 participants