Auto merge of #54997 - davidtwco:issue-54896, r=nagisa

The #[panic_handler] attribute can be applied to non-functions

Fixes #54896.

This commit extends the existing lang items functionality to assert
that the `#[lang_item]` attribute is only found on the appropriate item
for any given lang item. That is, language items representing traits
must only ever have their corresponding attribute placed on a trait, for
example.

r? @nagisa
This commit is contained in:
bors 2018-10-13 11:52:49 +00:00
commit 24faa97589
9 changed files with 286 additions and 153 deletions

View file

@ -10,7 +10,7 @@
#![feature(lang_items)]
#[lang = "panic_impl"]
#[lang = "arc"]
struct Foo; //~ ERROR E0152
fn main() {

View file

@ -1,10 +1,10 @@
error[E0152]: duplicate lang item found: `panic_impl`.
error[E0152]: duplicate lang item found: `arc`.
--> $DIR/E0152.rs:14:1
|
LL | struct Foo; //~ ERROR E0152
| ^^^^^^^^^^^
|
= note: first defined in crate `std`.
= note: first defined in crate `alloc`.
error: aborting due to previous error

View file

@ -0,0 +1,17 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(lang_items)]
// Arc is expected to be a struct, so this will error.
#[lang = "arc"]
static X: u32 = 42;
fn main() {}

View file

@ -0,0 +1,9 @@
error[E0718]: `arc` language item must be applied to a struct
--> $DIR/E0718.rs:14:1
|
LL | #[lang = "arc"]
| ^^^^^^^^^^^^^^^ attribute should be applied to a struct, not a static item
error: aborting due to previous error
For more information about this error, try `rustc --explain E0718`.

View file

@ -0,0 +1,18 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags:-C panic=abort
#![no_std]
#![no_main]
#[panic_handler]
#[no_mangle]
static X: u32 = 42;

View file

@ -0,0 +1,11 @@
error[E0718]: `panic_impl` language item must be applied to a function
--> $DIR/panic-handler-wrong-location.rs:16:1
|
LL | #[panic_handler]
| ^^^^^^^^^^^^^^^^ attribute should be applied to a function, not a static item
error: `#[panic_handler]` function required, but not found
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0718`.