diff options
author | uHOOCCOOHu <[email protected]> | 2019-09-11 16:53:41 +0100 |
---|---|---|
committer | uHOOCCOOHu <[email protected]> | 2019-09-11 19:35:09 +0100 |
commit | 8c078a01641518a6b093922d4b1d27d1a98bad08 (patch) | |
tree | 25060debb1bd0b5905d77a6b4d5f80de07588f94 /crates/ra_hir/src/ty | |
parent | 6ce6744e18f25ebcde387178125d820686692df5 (diff) |
Infer box expression
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 26 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 31 |
2 files changed, 57 insertions, 0 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 9244ff3cb..1057bbbec 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -1259,6 +1259,14 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1259 | let inner_ty = self.infer_expr(*expr, &expectation); | 1259 | let inner_ty = self.infer_expr(*expr, &expectation); |
1260 | Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty) | 1260 | Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty) |
1261 | } | 1261 | } |
1262 | Expr::Box { expr } => { | ||
1263 | let inner_ty = self.infer_expr(*expr, &Expectation::none()); | ||
1264 | if let Some(box_) = self.resolve_boxed_box() { | ||
1265 | Ty::apply_one(TypeCtor::Adt(box_), inner_ty) | ||
1266 | } else { | ||
1267 | Ty::Unknown | ||
1268 | } | ||
1269 | } | ||
1262 | Expr::UnaryOp { expr, op } => { | 1270 | Expr::UnaryOp { expr, op } => { |
1263 | let inner_ty = self.infer_expr(*expr, &Expectation::none()); | 1271 | let inner_ty = self.infer_expr(*expr, &Expectation::none()); |
1264 | match op { | 1272 | match op { |
@@ -1499,6 +1507,24 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1499 | _ => None, | 1507 | _ => None, |
1500 | } | 1508 | } |
1501 | } | 1509 | } |
1510 | |||
1511 | fn resolve_boxed_box(&self) -> Option<AdtDef> { | ||
1512 | let boxed_box_path = Path { | ||
1513 | kind: PathKind::Abs, | ||
1514 | segments: vec![ | ||
1515 | PathSegment { name: name::STD, args_and_bindings: None }, | ||
1516 | PathSegment { name: name::BOXED_MOD, args_and_bindings: None }, | ||
1517 | PathSegment { name: name::BOX_TYPE, args_and_bindings: None }, | ||
1518 | ], | ||
1519 | }; | ||
1520 | |||
1521 | match self.resolver.resolve_path_segments(self.db, &boxed_box_path).into_fully_resolved() { | ||
1522 | PerNs { types: Some(Def(ModuleDef::Struct(struct_))), .. } => { | ||
1523 | Some(AdtDef::Struct(struct_)) | ||
1524 | } | ||
1525 | _ => None, | ||
1526 | } | ||
1527 | } | ||
1502 | } | 1528 | } |
1503 | 1529 | ||
1504 | /// The ID of a type variable. | 1530 | /// The ID of a type variable. |
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 869ae13f1..9a5f6949d 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -56,6 +56,37 @@ mod future { | |||
56 | } | 56 | } |
57 | 57 | ||
58 | #[test] | 58 | #[test] |
59 | fn infer_box() { | ||
60 | let (mut db, pos) = MockDatabase::with_position( | ||
61 | r#" | ||
62 | //- /main.rs | ||
63 | |||
64 | fn test() { | ||
65 | let x = box 1; | ||
66 | let t = (x, box x, box &1, box [1]); | ||
67 | t<|>; | ||
68 | } | ||
69 | |||
70 | //- /std.rs | ||
71 | #[prelude_import] use prelude::*; | ||
72 | mod prelude {} | ||
73 | |||
74 | mod boxed { | ||
75 | pub struct Box<T: ?Sized> { | ||
76 | inner: *mut T, | ||
77 | } | ||
78 | } | ||
79 | |||
80 | "#, | ||
81 | ); | ||
82 | db.set_crate_graph_from_fixture(crate_graph! { | ||
83 | "main": ("/main.rs", ["std"]), | ||
84 | "std": ("/std.rs", []), | ||
85 | }); | ||
86 | assert_eq!("(Box<i32>, Box<Box<i32>>, Box<&i32>, Box<[i32;_]>)", type_at_pos(&db, pos)); | ||
87 | } | ||
88 | |||
89 | #[test] | ||
59 | fn infer_try() { | 90 | fn infer_try() { |
60 | let (mut db, pos) = MockDatabase::with_position( | 91 | let (mut db, pos) = MockDatabase::with_position( |
61 | r#" | 92 | r#" |