From 811d25b7237cfb74a312369a7820bf6fbb81483e Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 16 May 2020 18:32:15 +0200 Subject: Allow calling dyn trait super trait methods without the super trait in scope This also removes some vestiges of the old impl trait support which I think aren't currently in use. --- crates/ra_hir_ty/src/tests/method_resolution.rs | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'crates/ra_hir_ty/src/tests') diff --git a/crates/ra_hir_ty/src/tests/method_resolution.rs b/crates/ra_hir_ty/src/tests/method_resolution.rs index 67f964ab5..9c2c9e1d2 100644 --- a/crates/ra_hir_ty/src/tests/method_resolution.rs +++ b/crates/ra_hir_ty/src/tests/method_resolution.rs @@ -1096,3 +1096,34 @@ fn test() { (S {}).method()<|>; } ); assert_eq!(t, "()"); } + +#[test] +fn dyn_trait_super_trait_not_in_scope() { + assert_snapshot!( + infer(r#" +mod m { + pub trait SuperTrait { + fn foo(&self) -> u32 { 0 } + } +} +trait Trait: m::SuperTrait {} + +struct S; +impl m::SuperTrait for S {} +impl Trait for S {} + +fn test(d: &dyn Trait) { + d.foo(); +} +"#), + @r###" + 52..56 'self': &Self + 65..70 '{ 0 }': u32 + 67..68 '0': u32 + 177..178 'd': &dyn Trait + 192..208 '{ ...o(); }': () + 198..199 'd': &dyn Trait + 198..205 'd.foo()': u32 + "### + ); +} -- cgit v1.2.3 From 12a3bf3c31d4c9a6d9ee110db174604f688ca0f0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sun, 17 May 2020 23:37:30 +0800 Subject: Create LowerCtx on the fly --- crates/ra_hir_ty/src/tests/regression.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'crates/ra_hir_ty/src/tests') diff --git a/crates/ra_hir_ty/src/tests/regression.rs b/crates/ra_hir_ty/src/tests/regression.rs index 115ad8328..c2168222e 100644 --- a/crates/ra_hir_ty/src/tests/regression.rs +++ b/crates/ra_hir_ty/src/tests/regression.rs @@ -563,6 +563,37 @@ fn main() { ); } +#[test] +fn issue_4465_dollar_crate_at_type() { + assert_snapshot!( + infer(r#" +pub struct Foo {} +pub fn anything() -> T { + loop {} +} +macro_rules! foo { + () => {{ + let r: $crate::Foo = anything(); + r + }}; +} +fn main() { + let _a = foo!(); +} +"#), @r###" + 45..60 '{ loop {} }': T + 51..58 'loop {}': ! + 56..58 '{}': () + !0..31 '{letr:...g();r}': Foo + !4..5 'r': Foo + !18..26 'anything': fn anything() -> Foo + !18..28 'anything()': Foo + !29..30 'r': Foo + 164..188 '{ ...!(); }': () + 174..176 '_a': Foo +"###); +} + #[test] fn issue_4053_diesel_where_clauses() { assert_snapshot!( -- cgit v1.2.3 From 68db49c8534fa99768c7f600455ea76176f61994 Mon Sep 17 00:00:00 2001 From: Hrvoje Ban Date: Mon, 18 May 2020 08:07:31 +0200 Subject: Add more tests for Fn traits --- crates/ra_hir_ty/src/tests/traits.rs | 132 +++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) (limited to 'crates/ra_hir_ty/src/tests') diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index 9d32cbc7a..c49aacf98 100644 --- a/crates/ra_hir_ty/src/tests/traits.rs +++ b/crates/ra_hir_ty/src/tests/traits.rs @@ -1616,6 +1616,138 @@ fn test u128>(f: F) { ); } +#[test] +fn fn_ptr_and_item() { + assert_snapshot!( + infer(r#" +#[lang="fn_once"] +trait FnOnce { + type Output; + + fn call_once(self, args: Args) -> Self::Output; +} + +trait Foo { + fn foo(&self) -> T; +} + +struct Bar(T); + +impl R> Foo<(A1, R)> for Bar { + fn foo(&self) -> (A1, R) {} +} + +enum Opt { None, Some(T) } +impl Opt { + fn map U>(self, f: F) -> Opt {} +} + +fn test() { + let bar: Bar u32>; + bar.foo(); + + let opt: Opt; + let f: fn(u8) -> u32; + opt.map(f); +} +"#), + @r###" +75..79 'self': Self +81..85 'args': Args +140..144 'self': &Self +244..248 'self': &Bar +261..263 '{}': () +347..351 'self': Opt +353..354 'f': F +369..371 '{}': () +385..501 '{ ...(f); }': () +395..398 'bar': Bar u32> +424..427 'bar': Bar u32> +424..433 'bar.foo()': {unknown} +444..447 'opt': Opt +466..467 'f': fn(u8) -> u32 +488..491 'opt': Opt +488..498 'opt.map(f)': Opt u32, (u8,)>> +496..497 'f': fn(u8) -> u32 +"### + ); +} + +#[test] +fn fn_trait_deref_with_ty_default() { + assert_snapshot!( + infer(r#" +#[lang = "deref"] +trait Deref { + type Target; + + fn deref(&self) -> &Self::Target; +} + +#[lang="fn_once"] +trait FnOnce { + type Output; + + fn call_once(self, args: Args) -> Self::Output; +} + +struct Foo; + +impl Foo { + fn foo(&self) -> usize {} +} + +struct Lazy T>(F); + +impl Lazy { + pub fn new(f: F) -> Lazy {} +} + +impl T> Deref for Lazy { + type Target = T; +} + +fn test() { + let lazy1: Lazy = Lazy::new(|| Foo); + let r1 = lazy1.foo(); + + fn make_foo_fn() -> Foo {} + let make_foo_fn_ptr: fn() -> Foo = make_foo_fn; + let lazy2: Lazy = Lazy::new(make_foo_fn_ptr); + let r2 = lazy2.foo(); +} +"#), + @r###" +65..69 'self': &Self +166..170 'self': Self +172..176 'args': Args +240..244 'self': &Foo +255..257 '{}': () +335..336 'f': F +355..357 '{}': () +444..690 '{ ...o(); }': () +454..459 'lazy1': Lazy T> +476..485 'Lazy::new': fn new T>(fn() -> T) -> Lazy T> +476..493 'Lazy::...| Foo)': Lazy T> +486..492 '|| Foo': || -> T +489..492 'Foo': Foo +503..505 'r1': {unknown} +508..513 'lazy1': Lazy T> +508..519 'lazy1.foo()': {unknown} +561..576 'make_foo_fn_ptr': fn() -> Foo +592..603 'make_foo_fn': fn make_foo_fn() -> Foo +613..618 'lazy2': Lazy T> +635..644 'Lazy::new': fn new T>(fn() -> T) -> Lazy T> +635..661 'Lazy::...n_ptr)': Lazy T> +645..660 'make_foo_fn_ptr': fn() -> Foo +671..673 'r2': {unknown} +676..681 'lazy2': Lazy T> +676..687 'lazy2.foo()': {unknown} +550..552 '{}': () +"### + ); +} + #[test] fn closure_1() { assert_snapshot!( -- cgit v1.2.3 From da09f967469127576d9a87a7c143f754777a4f6b Mon Sep 17 00:00:00 2001 From: Roland Ruckerbauer Date: Tue, 19 May 2020 21:49:45 +0200 Subject: loop return value inference: add tests --- crates/ra_hir_ty/src/tests/simple.rs | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'crates/ra_hir_ty/src/tests') diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs index 72122c070..fd2208af2 100644 --- a/crates/ra_hir_ty/src/tests/simple.rs +++ b/crates/ra_hir_ty/src/tests/simple.rs @@ -1860,3 +1860,66 @@ fn test() { "### ); } + +#[test] +fn infer_loop_break_with_val() { + assert_snapshot!( + infer(r#" +enum Option { Some(T), None } +use Option::*; + +fn test() { + let x = loop { + if false { + break None; + } + + break Some(true); + }; +} +"#), + @r###" + 60..169 '{ ... }; }': () + 70..71 'x': Option + 74..166 'loop {... }': Option + 79..166 '{ ... }': () + 89..133 'if fal... }': () + 92..97 'false': bool + 98..133 '{ ... }': () + 112..122 'break None': ! + 118..122 'None': Option + 143..159 'break ...(true)': ! + 149..153 'Some': Some(bool) -> Option + 149..159 'Some(true)': Option + 154..158 'true': bool + "### + ); +} + +#[test] +fn infer_loop_break_without_val() { + assert_snapshot!( + infer(r#" +enum Option { Some(T), None } +use Option::*; + +fn test() { + let x = loop { + if false { + break; + } + }; +} +"#), + @r###" + 60..137 '{ ... }; }': () + 70..71 'x': () + 74..134 'loop {... }': () + 79..134 '{ ... }': () + 89..128 'if fal... }': () + 92..97 'false': bool + 98..128 '{ ... }': () + 112..117 'break': ! + "### + ); +} -- cgit v1.2.3 From ecac5d7de2192873c24b7b06d4964d188d8abe6a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 May 2020 12:59:20 +0200 Subject: Switch to new magic marks --- crates/ra_hir_ty/src/tests/coercion.rs | 6 +++--- crates/ra_hir_ty/src/tests/method_resolution.rs | 2 +- crates/ra_hir_ty/src/tests/patterns.rs | 4 ++-- crates/ra_hir_ty/src/tests/regression.rs | 15 +++++++-------- crates/ra_hir_ty/src/tests/traits.rs | 8 ++++---- 5 files changed, 17 insertions(+), 18 deletions(-) (limited to 'crates/ra_hir_ty/src/tests') diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs index 6dc4b2cd1..2cc4f4bf9 100644 --- a/crates/ra_hir_ty/src/tests/coercion.rs +++ b/crates/ra_hir_ty/src/tests/coercion.rs @@ -1,6 +1,6 @@ use super::infer_with_mismatches; use insta::assert_snapshot; -use test_utils::covers; +use test_utils::mark; // Infer with some common definitions and impls. fn infer(source: &str) -> String { @@ -339,7 +339,7 @@ fn test(i: i32) { #[test] fn coerce_merge_one_by_one1() { - covers!(coerce_merge_fail_fallback); + mark::check!(coerce_merge_fail_fallback); assert_snapshot!( infer(r#" @@ -547,7 +547,7 @@ fn test() { #[test] fn coerce_fn_items_in_match_arms() { - covers!(coerce_fn_reification); + mark::check!(coerce_fn_reification); assert_snapshot!( infer_with_mismatches(r#" fn foo1(x: u32) -> isize { 1 } diff --git a/crates/ra_hir_ty/src/tests/method_resolution.rs b/crates/ra_hir_ty/src/tests/method_resolution.rs index 9c2c9e1d2..558a70022 100644 --- a/crates/ra_hir_ty/src/tests/method_resolution.rs +++ b/crates/ra_hir_ty/src/tests/method_resolution.rs @@ -984,7 +984,7 @@ fn test() { S2.into()<|>; } #[test] fn method_resolution_overloaded_method() { - test_utils::covers!(impl_self_type_match_without_receiver); + test_utils::mark::check!(impl_self_type_match_without_receiver); let t = type_at( r#" //- main.rs diff --git a/crates/ra_hir_ty/src/tests/patterns.rs b/crates/ra_hir_ty/src/tests/patterns.rs index d83ff5e0e..0c5f972a2 100644 --- a/crates/ra_hir_ty/src/tests/patterns.rs +++ b/crates/ra_hir_ty/src/tests/patterns.rs @@ -1,5 +1,5 @@ use insta::assert_snapshot; -use test_utils::covers; +use test_utils::mark; use super::{infer, infer_with_mismatches}; @@ -197,7 +197,7 @@ fn test() { #[test] fn infer_pattern_match_ergonomics_ref() { - covers!(match_ergonomics_ref); + mark::check!(match_ergonomics_ref); assert_snapshot!( infer(r#" fn test() { diff --git a/crates/ra_hir_ty/src/tests/regression.rs b/crates/ra_hir_ty/src/tests/regression.rs index c2168222e..1f004bd63 100644 --- a/crates/ra_hir_ty/src/tests/regression.rs +++ b/crates/ra_hir_ty/src/tests/regression.rs @@ -1,9 +1,10 @@ use insta::assert_snapshot; -use test_utils::covers; +use ra_db::fixture::WithFixture; +use test_utils::mark; -use super::infer; use crate::test_db::TestDB; -use ra_db::fixture::WithFixture; + +use super::infer; #[test] fn bug_484() { @@ -89,8 +90,8 @@ fn quux() { #[test] fn recursive_vars() { - covers!(type_var_cycles_resolve_completely); - covers!(type_var_cycles_resolve_as_possible); + mark::check!(type_var_cycles_resolve_completely); + mark::check!(type_var_cycles_resolve_as_possible); assert_snapshot!( infer(r#" fn test() { @@ -112,8 +113,6 @@ fn test() { #[test] fn recursive_vars_2() { - covers!(type_var_cycles_resolve_completely); - covers!(type_var_cycles_resolve_as_possible); assert_snapshot!( infer(r#" fn test() { @@ -170,7 +169,7 @@ fn write() { #[test] fn infer_std_crash_2() { - covers!(type_var_resolves_to_int_var); + mark::check!(type_var_resolves_to_int_var); // caused "equating two type variables, ...", taken from std assert_snapshot!( infer(r#" diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index c49aacf98..34f4b9039 100644 --- a/crates/ra_hir_ty/src/tests/traits.rs +++ b/crates/ra_hir_ty/src/tests/traits.rs @@ -1,10 +1,11 @@ use insta::assert_snapshot; - use ra_db::fixture::WithFixture; +use test_utils::mark; -use super::{infer, infer_with_mismatches, type_at, type_at_pos}; use crate::test_db::TestDB; +use super::{infer, infer_with_mismatches, type_at, type_at_pos}; + #[test] fn infer_await() { let (db, pos) = TestDB::with_position( @@ -301,7 +302,7 @@ fn test() { #[test] fn trait_default_method_self_bound_implements_trait() { - test_utils::covers!(trait_self_implements_self); + mark::check!(trait_self_implements_self); assert_snapshot!( infer(r#" trait Trait { @@ -324,7 +325,6 @@ trait Trait { #[test] fn trait_default_method_self_bound_implements_super_trait() { - test_utils::covers!(trait_self_implements_self); assert_snapshot!( infer(r#" trait SuperTrait { -- cgit v1.2.3