about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-05-18 14:08:51 +0200
committerGitHub <noreply@github.com>2021-05-18 14:08:51 +0200
commit1bfd987f6911ec463c8d1cc10f80dd77a883d92c (patch)
tree57a91ed45200ea25d8eac13a64f4c18d210ca63d /src/test
parenta181806b8cbd21736b86ee6ee2f4797c0a4a58b4 (diff)
parent7b301985faa73b1404cbc21ffe7c7f859a293448 (diff)
downloadrust-1bfd987f6911ec463c8d1cc10f80dd77a883d92c.tar.gz
rust-1bfd987f6911ec463c8d1cc10f80dd77a883d92c.zip
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`.
```
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/lang-items/lang-item-missing-generator.rs (renamed from src/test/ui/lang-item-missing-generator.rs)0
-rw-r--r--src/test/ui/lang-items/lang-item-missing-generator.stderr (renamed from src/test/ui/lang-item-missing-generator.stderr)0
-rw-r--r--src/test/ui/lang-items/lang-item-missing.rs (renamed from src/test/ui/lang-item-missing.rs)0
-rw-r--r--src/test/ui/lang-items/lang-item-missing.stderr (renamed from src/test/ui/lang-item-missing.stderr)0
-rw-r--r--src/test/ui/lang-items/wrong-number-generic-args-add.rs20
-rw-r--r--src/test/ui/lang-items/wrong-number-generic-args-add.stderr20
-rw-r--r--src/test/ui/lang-items/wrong-number-generic-args-index.rs19
-rw-r--r--src/test/ui/lang-items/wrong-number-generic-args-index.stderr18
8 files changed, 77 insertions, 0 deletions
diff --git a/src/test/ui/lang-item-missing-generator.rs b/src/test/ui/lang-items/lang-item-missing-generator.rs
index 0c329542928..0c329542928 100644
--- a/src/test/ui/lang-item-missing-generator.rs
+++ b/src/test/ui/lang-items/lang-item-missing-generator.rs
diff --git a/src/test/ui/lang-item-missing-generator.stderr b/src/test/ui/lang-items/lang-item-missing-generator.stderr
index fa13bf0b127..fa13bf0b127 100644
--- a/src/test/ui/lang-item-missing-generator.stderr
+++ b/src/test/ui/lang-items/lang-item-missing-generator.stderr
diff --git a/src/test/ui/lang-item-missing.rs b/src/test/ui/lang-items/lang-item-missing.rs
index 4e26343242e..4e26343242e 100644
--- a/src/test/ui/lang-item-missing.rs
+++ b/src/test/ui/lang-items/lang-item-missing.rs
diff --git a/src/test/ui/lang-item-missing.stderr b/src/test/ui/lang-items/lang-item-missing.stderr
index f7516c7d377..f7516c7d377 100644
--- a/src/test/ui/lang-item-missing.stderr
+++ b/src/test/ui/lang-items/lang-item-missing.stderr
diff --git a/src/test/ui/lang-items/wrong-number-generic-args-add.rs b/src/test/ui/lang-items/wrong-number-generic-args-add.rs
new file mode 100644
index 00000000000..9f4f2464a1e
--- /dev/null
+++ b/src/test/ui/lang-items/wrong-number-generic-args-add.rs
@@ -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]
+}
diff --git a/src/test/ui/lang-items/wrong-number-generic-args-add.stderr b/src/test/ui/lang-items/wrong-number-generic-args-add.stderr
new file mode 100644
index 00000000000..6f89441fd28
--- /dev/null
+++ b/src/test/ui/lang-items/wrong-number-generic-args-add.stderr
@@ -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`.
diff --git a/src/test/ui/lang-items/wrong-number-generic-args-index.rs b/src/test/ui/lang-items/wrong-number-generic-args-index.rs
new file mode 100644
index 00000000000..1d90e63dc54
--- /dev/null
+++ b/src/test/ui/lang-items/wrong-number-generic-args-index.rs
@@ -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]
+}
diff --git a/src/test/ui/lang-items/wrong-number-generic-args-index.stderr b/src/test/ui/lang-items/wrong-number-generic-args-index.stderr
new file mode 100644
index 00000000000..bc3f19ff276
--- /dev/null
+++ b/src/test/ui/lang-items/wrong-number-generic-args-index.stderr
@@ -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`.