Skip to content

Commit cb2e9e9

Browse files
committed
Add ui tests for new and modified errors
1 parent f21cea8 commit cb2e9e9

10 files changed

+84
-23
lines changed

macro/src/expand.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ fn expand_struct(namespace: &Namespace, strct: &Struct) -> TokenStream {
131131
quote!(#vis #field)
132132
});
133133
let type_id = type_id(namespace, ident);
134-
// TODO: Add ui test for mismatched alias kinds
135134
quote! {
136135
#doc
137136
#[derive(#(#derives),*)]

syntax/parse.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ fn parse_enum(cx: &mut Errors, item: ItemEnum) -> Result<Api> {
177177
fn parse_alias(cx: &mut Errors, item: ItemType, kind: AliasKind) -> Result<Api> {
178178
let generics = &item.generics;
179179
if !generics.params.is_empty() || generics.where_clause.is_some() {
180-
// TODO: Add ui test for this
181180
let type_token = item.type_token;
182181
let ident = &item.ident;
183182
let where_clause = &generics.where_clause;
@@ -417,14 +416,11 @@ fn parse_extern_verbatim(cx: &mut Errors, tokens: &TokenStream, lang: Lang) -> R
417416
let parse = |input: ParseStream| -> Result<ItemType> {
418417
// Test whether this is a type alias. This ends up parsing attributes twice but lets us emit
419418
// more useful errors that differentiate between an unsupported item and other alias parsing
420-
// errors.
421-
// TODO: Add ui test to verify this
419+
// errors while still reusing syn's ItemType parsing.
422420
let fork = input.fork();
423421
fork.call(Attribute::parse_outer)?;
424-
fork.parse::<Token![type]>().map_err(|_| {
425-
let span = fork.cursor().token_stream();
426-
Error::new_spanned(span, "unsupported foreign item")
427-
})?;
422+
fork.parse::<Token![type]>()
423+
.map_err(|_| Error::new_spanned(tokens, "unsupported foreign item"))?;
428424

429425
// Reuse alias parsing from syn
430426
input.parse()

tests/ui/alias_parse_errors.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Rustfmt mangles the extern type alias.
2+
// https://github.com/rust-lang/rustfmt/issues/4159
3+
// ...normally would add #[rustfmt::skip], but that seems to interfere with the error spans.
4+
#[cxx::bridge]
5+
mod ffi {
6+
extern "C" {
7+
fn unsupported_foreign_item() {}
8+
9+
type BadGeneric<T> = Bad;
10+
}
11+
12+
extern "Rust" {
13+
/// Incorrect.
14+
type Alias = crate::Type;
15+
}
16+
}
17+
18+
fn main() {}

tests/ui/alias_parse_errors.stderr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: unsupported foreign item
2+
--> $DIR/alias_parse_errors.rs:7:9
3+
|
4+
7 | fn unsupported_foreign_item() {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aliases with generic parameters are not allowed
8+
--> $DIR/alias_parse_errors.rs:9:9
9+
|
10+
9 | type BadGeneric<T> = Bad;
11+
| ^^^^^^^^^^^^^^^^^^
12+
13+
error: type alias in extern "Rust" block is not supported
14+
--> $DIR/alias_parse_errors.rs:14:9
15+
|
16+
14 | type Alias = crate::Type;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^

tests/ui/alias_wrong_kind.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#[cxx::bridge]
2+
mod here {
3+
struct Shared {
4+
z: usize,
5+
}
6+
7+
extern "C" {
8+
type C;
9+
}
10+
}
11+
12+
// Rustfmt mangles the extern type alias.
13+
// https://github.com/rust-lang/rustfmt/issues/4159
14+
#[rustfmt::skip]
15+
#[cxx::bridge]
16+
mod there {
17+
type C = crate::here::C;
18+
19+
extern "C" {
20+
type Shared = crate::here::Shared;
21+
}
22+
}
23+
24+
fn main() {}

tests/ui/alias_wrong_kind.stderr

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0271]: type mismatch resolving `<here::C as ExternType>::Kind == KindShared`
2+
--> $DIR/alias_wrong_kind.rs:15:1
3+
|
4+
15 | #[cxx::bridge]
5+
| ^^^^^^^^^^^^^^ expected struct `KindShared`, found struct `KindOpaqueCpp`
6+
|
7+
::: $WORKSPACE/src/extern_type.rs
8+
|
9+
| pub fn verify_extern_type<T: ExternType<Kind = Kind, Id = Id>, Kind, Id>() {}
10+
| ----------- required by this bound in `verify_extern_type`
11+
12+
error[E0271]: type mismatch resolving `<here::Shared as ExternType>::Kind == KindOpaqueCpp`
13+
--> $DIR/alias_wrong_kind.rs:15:1
14+
|
15+
15 | #[cxx::bridge]
16+
| ^^^^^^^^^^^^^^ expected struct `KindOpaqueCpp`, found struct `KindShared`
17+
|
18+
::: $WORKSPACE/src/extern_type.rs
19+
|
20+
| pub fn verify_extern_type<T: ExternType<Kind = Kind, Id = Id>, Kind, Id>() {}
21+
| ----------- required by this bound in `verify_extern_type`
File renamed without changes.

tests/ui/wrong_type_id.stderr renamed to tests/ui/alias_wrong_type_id.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0271]: type mismatch resolving `<StringPiece as ExternType>::Id == (f, o, l, l, y, (), B, y, t, e, R, a, n, g, e)`
2-
--> $DIR/wrong_type_id.rs:11:9
2+
--> $DIR/alias_wrong_type_id.rs:11:9
33
|
44
11 | type ByteRange = crate::here::StringPiece;
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected a tuple with 15 elements, found one with 17 elements

tests/ui/type_alias_rust.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/ui/type_alias_rust.stderr

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)