rust-analyzer/crates/hir-def/src/item_tree/tests.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

458 lines
9.2 KiB
Rust
Raw Normal View History

2021-05-21 21:45:27 +00:00
use expect_test::{expect, Expect};
use test_fixture::WithFixture;
2021-05-21 21:45:27 +00:00
use crate::{db::DefDatabase, test_db::TestDB};
fn check(ra_fixture: &str, expect: Expect) {
let (db, file_id) = TestDB::with_single_file(ra_fixture);
let item_tree = db.file_item_tree(file_id.into());
let pretty = item_tree.pretty_print(&db);
2021-05-21 21:45:27 +00:00
expect.assert_eq(&pretty);
}
#[test]
fn imports() {
check(
r#"
//! file comment
#![no_std]
//! another file comment
extern crate self as renamed;
pub(super) extern crate bli;
pub use crate::path::{nested, items as renamed, Trait as _};
use globs::*;
/// docs on import
use crate::{A, B};
use a::{c, d::{e}};
2021-05-21 21:45:27 +00:00
"#,
expect![[r##"
2022-04-14 12:24:27 +00:00
#![doc = " file comment"]
#![no_std]
#![doc = " another file comment"]
2021-05-21 21:45:27 +00:00
2024-01-16 09:47:28 +00:00
// AstId: 1
2021-05-21 21:45:27 +00:00
pub(self) extern crate self as renamed;
2024-01-16 09:47:28 +00:00
// AstId: 2
2021-05-21 21:45:27 +00:00
pub(super) extern crate bli;
2024-01-16 09:47:28 +00:00
// AstId: 3
pub use crate::path::{nested, items as renamed, Trait as _};
2021-05-21 21:45:27 +00:00
2024-01-16 09:47:28 +00:00
// AstId: 4
pub(self) use globs::*;
2021-05-21 21:45:27 +00:00
2022-04-14 12:24:27 +00:00
#[doc = " docs on import"]
2024-01-16 09:47:28 +00:00
// AstId: 5
pub(self) use crate::{A, B};
2021-05-21 21:45:27 +00:00
2024-01-16 09:47:28 +00:00
// AstId: 6
pub(self) use a::{c, d::{e}};
2021-05-21 21:45:27 +00:00
"##]],
);
}
#[test]
fn extern_blocks() {
check(
r#"
#[on_extern_block]
extern "C" {
#[on_extern_type]
type ExType;
#[on_extern_static]
static EX_STATIC: u8;
#[on_extern_fn]
fn ex_fn();
}
"#,
expect![[r##"
2022-04-14 12:24:27 +00:00
#[on_extern_block]
2024-01-16 09:47:28 +00:00
// AstId: 1
2021-05-21 21:45:27 +00:00
extern "C" {
2022-04-14 12:24:27 +00:00
#[on_extern_type]
2024-01-16 09:47:28 +00:00
// AstId: 2
2021-12-07 16:31:26 +00:00
pub(self) type ExType;
2021-05-21 21:45:27 +00:00
2022-04-14 12:24:27 +00:00
#[on_extern_static]
2024-01-16 09:47:28 +00:00
// AstId: 3
2021-12-07 16:31:26 +00:00
pub(self) static EX_STATIC: u8 = _;
2021-05-21 21:45:27 +00:00
2022-04-14 12:24:27 +00:00
#[on_extern_fn]
2024-01-16 09:47:28 +00:00
// AstId: 4
2021-05-21 21:45:27 +00:00
pub(self) fn ex_fn() -> ();
}
"##]],
);
}
#[test]
fn adts() {
check(
r#"
struct Unit;
#[derive(Debug)]
struct Struct {
/// fld docs
fld: (),
}
struct Tuple(#[attr] u8);
union Ize {
a: (),
b: (),
}
enum E {
/// comment on Unit
Unit,
/// comment on Tuple
Tuple(u8),
Struct {
/// comment on a: u8
a: u8,
}
}
"#,
2024-01-16 09:47:28 +00:00
expect![[r#"
// AstId: 1
2021-05-21 21:45:27 +00:00
pub(self) struct Unit;
2022-04-14 12:24:27 +00:00
#[derive(Debug)]
2024-01-16 09:47:28 +00:00
// AstId: 2
2021-05-21 21:45:27 +00:00
pub(self) struct Struct {
2024-01-16 09:47:28 +00:00
// AstId: 6
2022-04-14 12:24:27 +00:00
#[doc = " fld docs"]
2021-05-21 21:45:27 +00:00
pub(self) fld: (),
}
2024-01-16 09:47:28 +00:00
// AstId: 3
2021-05-21 21:45:27 +00:00
pub(self) struct Tuple(
2024-01-16 09:47:28 +00:00
// AstId: 7
2022-04-14 12:24:27 +00:00
#[attr]
2021-05-21 21:45:27 +00:00
pub(self) 0: u8,
);
2024-01-16 09:47:28 +00:00
// AstId: 4
2021-05-21 21:45:27 +00:00
pub(self) union Ize {
2024-01-16 09:47:28 +00:00
// AstId: 8
2021-05-21 21:45:27 +00:00
pub(self) a: (),
2024-01-16 09:47:28 +00:00
// AstId: 9
2021-05-21 21:45:27 +00:00
pub(self) b: (),
}
2024-01-16 09:47:28 +00:00
// AstId: 5
2021-05-21 21:45:27 +00:00
pub(self) enum E {
2024-01-16 09:47:28 +00:00
// AstId: 10
2022-04-14 12:24:27 +00:00
#[doc = " comment on Unit"]
2021-05-21 21:45:27 +00:00
Unit,
2024-01-16 09:47:28 +00:00
// AstId: 11
2022-04-14 12:24:27 +00:00
#[doc = " comment on Tuple"]
2021-05-21 21:45:27 +00:00
Tuple(
2024-01-16 09:47:28 +00:00
// AstId: 13
2021-05-21 21:45:27 +00:00
pub(self) 0: u8,
),
2024-01-16 09:47:28 +00:00
// AstId: 12
2021-05-21 21:45:27 +00:00
Struct {
2024-01-16 09:47:28 +00:00
// AstId: 14
2022-04-14 12:24:27 +00:00
#[doc = " comment on a: u8"]
2021-05-21 21:45:27 +00:00
pub(self) a: u8,
},
}
2024-01-16 09:47:28 +00:00
"#]],
2021-05-21 21:45:27 +00:00
);
}
#[test]
fn misc() {
check(
r#"
pub static mut ST: () = ();
const _: Anon = ();
#[attr]
fn f(#[attr] arg: u8, _: ()) {
#![inner_attr_in_fn]
}
trait Tr: SuperTrait + 'lifetime {
type Assoc: AssocBound = Default;
fn method(&self);
}
"#,
expect![[r#"
2024-01-16 09:47:28 +00:00
// AstId: 1
2021-05-21 21:45:27 +00:00
pub static mut ST: () = _;
2024-01-16 09:47:28 +00:00
// AstId: 2
2021-05-21 21:45:27 +00:00
pub(self) const _: Anon = _;
2022-04-14 12:24:27 +00:00
#[attr]
#[inner_attr_in_fn]
2024-01-16 09:47:28 +00:00
// AstId: 3
2021-05-21 21:45:27 +00:00
pub(self) fn f(
2022-04-14 12:24:27 +00:00
#[attr]
2024-01-16 09:47:28 +00:00
// AstId: 5
u8,
2024-01-16 09:47:28 +00:00
// AstId: 6
(),
2022-04-14 12:24:27 +00:00
) -> () { ... }
2021-05-21 21:45:27 +00:00
2024-01-16 09:47:28 +00:00
// AstId: 4
2021-05-27 13:52:15 +00:00
pub(self) trait Tr<Self>
where
Self: SuperTrait,
Self: 'lifetime
{
2024-01-16 09:47:28 +00:00
// AstId: 8
2021-05-21 21:45:27 +00:00
pub(self) type Assoc: AssocBound = Default;
2024-01-16 09:47:28 +00:00
// AstId: 9
2021-05-21 21:45:27 +00:00
pub(self) fn method(
2024-01-16 09:47:28 +00:00
// AstId: 10
self: &Self,
2021-05-21 21:45:27 +00:00
) -> ();
}
"#]],
2021-05-21 21:45:27 +00:00
);
}
#[test]
fn modules() {
check(
r#"
/// outer
mod inline {
//! inner
use super::*;
fn fn_in_module() {}
}
mod outline;
2021-05-21 21:45:27 +00:00
"#,
expect![[r##"
2022-04-14 12:24:27 +00:00
#[doc = " outer"]
#[doc = " inner"]
2024-01-16 09:47:28 +00:00
// AstId: 1
2021-05-21 21:45:27 +00:00
pub(self) mod inline {
2024-01-16 09:47:28 +00:00
// AstId: 3
pub(self) use super::*;
2021-05-21 21:45:27 +00:00
2024-01-16 09:47:28 +00:00
// AstId: 4
2022-04-14 12:24:27 +00:00
pub(self) fn fn_in_module() -> () { ... }
2021-05-21 21:45:27 +00:00
}
2024-01-16 09:47:28 +00:00
// AstId: 2
pub(self) mod outline;
2021-05-21 21:45:27 +00:00
"##]],
);
}
#[test]
fn macros() {
check(
r#"
macro_rules! m {
() => {};
}
pub macro m2() {}
m!();
"#,
expect![[r#"
// AstId: 1
2021-05-21 21:45:27 +00:00
macro_rules! m { ... }
// AstId: 2
2021-05-21 21:45:27 +00:00
pub macro m2 { ... }
// AstId: 3, SyntaxContext: 0, ExpandTo: Items
2021-05-21 21:45:27 +00:00
m!(...);
"#]],
);
}
2021-05-22 22:37:15 +00:00
#[test]
fn mod_paths() {
check(
r#"
struct S {
a: self::Ty,
b: super::SuperTy,
c: super::super::SuperSuperTy,
d: ::abs::Path,
e: crate::Crate,
f: plain::path::Ty,
}
"#,
expect![[r#"
2024-01-16 09:47:28 +00:00
// AstId: 1
2021-05-22 22:37:15 +00:00
pub(self) struct S {
2024-01-16 09:47:28 +00:00
// AstId: 2
2021-05-22 22:37:15 +00:00
pub(self) a: self::Ty,
2024-01-16 09:47:28 +00:00
// AstId: 3
2021-05-22 22:37:15 +00:00
pub(self) b: super::SuperTy,
2024-01-16 09:47:28 +00:00
// AstId: 4
2021-05-22 22:37:15 +00:00
pub(self) c: super::super::SuperSuperTy,
2024-01-16 09:47:28 +00:00
// AstId: 5
2021-05-22 22:37:15 +00:00
pub(self) d: ::abs::Path,
2024-01-16 09:47:28 +00:00
// AstId: 6
2021-05-22 22:37:15 +00:00
pub(self) e: crate::Crate,
2024-01-16 09:47:28 +00:00
// AstId: 7
2021-05-22 22:37:15 +00:00
pub(self) f: plain::path::Ty,
}
"#]],
)
}
#[test]
fn types() {
check(
r#"
struct S {
a: Mixed<'a, T, Item=(), OtherItem=u8>,
b: <Fully as Qualified>::Syntax,
c: <TypeAnchored>::Path::<'a>,
2021-06-29 23:34:51 +00:00
d: dyn for<'a> Trait<'a>,
2021-05-22 22:37:15 +00:00
}
"#,
expect![[r#"
2024-01-16 09:47:28 +00:00
// AstId: 1
2021-05-22 22:37:15 +00:00
pub(self) struct S {
2024-01-16 09:47:28 +00:00
// AstId: 2
2022-08-15 11:51:45 +00:00
pub(self) a: Mixed::<'a, T, Item = (), OtherItem = u8>,
2024-01-16 09:47:28 +00:00
// AstId: 3
2022-08-15 11:51:45 +00:00
pub(self) b: Qualified::<Self=Fully>::Syntax,
2024-01-16 09:47:28 +00:00
// AstId: 4
2022-08-15 11:51:45 +00:00
pub(self) c: <TypeAnchored>::Path::<'a>,
2024-01-16 09:47:28 +00:00
// AstId: 5
2022-08-15 11:51:45 +00:00
pub(self) d: dyn for<'a> Trait::<'a>,
2021-05-22 22:37:15 +00:00
}
"#]],
)
}
2021-05-22 23:00:17 +00:00
#[test]
fn generics() {
check(
r#"
struct S<'a, 'b: 'a, T: Copy + 'a + 'b, const K: u8 = 0> {
field: &'a &'b T,
}
2021-06-14 12:36:56 +00:00
struct Tuple<T: Copy, U: ?Sized>(T, U);
2021-05-22 23:00:17 +00:00
impl<'a, 'b: 'a, T: Copy + 'a + 'b, const K: u8 = 0> S<'a, 'b, T, K> {
fn f<G: 'a>(arg: impl Copy) -> impl Copy {}
}
enum Enum<'a, T, const U: u8> {}
union Union<'a, T, const U: u8> {}
2021-06-29 23:34:51 +00:00
trait Tr<'a, T: 'a>: Super where Self: for<'a> Tr<'a, T> {}
2021-05-22 23:00:17 +00:00
"#,
expect![[r#"
2024-01-16 09:47:28 +00:00
// AstId: 1
pub(self) struct S<'a, 'b, T, const K: u8>
where
T: Copy,
T: 'a,
T: 'b
{
2024-01-16 09:47:28 +00:00
// AstId: 8
pub(self) field: &'a &'b T,
2021-05-22 23:00:17 +00:00
}
2024-01-16 09:47:28 +00:00
// AstId: 2
2021-06-14 12:36:56 +00:00
pub(self) struct Tuple<T, U>(
2024-01-16 09:47:28 +00:00
// AstId: 9
pub(self) 0: T,
2024-01-16 09:47:28 +00:00
// AstId: 10
2021-06-14 12:36:56 +00:00
pub(self) 1: U,
)
where
2021-06-14 12:36:56 +00:00
T: Copy,
U: ?Sized;
2024-01-16 09:47:28 +00:00
// AstId: 3
2022-08-15 11:51:45 +00:00
impl<'a, 'b, T, const K: u8> S::<'a, 'b, T, K>
where
T: Copy,
T: 'a,
T: 'b
{
2024-01-16 09:47:28 +00:00
// AstId: 12
2021-09-21 13:37:47 +00:00
pub(self) fn f<G>(
2024-01-16 09:47:28 +00:00
// AstId: 13
impl Copy,
) -> impl Copy
where
2022-04-14 12:24:27 +00:00
G: 'a { ... }
2021-05-22 23:00:17 +00:00
}
2024-01-16 09:47:28 +00:00
// AstId: 4
2021-05-22 23:00:17 +00:00
pub(self) enum Enum<'a, T, const U: u8> {
}
2024-01-16 09:47:28 +00:00
// AstId: 5
2021-05-22 23:00:17 +00:00
pub(self) union Union<'a, T, const U: u8> {
}
2024-01-16 09:47:28 +00:00
// AstId: 6
2021-05-27 13:52:15 +00:00
pub(self) trait Tr<'a, Self, T>
where
Self: Super,
2021-06-29 23:34:51 +00:00
T: 'a,
2022-08-15 11:51:45 +00:00
Self: for<'a> Tr::<'a, T>
{
}
2021-05-22 23:00:17 +00:00
"#]],
)
}
2023-07-28 10:45:35 +00:00
#[test]
fn generics_with_attributes() {
check(
r#"
struct S<#[cfg(never)] T>;
struct S<A, B, #[cfg(never)] C>;
struct S<A, #[cfg(never)] B, C>;
2023-07-28 10:45:35 +00:00
"#,
expect![[r#"
2024-01-16 09:47:28 +00:00
// AstId: 1
2023-07-28 10:45:35 +00:00
pub(self) struct S<#[cfg(never)] T>;
// AstId: 2
pub(self) struct S<A, B, #[cfg(never)] C>;
// AstId: 3
pub(self) struct S<A, #[cfg(never)] B, C>;
2023-07-28 10:45:35 +00:00
"#]],
)
}
#[test]
fn pub_self() {
check(
r#"
pub(self) struct S;
"#,
expect![[r#"
2024-01-16 09:47:28 +00:00
// AstId: 1
pub(self) struct S;
"#]],
)
}