diff options
author | Florian Diebold <[email protected]> | 2019-08-13 22:09:08 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-08-22 18:33:00 +0100 |
commit | 16a7d8cc850002b427fdc8d21ccde81caaed7902 (patch) | |
tree | 7f3c43cf9e83d479edc7f9b4849dae5fbd0f356d /crates/ra_hir/src/ty/tests.rs | |
parent | 08e5d394dfbca28b15ed5dc772d55d48f87c3f54 (diff) |
Add `impl Trait` and `dyn Trait` types
- refactor bounds handling in the AST a bit
- add HIR for bounds
- add `Ty::Dyn` and `Ty::Opaque` variants and lower `dyn Trait` / `impl Trait`
syntax to them
Diffstat (limited to 'crates/ra_hir/src/ty/tests.rs')
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 57fd5492d..93c62b0d4 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -3273,6 +3273,126 @@ fn test<T: ApplyL>(t: T) { | |||
3273 | assert_eq!(t, "{unknown}"); | 3273 | assert_eq!(t, "{unknown}"); |
3274 | } | 3274 | } |
3275 | 3275 | ||
3276 | #[test] | ||
3277 | fn impl_trait() { | ||
3278 | assert_snapshot_matches!( | ||
3279 | infer(r#" | ||
3280 | trait Trait<T> { | ||
3281 | fn foo(&self) -> T; | ||
3282 | } | ||
3283 | fn bar() -> impl Trait<u64> {} | ||
3284 | |||
3285 | fn test(x: impl Trait<u64>, y: &impl Trait<u64>) { | ||
3286 | x; | ||
3287 | y; | ||
3288 | let z = bar(); | ||
3289 | x.foo(); | ||
3290 | y.foo(); | ||
3291 | z.foo(); | ||
3292 | } | ||
3293 | "#), | ||
3294 | @r###" | ||
3295 | ⋮ | ||
3296 | ⋮[30; 34) 'self': &Self | ||
3297 | ⋮[72; 74) '{}': () | ||
3298 | ⋮[84; 85) 'x': impl Trait<u64> | ||
3299 | ⋮[104; 105) 'y': &impl Trait<u64> | ||
3300 | ⋮[125; 200) '{ ...o(); }': () | ||
3301 | ⋮[131; 132) 'x': impl Trait<u64> | ||
3302 | ⋮[138; 139) 'y': &impl Trait<u64> | ||
3303 | ⋮[149; 150) 'z': impl Trait<u64> | ||
3304 | ⋮[153; 156) 'bar': fn bar() -> impl Trait<u64> | ||
3305 | ⋮[153; 158) 'bar()': impl Trait<u64> | ||
3306 | ⋮[164; 165) 'x': impl Trait<u64> | ||
3307 | ⋮[164; 171) 'x.foo()': {unknown} | ||
3308 | ⋮[177; 178) 'y': &impl Trait<u64> | ||
3309 | ⋮[177; 184) 'y.foo()': {unknown} | ||
3310 | ⋮[190; 191) 'z': impl Trait<u64> | ||
3311 | ⋮[190; 197) 'z.foo()': {unknown} | ||
3312 | "### | ||
3313 | ); | ||
3314 | } | ||
3315 | |||
3316 | #[test] | ||
3317 | fn dyn_trait() { | ||
3318 | assert_snapshot_matches!( | ||
3319 | infer(r#" | ||
3320 | trait Trait<T> { | ||
3321 | fn foo(&self) -> T; | ||
3322 | } | ||
3323 | fn bar() -> dyn Trait<u64> {} | ||
3324 | |||
3325 | fn test(x: dyn Trait<u64>, y: &dyn Trait<u64>) { | ||
3326 | x; | ||
3327 | y; | ||
3328 | let z = bar(); | ||
3329 | x.foo(); | ||
3330 | y.foo(); | ||
3331 | z.foo(); | ||
3332 | } | ||
3333 | "#), | ||
3334 | @r###" | ||
3335 | ⋮ | ||
3336 | ⋮[30; 34) 'self': &Self | ||
3337 | ⋮[71; 73) '{}': () | ||
3338 | ⋮[83; 84) 'x': dyn Trait<u64> | ||
3339 | ⋮[102; 103) 'y': &dyn Trait<u64> | ||
3340 | ⋮[122; 197) '{ ...o(); }': () | ||
3341 | ⋮[128; 129) 'x': dyn Trait<u64> | ||
3342 | ⋮[135; 136) 'y': &dyn Trait<u64> | ||
3343 | ⋮[146; 147) 'z': dyn Trait<u64> | ||
3344 | ⋮[150; 153) 'bar': fn bar() -> dyn Trait<u64> | ||
3345 | ⋮[150; 155) 'bar()': dyn Trait<u64> | ||
3346 | ⋮[161; 162) 'x': dyn Trait<u64> | ||
3347 | ⋮[161; 168) 'x.foo()': {unknown} | ||
3348 | ⋮[174; 175) 'y': &dyn Trait<u64> | ||
3349 | ⋮[174; 181) 'y.foo()': {unknown} | ||
3350 | ⋮[187; 188) 'z': dyn Trait<u64> | ||
3351 | ⋮[187; 194) 'z.foo()': {unknown} | ||
3352 | "### | ||
3353 | ); | ||
3354 | } | ||
3355 | |||
3356 | #[test] | ||
3357 | fn dyn_trait_bare() { | ||
3358 | assert_snapshot_matches!( | ||
3359 | infer(r#" | ||
3360 | trait Trait { | ||
3361 | fn foo(&self) -> u64; | ||
3362 | } | ||
3363 | fn bar() -> Trait {} | ||
3364 | |||
3365 | fn test(x: Trait, y: &Trait) -> u64 { | ||
3366 | x; | ||
3367 | y; | ||
3368 | let z = bar(); | ||
3369 | x.foo(); | ||
3370 | y.foo(); | ||
3371 | z.foo(); | ||
3372 | } | ||
3373 | "#), | ||
3374 | @r###" | ||
3375 | ⋮ | ||
3376 | ⋮[27; 31) 'self': &Self | ||
3377 | ⋮[61; 63) '{}': () | ||
3378 | ⋮[73; 74) 'x': {unknown} | ||
3379 | ⋮[83; 84) 'y': &{unknown} | ||
3380 | ⋮[101; 176) '{ ...o(); }': () | ||
3381 | ⋮[107; 108) 'x': {unknown} | ||
3382 | ⋮[114; 115) 'y': &{unknown} | ||
3383 | ⋮[125; 126) 'z': {unknown} | ||
3384 | ⋮[129; 132) 'bar': fn bar() -> {unknown} | ||
3385 | ⋮[129; 134) 'bar()': {unknown} | ||
3386 | ⋮[140; 141) 'x': {unknown} | ||
3387 | ⋮[140; 147) 'x.foo()': {unknown} | ||
3388 | ⋮[153; 154) 'y': &{unknown} | ||
3389 | ⋮[153; 160) 'y.foo()': {unknown} | ||
3390 | ⋮[166; 167) 'z': {unknown} | ||
3391 | ⋮[166; 173) 'z.foo()': {unknown} | ||
3392 | "### | ||
3393 | ); | ||
3394 | } | ||
3395 | |||
3276 | fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { | 3396 | fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { |
3277 | let file = db.parse(pos.file_id).ok().unwrap(); | 3397 | let file = db.parse(pos.file_id).ok().unwrap(); |
3278 | let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap(); | 3398 | let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap(); |