Skip to content

Commit f06fabc

Browse files
committed
add tests
1 parent 3989e97 commit f06fabc

File tree

10 files changed

+206
-4
lines changed

10 files changed

+206
-4
lines changed

compiler/rustc_hir_analysis/src/check/compare_eii.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ use std::borrow::Cow;
22
use std::iter;
33

44
use rustc_data_structures::fx::FxIndexSet;
5-
use rustc_errors::{Applicability, E0053, struct_span_code_err};
5+
use rustc_errors::{
6+
Applicability, Applicability, E0053, E0053, struct_span_code_err, struct_span_code_err,
7+
};
68
use rustc_hir::def_id::{DefId, LocalDefId};
7-
use rustc_hir::{self as hir, HirId, ItemKind};
9+
use rustc_hir::{self as hir, self as hir, HirId, HirId, ItemKind, ItemKind};
810
use rustc_infer::infer::{self, InferCtxt, TyCtxtInferExt};
911
use rustc_infer::traits::{ObligationCause, ObligationCauseCode};
1012
use rustc_middle::ty;
1113
use rustc_middle::ty::TyCtxt;
12-
use rustc_middle::ty::error::{ExpectedFound, TypeError};
14+
use rustc_middle::ty::error::{ExpectedFound, ExpectedFound, TypeError, TypeError};
1315
use rustc_span::{ErrorGuaranteed, Ident, Span};
1416
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
1517
use rustc_trait_selection::regions::InferCtxtRegionExt;
@@ -63,7 +65,7 @@ pub(crate) fn compare_eii_function_types<'tcx>(
6365
let external_impl_sig = ocx.normalize(&norm_cause, param_env, external_impl_sig);
6466
debug!(?external_impl_sig);
6567

66-
let declaration_sig = tcx.fn_sig(declaration).no_bound_vars().expect("no bound vars");
68+
let declaration_sig = tcx.fn_sig(declaration).instantiate_identity();
6769
let declaration_sig =
6870
tcx.liberate_late_bound_regions(external_impl.to_def_id(), declaration_sig);
6971
let declaration_sig = ocx.normalize(&norm_cause, param_env, declaration_sig);

tests/ui/eii/subtype_1.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ compile-flags: --crate-type rlib
2+
//@ check-pass
3+
#![feature(eii)]
4+
#![feature(decl_macro)]
5+
#![feature(rustc_attrs)]
6+
#![feature(eii_internals)]
7+
8+
#[core::eii_macro_for(bar)]
9+
#[rustc_builtin_macro(eii_macro)]
10+
macro foo() {
11+
12+
}
13+
14+
unsafe extern "Rust" {
15+
safe fn bar<'a, 'b>(x: &'b u64) -> &'a u64;
16+
}
17+
18+
#[foo]
19+
fn other<'a, 'b>(x: &'b u64) -> &'b u64 {
20+
&0
21+
}
22+
23+
fn main() {
24+
bar(&0);
25+
}

tests/ui/eii/subtype_2.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ compile-flags: --crate-type rlib
2+
//@ check-pass
3+
#![feature(eii)]
4+
#![feature(decl_macro)]
5+
#![feature(rustc_attrs)]
6+
#![feature(eii_internals)]
7+
8+
#[core::eii_macro_for(bar)]
9+
#[rustc_builtin_macro(eii_macro)]
10+
macro foo() {
11+
12+
}
13+
14+
unsafe extern "Rust" {
15+
safe fn bar<'a>(x: &'static u64) -> &'a u64;
16+
}
17+
18+
#[foo]
19+
fn other<'a>(x: &'a u64) -> &'static u64 {
20+
&0
21+
}
22+
23+
fn main() {
24+
bar(&0);
25+
}

tests/ui/eii/unsafe_impl_err.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ compile-flags: --crate-type rlib
2+
#![feature(eii)]
3+
#![feature(decl_macro)]
4+
#![feature(rustc_attrs)]
5+
#![feature(eii_internals)]
6+
7+
#[core::eii_macro_for(bar, "unsafe")]
8+
#[rustc_builtin_macro(eii_macro)]
9+
macro foo() {
10+
11+
}
12+
13+
unsafe extern "Rust" {
14+
safe fn bar(x: u64) -> u64;
15+
}
16+
17+
#[foo] //~ ERROR `#[foo]` is unsafe to implement
18+
fn other(x: u64) -> u64 {
19+
x
20+
}
21+
22+
fn main() {
23+
bar(0);
24+
}

tests/ui/eii/unsafe_impl_err.stderr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: `#[foo]` is unsafe to implement
2+
--> $DIR/unsafe_impl_err.rs:17:1
3+
|
4+
LL | #[foo]
5+
| ^^^^^^
6+
|
7+
help: wrap the attribute in `unsafe(...)`
8+
|
9+
LL | #[unsafe(foo)]
10+
| +++++++ +
11+
12+
error: aborting due to 1 previous error
13+

tests/ui/eii/unsafe_impl_ok.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ compile-flags: --crate-type rlib
2+
//@ check-pass
3+
#![feature(eii)]
4+
#![feature(decl_macro)]
5+
#![feature(rustc_attrs)]
6+
#![feature(eii_internals)]
7+
8+
#[core::eii_macro_for(bar, "unsafe")]
9+
#[rustc_builtin_macro(eii_macro)]
10+
macro foo() {
11+
12+
}
13+
14+
unsafe extern "Rust" {
15+
safe fn bar(x: u64) -> u64;
16+
}
17+
18+
#[unsafe(foo)]
19+
fn other(x: u64) -> u64 {
20+
x
21+
}
22+
23+
fn main() {
24+
bar(0);
25+
}

tests/ui/eii/wrong_ret_ty.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ compile-flags: --crate-type rlib
2+
#![feature(eii)]
3+
#![feature(decl_macro)]
4+
#![feature(rustc_attrs)]
5+
#![feature(eii_internals)]
6+
7+
#[core::eii_macro_for(bar)]
8+
#[rustc_builtin_macro(eii_macro)]
9+
macro foo() {
10+
11+
}
12+
13+
unsafe extern "Rust" {
14+
safe fn bar(x: u64) -> u64;
15+
}
16+
17+
#[foo]
18+
fn other(_x: u64) {
19+
//~^ ERROR function `other` has a type that is incompatible with the declaration
20+
21+
}
22+
23+
fn main() {
24+
bar(0);
25+
}

tests/ui/eii/wrong_ret_ty.stderr

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0053]: function `other` has a type that is incompatible with the declaration
2+
--> $DIR/wrong_ret_ty.rs:18:18
3+
|
4+
LL | fn other(_x: u64) {
5+
| ^ expected `u64`, found `()`
6+
|
7+
note: type in declaration
8+
--> $DIR/wrong_ret_ty.rs:14:28
9+
|
10+
LL | safe fn bar(x: u64) -> u64;
11+
| ^^^
12+
= note: expected signature `fn(_) -> u64`
13+
found signature `fn(_) -> ()`
14+
help: change the output type to match the declaration
15+
|
16+
LL | fn other(_x: u64) -> u64 {
17+
| ++++++
18+
19+
error: aborting due to 1 previous error
20+
21+
For more information about this error, try `rustc --explain E0053`.

tests/ui/eii/wrong_ty.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ compile-flags: --crate-type rlib
2+
#![feature(eii)]
3+
#![feature(decl_macro)]
4+
#![feature(rustc_attrs)]
5+
#![feature(eii_internals)]
6+
7+
#[core::eii_macro_for(bar)]
8+
#[rustc_builtin_macro(eii_macro)]
9+
macro foo() {
10+
11+
}
12+
13+
unsafe extern "Rust" {
14+
safe fn bar(x: u64) -> u64;
15+
}
16+
17+
#[foo]
18+
fn other() -> u64 {
19+
//~^ ERROR function `other` has a type that is incompatible with the declaration
20+
3
21+
}
22+
23+
fn main() {
24+
bar(0);
25+
}

tests/ui/eii/wrong_ty.stderr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0053]: function `other` has a type that is incompatible with the declaration
2+
--> $DIR/wrong_ty.rs:18:1
3+
|
4+
LL | fn other() -> u64 {
5+
| ^^^^^^^^^^^^^^^^^ incorrect number of function parameters
6+
|
7+
note: type in declaration
8+
--> $DIR/wrong_ty.rs:14:5
9+
|
10+
LL | safe fn bar(x: u64) -> u64;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= note: expected signature `fn(u64) -> _`
13+
found signature `fn() -> _`
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0053`.

0 commit comments

Comments
 (0)