rust/compiler/rustc_passes/src
Guillaume Gomez 1bfd987f69
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`.
```
2021-05-18 14:08:51 +02:00
..
liveness Use iter::zip in compiler/ 2021-03-26 09:32:31 -07:00
check_attr.rs entirely remove rustc_args_required_const attribute 2021-05-12 16:15:27 +02:00
check_const.rs remove redundant wrapping of return types of allow_internal_unstable() and rustc_allow_const_fn_unstable() 2021-02-21 18:11:27 +01:00
dead.rs Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
diagnostic_items.rs Use () for lang items. 2021-05-12 13:58:45 +02:00
entry.rs Use () for entry_fn. 2021-05-12 13:58:42 +02:00
hir_id_validator.rs GenericParam does not need to be a HIR owner. 2021-03-23 22:47:22 +01:00
hir_stats.rs ast/hir: Rename field-related structures 2021-03-16 11:41:24 +03:00
intrinsicck.rs Remove support for floating-point constants in asm! 2021-05-14 14:58:21 +01:00
lang_items.rs Rollup merge of #85339 - FabianWolff:issue-83893, r=varkor 2021-05-18 14:08:51 +02:00
layout_test.rs Only store a LocalDefId in hir::Item. 2021-02-15 19:32:10 +01:00
lib.rs fix feature use in rustc libs 2021-04-18 22:05:45 +02:00
lib_features.rs Use () for lang items. 2021-05-12 13:58:45 +02:00
liveness.rs Split span_to_string into span_to_diagnostic/embeddable_string 2021-05-11 00:04:12 +01:00
loops.rs Do not suggest using a break label when one is already present 2021-01-21 21:43:29 -08:00
naked_functions.rs Do not store attrs in FnKind. 2021-03-09 19:09:33 +01:00
reachable.rs Auto merge of #85178 - cjgillot:local-crate, r=oli-obk 2021-05-17 01:42:03 +00:00
region.rs Split span_to_string into span_to_diagnostic/embeddable_string 2021-05-11 00:04:12 +01:00
stability.rs Use () for lang items. 2021-05-12 13:58:45 +02:00
upvars.rs mv compiler to compiler/ 2020-08-30 18:45:07 +03:00
weak_lang_items.rs Remove hir::ForeignItem::attrs. 2021-03-09 19:23:07 +01:00