X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=crates%2Fhir_ty%2Fsrc%2Ftests%2Fmacros.rs;h=344e7293c59343fc9faee4ee7b24ebf304da6756;hb=0b53744f2d7e0694cd7207cca632fd6de1dc5bff;hp=b8e373ed889466f7218e0576da4c2bd16ac618ef;hpb=eccd0efedb230985c582edbf9d272bf5f0224acf;p=rust.git diff --git a/crates/hir_ty/src/tests/macros.rs b/crates/hir_ty/src/tests/macros.rs index b8e373ed889..344e7293c59 100644 --- a/crates/hir_ty/src/tests/macros.rs +++ b/crates/hir_ty/src/tests/macros.rs @@ -27,7 +27,7 @@ fn test() { } //^ (i32, {unknown}, i32, {unknown}) //- /foo.rs crate:foo -struct S; +pub struct S; #[cfg(not(test))] impl S { @@ -190,7 +190,6 @@ fn spam() { !0..6 '1isize': isize !0..6 '1isize': isize !0..6 '1isize': isize - !0..6 '1isize': isize 39..442 '{ ...!(); }': () 73..94 'spam!(...am!())': {unknown} 100..119 'for _ ...!() {}': () @@ -198,6 +197,7 @@ fn spam() { 117..119 '{}': () 124..134 '|| spam!()': || -> isize 140..156 'while ...!() {}': () + 146..153 'spam!()': bool 154..156 '{}': () 161..174 'break spam!()': ! 180..194 'return spam!()': ! @@ -271,7 +271,6 @@ fn spam() { !0..6 '1isize': isize !0..6 '1isize': isize !0..6 '1isize': isize - !0..6 '1isize': isize 53..456 '{ ...!(); }': () 87..108 'spam!(...am!())': {unknown} 114..133 'for _ ...!() {}': () @@ -279,6 +278,7 @@ fn spam() { 131..133 '{}': () 138..148 '|| spam!()': || -> isize 154..170 'while ...!() {}': () + 160..167 'spam!()': bool 168..170 '{}': () 175..188 'break spam!()': ! 194..208 'return spam!()': ! @@ -435,11 +435,11 @@ fn processes_impls_generated_by_macros() { macro_rules! m { ($ident:ident) => (impl Trait for $ident {}) } -trait Trait { fn foo(self) -> u128 {} } +trait Trait { fn foo(self) -> u128 { 0 } } struct S; m!(S); fn test() { S.foo(); } - //^ u128 + //^^^^^^^ u128 "#, ); } @@ -457,7 +457,7 @@ impl S { } fn test() { S.foo(); } - //^ u128 + //^^^^^^^ u128 "#, ); } @@ -479,7 +479,7 @@ impl S { } fn test() { S.foo(); } - //^ u128 + //^^^^^^^ u128 "#, ); } @@ -743,7 +743,7 @@ macro_rules! include {() => {}} fn main() { bar(); -} //^ u32 +} //^^^^^ u32 //- /foo.rs fn bar() -> u32 {0} @@ -751,6 +751,24 @@ fn bar() -> u32 {0} ); } +#[test] +fn infer_builtin_macros_include_expression() { + check_types( + r#" +//- /main.rs +#[rustc_builtin_macro] +macro_rules! include {() => {}} +fn main() { + let i = include!("bla.rs"); + i; + //^ i32 +} +//- /bla.rs +0 + "#, + ) +} + #[test] fn infer_builtin_macros_include_child_mod() { check_types( @@ -763,7 +781,7 @@ macro_rules! include {() => {}} fn main() { bar::bar(); -} //^ u32 +} //^^^^^^^^^^ u32 //- /f/foo.rs pub mod bar; @@ -835,7 +853,7 @@ macro_rules! include {() => {}} fn main() { RegisterBlock { }; - //^ RegisterBlock + //^^^^^^^^^^^^^^^^^ RegisterBlock } "#; let fixture = format!("{}\n//- /foo.rs\n{}", fixture, data); @@ -861,7 +879,7 @@ macro_rules! concat {() => {}} fn main() { bar(); -} //^ u32 +} //^^^^^ u32 //- /foo.rs fn bar() -> u32 {0} @@ -887,7 +905,7 @@ macro_rules! env {() => {}} fn main() { bar(); -} //^ {unknown} +} //^^^^^ {unknown} //- /foo.rs fn bar() -> u32 {0} @@ -905,7 +923,7 @@ macro_rules! include {() => {}} include!("main.rs"); fn main() { - 0 + 0; } //^ i32 "#, ); @@ -956,49 +974,12 @@ fn main() { fn infer_derive_clone_simple() { check_types( r#" -//- /main.rs crate:main deps:core +//- minicore: derive, clone #[derive(Clone)] struct S; fn test() { S.clone(); -} //^ S - -//- /lib.rs crate:core -#[prelude_import] -use clone::*; -mod clone { - trait Clone { - fn clone(&self) -> Self; - } - #[rustc_builtin_macro] - macro Clone {} -} -"#, - ); -} - -#[test] -fn infer_derive_clone_in_core() { - check_types( - r#" -//- /lib.rs crate:core -#[prelude_import] -use clone::*; -mod clone { - trait Clone { - fn clone(&self) -> Self; - } - #[rustc_builtin_macro] - macro Clone {} -} -#[derive(Clone)] -pub struct S; - -//- /main.rs crate:main deps:core -use core::S; -fn test() { - S.clone(); -} //^ S +} //^^^^^^^^^ S "#, ); } @@ -1007,27 +988,17 @@ fn test() { fn infer_derive_clone_with_params() { check_types( r#" -//- /main.rs crate:main deps:core +//- minicore: clone, derive #[derive(Clone)] struct S; #[derive(Clone)] struct Wrapper(T); struct NonClone; fn test() { - (Wrapper(S).clone(), Wrapper(NonClone).clone()); + let x = (Wrapper(S).clone(), Wrapper(NonClone).clone()); + x; //^ (Wrapper, {unknown}) } - -//- /lib.rs crate:core -#[prelude_import] -use clone::*; -mod clone { - trait Clone { - fn clone(&self) -> Self; - } - #[rustc_builtin_macro] - macro Clone {} -} "#, ); } @@ -1037,7 +1008,7 @@ fn infer_custom_derive_simple() { // FIXME: this test current now do nothing check_types( r#" -//- /main.rs crate:main +//- minicore: derive use foo::Foo; #[derive(Foo)] @@ -1045,7 +1016,7 @@ struct S{} fn test() { S{}; -} //^ S +} //^^^ S "#, ); } @@ -1074,3 +1045,202 @@ fn main() { "#]], ); } + +#[test] +fn macro_in_type_alias_position() { + check_infer( + r#" + macro_rules! U32 { + () => { u32 }; + } + + trait Foo { + type Ty; + } + + impl Foo for T { + type Ty = U32!(); + } + + type TayTo = U32!(); + + fn testy() { + let a: <() as Foo>::Ty; + let b: TayTo; + } + "#, + expect![[r#" + 147..196 '{ ...yTo; }': () + 157..158 'a': u32 + 185..186 'b': u32 + "#]], + ); +} + +#[test] +fn nested_macro_in_type_alias_position() { + check_infer( + r#" + macro_rules! U32Inner2 { + () => { u32 }; + } + + macro_rules! U32Inner1 { + () => { U32Inner2!() }; + } + + macro_rules! U32 { + () => { U32Inner1!() }; + } + + trait Foo { + type Ty; + } + + impl Foo for T { + type Ty = U32!(); + } + + type TayTo = U32!(); + + fn testy() { + let a: <() as Foo>::Ty; + let b: TayTo; + } + "#, + expect![[r#" + 259..308 '{ ...yTo; }': () + 269..270 'a': u32 + 297..298 'b': u32 + "#]], + ); +} + +#[test] +fn macros_in_type_alias_position_generics() { + check_infer( + r#" + struct Foo(A, B); + + macro_rules! U32 { + () => { u32 }; + } + + macro_rules! Bar { + () => { Foo }; + } + + trait Moo { + type Ty; + } + + impl Moo for T { + type Ty = Bar!(); + } + + type TayTo = Bar!(); + + fn main() { + let a: <() as Moo>::Ty; + let b: TayTo; + } + "#, + expect![[r#" + 228..277 '{ ...yTo; }': () + 238..239 'a': Foo + 266..267 'b': Foo + "#]], + ); +} + +#[test] +fn macros_in_type_position() { + check_infer( + r#" + struct Foo(A, B); + + macro_rules! U32 { + () => { u32 }; + } + + macro_rules! Bar { + () => { Foo }; + } + + fn main() { + let a: Bar!(); + } + "#, + expect![[r#" + 133..155 '{ ...!(); }': () + 143..144 'a': Foo + "#]], + ); +} + +#[test] +fn macros_in_type_generics() { + check_infer( + r#" + struct Foo(A, B); + + macro_rules! U32 { + () => { u32 }; + } + + macro_rules! Bar { + () => { Foo }; + } + + trait Moo { + type Ty; + } + + impl Moo for T { + type Ty = Foo; + } + + type TayTo = Foo; + + fn main() { + let a: <() as Moo>::Ty; + let b: TayTo; + } + "#, + expect![[r#" + 254..303 '{ ...yTo; }': () + 264..265 'a': Foo, Foo> + 292..293 'b': Foo, u32> + "#]], + ); +} + +#[test] +fn infinitely_recursive_macro_type() { + check_infer( + r#" + struct Bar(T, X); + + macro_rules! Foo { + () => { Foo!() } + } + + macro_rules! U32 { + () => { u32 } + } + + type A = Foo!(); + type B = Bar; + + fn main() { + let a: A; + let b: B; + } + "#, + expect![[r#" + 166..197 '{ ...: B; }': () + 176..177 'a': {unknown} + 190..191 'b': Bar<{unknown}, u32> + "#]], + ); +}