diff options
Diffstat (limited to 'crates/hir_ty/src')
-rw-r--r-- | crates/hir_ty/src/diagnostics.rs | 55 | ||||
-rw-r--r-- | crates/hir_ty/src/infer.rs | 24 | ||||
-rw-r--r-- | crates/hir_ty/src/infer/expr.rs | 18 | ||||
-rw-r--r-- | crates/hir_ty/src/tests/simple.rs | 89 |
4 files changed, 181 insertions, 5 deletions
diff --git a/crates/hir_ty/src/diagnostics.rs b/crates/hir_ty/src/diagnostics.rs index dfe98571e..b58fe0ed7 100644 --- a/crates/hir_ty/src/diagnostics.rs +++ b/crates/hir_ty/src/diagnostics.rs | |||
@@ -36,6 +36,9 @@ pub fn validate_body(db: &dyn HirDatabase, owner: DefWithBodyId, sink: &mut Diag | |||
36 | validator.validate_body(db); | 36 | validator.validate_body(db); |
37 | } | 37 | } |
38 | 38 | ||
39 | // Diagnostic: no-such-field | ||
40 | // | ||
41 | // This diagnostic is triggered if created structure does not have field provided in record. | ||
39 | #[derive(Debug)] | 42 | #[derive(Debug)] |
40 | pub struct NoSuchField { | 43 | pub struct NoSuchField { |
41 | pub file: HirFileId, | 44 | pub file: HirFileId, |
@@ -60,6 +63,17 @@ impl Diagnostic for NoSuchField { | |||
60 | } | 63 | } |
61 | } | 64 | } |
62 | 65 | ||
66 | // Diagnostic: missing-structure-fields | ||
67 | // | ||
68 | // This diagnostic is triggered if record lacks some fields that exist in the corresponding structure. | ||
69 | // | ||
70 | // Example: | ||
71 | // | ||
72 | // ```rust | ||
73 | // struct A { a: u8, b: u8 } | ||
74 | // | ||
75 | // let a = A { a: 10 }; | ||
76 | // ``` | ||
63 | #[derive(Debug)] | 77 | #[derive(Debug)] |
64 | pub struct MissingFields { | 78 | pub struct MissingFields { |
65 | pub file: HirFileId, | 79 | pub file: HirFileId, |
@@ -96,6 +110,21 @@ impl Diagnostic for MissingFields { | |||
96 | } | 110 | } |
97 | } | 111 | } |
98 | 112 | ||
113 | // Diagnostic: missing-pat-fields | ||
114 | // | ||
115 | // This diagnostic is triggered if pattern lacks some fields that exist in the corresponding structure. | ||
116 | // | ||
117 | // Example: | ||
118 | // | ||
119 | // ```rust | ||
120 | // struct A { a: u8, b: u8 } | ||
121 | // | ||
122 | // let a = A { a: 10, b: 20 }; | ||
123 | // | ||
124 | // if let A { a } = a { | ||
125 | // // ... | ||
126 | // } | ||
127 | // ``` | ||
99 | #[derive(Debug)] | 128 | #[derive(Debug)] |
100 | pub struct MissingPatFields { | 129 | pub struct MissingPatFields { |
101 | pub file: HirFileId, | 130 | pub file: HirFileId, |
@@ -130,6 +159,9 @@ impl Diagnostic for MissingPatFields { | |||
130 | } | 159 | } |
131 | } | 160 | } |
132 | 161 | ||
162 | // Diagnostic: missing-match-arm | ||
163 | // | ||
164 | // This diagnostic is triggered if `match` block is missing one or more match arms. | ||
133 | #[derive(Debug)] | 165 | #[derive(Debug)] |
134 | pub struct MissingMatchArms { | 166 | pub struct MissingMatchArms { |
135 | pub file: HirFileId, | 167 | pub file: HirFileId, |
@@ -152,6 +184,17 @@ impl Diagnostic for MissingMatchArms { | |||
152 | } | 184 | } |
153 | } | 185 | } |
154 | 186 | ||
187 | // Diagnostic: missing-ok-in-tail-expr | ||
188 | // | ||
189 | // This diagnostic is triggered if block that should return `Result` returns a value not wrapped in `Ok`. | ||
190 | // | ||
191 | // Example: | ||
192 | // | ||
193 | // ```rust | ||
194 | // fn foo() -> Result<u8, ()> { | ||
195 | // 10 | ||
196 | // } | ||
197 | // ``` | ||
155 | #[derive(Debug)] | 198 | #[derive(Debug)] |
156 | pub struct MissingOkInTailExpr { | 199 | pub struct MissingOkInTailExpr { |
157 | pub file: HirFileId, | 200 | pub file: HirFileId, |
@@ -173,6 +216,9 @@ impl Diagnostic for MissingOkInTailExpr { | |||
173 | } | 216 | } |
174 | } | 217 | } |
175 | 218 | ||
219 | // Diagnostic: break-outside-of-loop | ||
220 | // | ||
221 | // This diagnostic is triggered if `break` keyword is used outside of a loop. | ||
176 | #[derive(Debug)] | 222 | #[derive(Debug)] |
177 | pub struct BreakOutsideOfLoop { | 223 | pub struct BreakOutsideOfLoop { |
178 | pub file: HirFileId, | 224 | pub file: HirFileId, |
@@ -194,6 +240,9 @@ impl Diagnostic for BreakOutsideOfLoop { | |||
194 | } | 240 | } |
195 | } | 241 | } |
196 | 242 | ||
243 | // Diagnostic: missing-unsafe | ||
244 | // | ||
245 | // This diagnostic is triggered if operation marked as `unsafe` is used outside of `unsafe` function or block. | ||
197 | #[derive(Debug)] | 246 | #[derive(Debug)] |
198 | pub struct MissingUnsafe { | 247 | pub struct MissingUnsafe { |
199 | pub file: HirFileId, | 248 | pub file: HirFileId, |
@@ -215,6 +264,9 @@ impl Diagnostic for MissingUnsafe { | |||
215 | } | 264 | } |
216 | } | 265 | } |
217 | 266 | ||
267 | // Diagnostic: mismatched-arg-count | ||
268 | // | ||
269 | // This diagnostic is triggered if function is invoked with an incorrect amount of arguments. | ||
218 | #[derive(Debug)] | 270 | #[derive(Debug)] |
219 | pub struct MismatchedArgCount { | 271 | pub struct MismatchedArgCount { |
220 | pub file: HirFileId, | 272 | pub file: HirFileId, |
@@ -264,6 +316,9 @@ impl fmt::Display for CaseType { | |||
264 | } | 316 | } |
265 | } | 317 | } |
266 | 318 | ||
319 | // Diagnostic: incorrect-ident-case | ||
320 | // | ||
321 | // This diagnostic is triggered if item name doesn't follow https://doc.rust-lang.org/1.0.0/style/style/naming/README.html[Rust naming convention]. | ||
267 | #[derive(Debug)] | 322 | #[derive(Debug)] |
268 | pub struct IncorrectCase { | 323 | pub struct IncorrectCase { |
269 | pub file: HirFileId, | 324 | pub file: HirFileId, |
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs index 9a7785c76..644ebd42d 100644 --- a/crates/hir_ty/src/infer.rs +++ b/crates/hir_ty/src/infer.rs | |||
@@ -22,7 +22,7 @@ use arena::map::ArenaMap; | |||
22 | use hir_def::{ | 22 | use hir_def::{ |
23 | body::Body, | 23 | body::Body, |
24 | data::{ConstData, FunctionData, StaticData}, | 24 | data::{ConstData, FunctionData, StaticData}, |
25 | expr::{BindingAnnotation, ExprId, PatId}, | 25 | expr::{ArithOp, BinaryOp, BindingAnnotation, ExprId, PatId}, |
26 | lang_item::LangItemTarget, | 26 | lang_item::LangItemTarget, |
27 | path::{path, Path}, | 27 | path::{path, Path}, |
28 | resolver::{HasResolver, Resolver, TypeNs}, | 28 | resolver::{HasResolver, Resolver, TypeNs}, |
@@ -586,6 +586,28 @@ impl<'a> InferenceContext<'a> { | |||
586 | self.db.trait_data(trait_).associated_type_by_name(&name![Output]) | 586 | self.db.trait_data(trait_).associated_type_by_name(&name![Output]) |
587 | } | 587 | } |
588 | 588 | ||
589 | fn resolve_binary_op_output(&self, bop: &BinaryOp) -> Option<TypeAliasId> { | ||
590 | let lang_item = match bop { | ||
591 | BinaryOp::ArithOp(aop) => match aop { | ||
592 | ArithOp::Add => "add", | ||
593 | ArithOp::Sub => "sub", | ||
594 | ArithOp::Mul => "mul", | ||
595 | ArithOp::Div => "div", | ||
596 | ArithOp::Shl => "shl", | ||
597 | ArithOp::Shr => "shr", | ||
598 | ArithOp::Rem => "rem", | ||
599 | ArithOp::BitXor => "bitxor", | ||
600 | ArithOp::BitOr => "bitor", | ||
601 | ArithOp::BitAnd => "bitand", | ||
602 | }, | ||
603 | _ => return None, | ||
604 | }; | ||
605 | |||
606 | let trait_ = self.resolve_lang_item(lang_item)?.as_trait(); | ||
607 | |||
608 | self.db.trait_data(trait_?).associated_type_by_name(&name![Output]) | ||
609 | } | ||
610 | |||
589 | fn resolve_boxed_box(&self) -> Option<AdtId> { | 611 | fn resolve_boxed_box(&self) -> Option<AdtId> { |
590 | let struct_ = self.resolve_lang_item("owned_box")?.as_struct()?; | 612 | let struct_ = self.resolve_lang_item("owned_box")?.as_struct()?; |
591 | Some(struct_.into()) | 613 | Some(struct_.into()) |
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 0a141b9cb..8ac4cf89a 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs | |||
@@ -12,6 +12,7 @@ use hir_def::{ | |||
12 | }; | 12 | }; |
13 | use hir_expand::name::{name, Name}; | 13 | use hir_expand::name::{name, Name}; |
14 | use syntax::ast::RangeOp; | 14 | use syntax::ast::RangeOp; |
15 | use test_utils::mark; | ||
15 | 16 | ||
16 | use crate::{ | 17 | use crate::{ |
17 | autoderef, method_resolution, op, | 18 | autoderef, method_resolution, op, |
@@ -531,13 +532,22 @@ impl<'a> InferenceContext<'a> { | |||
531 | _ => Expectation::none(), | 532 | _ => Expectation::none(), |
532 | }; | 533 | }; |
533 | let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); | 534 | let lhs_ty = self.infer_expr(*lhs, &lhs_expectation); |
534 | // FIXME: find implementation of trait corresponding to operation | ||
535 | // symbol and resolve associated `Output` type | ||
536 | let rhs_expectation = op::binary_op_rhs_expectation(*op, lhs_ty.clone()); | 535 | let rhs_expectation = op::binary_op_rhs_expectation(*op, lhs_ty.clone()); |
537 | let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation)); | 536 | let rhs_ty = self.infer_expr(*rhs, &Expectation::has_type(rhs_expectation)); |
538 | 537 | ||
539 | // FIXME: similar as above, return ty is often associated trait type | 538 | let ret = op::binary_op_return_ty(*op, lhs_ty.clone(), rhs_ty.clone()); |
540 | op::binary_op_return_ty(*op, lhs_ty, rhs_ty) | 539 | |
540 | if ret == Ty::Unknown { | ||
541 | mark::hit!(infer_expr_inner_binary_operator_overload); | ||
542 | |||
543 | self.resolve_associated_type_with_params( | ||
544 | lhs_ty, | ||
545 | self.resolve_binary_op_output(op), | ||
546 | &[rhs_ty], | ||
547 | ) | ||
548 | } else { | ||
549 | ret | ||
550 | } | ||
541 | } | 551 | } |
542 | _ => Ty::Unknown, | 552 | _ => Ty::Unknown, |
543 | }, | 553 | }, |
diff --git a/crates/hir_ty/src/tests/simple.rs b/crates/hir_ty/src/tests/simple.rs index 5b07948f3..4f72582b6 100644 --- a/crates/hir_ty/src/tests/simple.rs +++ b/crates/hir_ty/src/tests/simple.rs | |||
@@ -1,4 +1,5 @@ | |||
1 | use expect_test::expect; | 1 | use expect_test::expect; |
2 | use test_utils::mark; | ||
2 | 3 | ||
3 | use super::{check_infer, check_types}; | 4 | use super::{check_infer, check_types}; |
4 | 5 | ||
@@ -2225,3 +2226,91 @@ fn generic_default_depending_on_other_type_arg_forward() { | |||
2225 | "#]], | 2226 | "#]], |
2226 | ); | 2227 | ); |
2227 | } | 2228 | } |
2229 | |||
2230 | #[test] | ||
2231 | fn infer_operator_overload() { | ||
2232 | mark::check!(infer_expr_inner_binary_operator_overload); | ||
2233 | |||
2234 | check_infer( | ||
2235 | r#" | ||
2236 | struct V2([f32; 2]); | ||
2237 | |||
2238 | #[lang = "add"] | ||
2239 | pub trait Add<Rhs = Self> { | ||
2240 | /// The resulting type after applying the `+` operator. | ||
2241 | type Output; | ||
2242 | |||
2243 | /// Performs the `+` operation. | ||
2244 | #[must_use] | ||
2245 | fn add(self, rhs: Rhs) -> Self::Output; | ||
2246 | } | ||
2247 | |||
2248 | impl Add<V2> for V2 { | ||
2249 | type Output = V2; | ||
2250 | |||
2251 | fn add(self, rhs: V2) -> V2 { | ||
2252 | let x = self.0[0] + rhs.0[0]; | ||
2253 | let y = self.0[1] + rhs.0[1]; | ||
2254 | V2([x, y]) | ||
2255 | } | ||
2256 | } | ||
2257 | |||
2258 | fn test() { | ||
2259 | let va = V2([0.0, 1.0]); | ||
2260 | let vb = V2([0.0, 1.0]); | ||
2261 | |||
2262 | let r = va + vb; | ||
2263 | } | ||
2264 | |||
2265 | "#, | ||
2266 | expect![[r#" | ||
2267 | 207..211 'self': Self | ||
2268 | 213..216 'rhs': Rhs | ||
2269 | 299..303 'self': V2 | ||
2270 | 305..308 'rhs': V2 | ||
2271 | 320..422 '{ ... }': V2 | ||
2272 | 334..335 'x': f32 | ||
2273 | 338..342 'self': V2 | ||
2274 | 338..344 'self.0': [f32; _] | ||
2275 | 338..347 'self.0[0]': {unknown} | ||
2276 | 338..358 'self.0...s.0[0]': f32 | ||
2277 | 345..346 '0': i32 | ||
2278 | 350..353 'rhs': V2 | ||
2279 | 350..355 'rhs.0': [f32; _] | ||
2280 | 350..358 'rhs.0[0]': {unknown} | ||
2281 | 356..357 '0': i32 | ||
2282 | 372..373 'y': f32 | ||
2283 | 376..380 'self': V2 | ||
2284 | 376..382 'self.0': [f32; _] | ||
2285 | 376..385 'self.0[1]': {unknown} | ||
2286 | 376..396 'self.0...s.0[1]': f32 | ||
2287 | 383..384 '1': i32 | ||
2288 | 388..391 'rhs': V2 | ||
2289 | 388..393 'rhs.0': [f32; _] | ||
2290 | 388..396 'rhs.0[1]': {unknown} | ||
2291 | 394..395 '1': i32 | ||
2292 | 406..408 'V2': V2([f32; _]) -> V2 | ||
2293 | 406..416 'V2([x, y])': V2 | ||
2294 | 409..415 '[x, y]': [f32; _] | ||
2295 | 410..411 'x': f32 | ||
2296 | 413..414 'y': f32 | ||
2297 | 436..519 '{ ... vb; }': () | ||
2298 | 446..448 'va': V2 | ||
2299 | 451..453 'V2': V2([f32; _]) -> V2 | ||
2300 | 451..465 'V2([0.0, 1.0])': V2 | ||
2301 | 454..464 '[0.0, 1.0]': [f32; _] | ||
2302 | 455..458 '0.0': f32 | ||
2303 | 460..463 '1.0': f32 | ||
2304 | 475..477 'vb': V2 | ||
2305 | 480..482 'V2': V2([f32; _]) -> V2 | ||
2306 | 480..494 'V2([0.0, 1.0])': V2 | ||
2307 | 483..493 '[0.0, 1.0]': [f32; _] | ||
2308 | 484..487 '0.0': f32 | ||
2309 | 489..492 '1.0': f32 | ||
2310 | 505..506 'r': V2 | ||
2311 | 509..511 'va': V2 | ||
2312 | 509..516 'va + vb': V2 | ||
2313 | 514..516 'vb': V2 | ||
2314 | "#]], | ||
2315 | ); | ||
2316 | } | ||