]>
Commit | Line | Data |
---|---|---|
29967ef6 XL |
1 | use crate::astconv::AstConv; |
2 | use crate::check::coercion::CoerceMany; | |
a2a8927a | 3 | use crate::check::gather_locals::Declaration; |
29967ef6 XL |
4 | use crate::check::method::MethodCallee; |
5 | use crate::check::Expectation::*; | |
6 | use crate::check::TupleArgumentsFlag::*; | |
7 | use crate::check::{ | |
8 | potentially_plural_count, struct_span_err, BreakableCtxt, Diverges, Expectation, FnCtxt, | |
9 | LocalTy, Needs, TupleArgumentsFlag, | |
10 | }; | |
11 | ||
12 | use rustc_ast as ast; | |
c295e0f8 | 13 | use rustc_data_structures::sync::Lrc; |
29967ef6 XL |
14 | use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId}; |
15 | use rustc_hir as hir; | |
136023e0 | 16 | use rustc_hir::def::{CtorOf, DefKind, Res}; |
29967ef6 XL |
17 | use rustc_hir::def_id::DefId; |
18 | use rustc_hir::{ExprKind, Node, QPath}; | |
19 | use rustc_middle::ty::adjustment::AllowTwoPhase; | |
20 | use rustc_middle::ty::fold::TypeFoldable; | |
21 | use rustc_middle::ty::{self, Ty}; | |
22 | use rustc_session::Session; | |
17df50a5 | 23 | use rustc_span::symbol::Ident; |
29967ef6 XL |
24 | use rustc_span::{self, MultiSpan, Span}; |
25 | use rustc_trait_selection::traits::{self, ObligationCauseCode, StatementAsExpression}; | |
26 | ||
5869c6ff | 27 | use crate::structured_errors::StructuredDiagnostic; |
cdc7bbd5 | 28 | use std::iter; |
29967ef6 XL |
29 | use std::slice; |
30 | ||
5099ac24 FG |
31 | struct FnArgsAsTuple<'hir> { |
32 | first: &'hir hir::Expr<'hir>, | |
33 | last: &'hir hir::Expr<'hir>, | |
34 | } | |
35 | ||
29967ef6 XL |
36 | impl<'a, 'tcx> FnCtxt<'a, 'tcx> { |
37 | pub(in super::super) fn check_casts(&self) { | |
38 | let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut(); | |
94222f64 | 39 | debug!("FnCtxt::check_casts: {} deferred checks", deferred_cast_checks.len()); |
29967ef6 XL |
40 | for cast in deferred_cast_checks.drain(..) { |
41 | cast.check(self); | |
42 | } | |
43 | } | |
44 | ||
45 | pub(in super::super) fn check_method_argument_types( | |
46 | &self, | |
47 | sp: Span, | |
48 | expr: &'tcx hir::Expr<'tcx>, | |
49 | method: Result<MethodCallee<'tcx>, ()>, | |
50 | args_no_rcvr: &'tcx [hir::Expr<'tcx>], | |
51 | tuple_arguments: TupleArgumentsFlag, | |
52 | expected: Expectation<'tcx>, | |
53 | ) -> Ty<'tcx> { | |
54 | let has_error = match method { | |
55 | Ok(method) => method.substs.references_error() || method.sig.references_error(), | |
56 | Err(_) => true, | |
57 | }; | |
58 | if has_error { | |
59 | let err_inputs = self.err_args(args_no_rcvr.len()); | |
60 | ||
61 | let err_inputs = match tuple_arguments { | |
62 | DontTupleArguments => err_inputs, | |
a2a8927a | 63 | TupleArguments => vec![self.tcx.intern_tup(&err_inputs)], |
29967ef6 XL |
64 | }; |
65 | ||
66 | self.check_argument_types( | |
67 | sp, | |
68 | expr, | |
a2a8927a XL |
69 | &err_inputs, |
70 | vec![], | |
29967ef6 XL |
71 | args_no_rcvr, |
72 | false, | |
73 | tuple_arguments, | |
74 | None, | |
75 | ); | |
76 | return self.tcx.ty_error(); | |
77 | } | |
78 | ||
79 | let method = method.unwrap(); | |
80 | // HACK(eddyb) ignore self in the definition (see above). | |
a2a8927a | 81 | let expected_input_tys = self.expected_inputs_for_expected_output( |
29967ef6 XL |
82 | sp, |
83 | expected, | |
84 | method.sig.output(), | |
85 | &method.sig.inputs()[1..], | |
86 | ); | |
87 | self.check_argument_types( | |
88 | sp, | |
89 | expr, | |
90 | &method.sig.inputs()[1..], | |
a2a8927a | 91 | expected_input_tys, |
29967ef6 XL |
92 | args_no_rcvr, |
93 | method.sig.c_variadic, | |
94 | tuple_arguments, | |
95 | Some(method.def_id), | |
96 | ); | |
97 | method.sig.output() | |
98 | } | |
99 | ||
100 | /// Generic function that factors out common logic from function calls, | |
101 | /// method calls and overloaded operators. | |
102 | pub(in super::super) fn check_argument_types( | |
103 | &self, | |
a2a8927a XL |
104 | // Span enclosing the call site |
105 | call_span: Span, | |
106 | // Expression of the call site | |
107 | call_expr: &'tcx hir::Expr<'tcx>, | |
108 | // Types (as defined in the *signature* of the target function) | |
109 | formal_input_tys: &[Ty<'tcx>], | |
110 | // More specific expected types, after unifying with caller output types | |
111 | expected_input_tys: Vec<Ty<'tcx>>, | |
112 | // The expressions for each provided argument | |
113 | provided_args: &'tcx [hir::Expr<'tcx>], | |
114 | // Whether the function is variadic, for example when imported from C | |
29967ef6 | 115 | c_variadic: bool, |
a2a8927a | 116 | // Whether the arguments have been bundled in a tuple (ex: closures) |
29967ef6 | 117 | tuple_arguments: TupleArgumentsFlag, |
a2a8927a XL |
118 | // The DefId for the function being called, for better error messages |
119 | fn_def_id: Option<DefId>, | |
29967ef6 XL |
120 | ) { |
121 | let tcx = self.tcx; | |
122 | // Grab the argument types, supplying fresh type variables | |
123 | // if the wrong number of arguments were supplied | |
a2a8927a XL |
124 | let supplied_arg_count = |
125 | if tuple_arguments == DontTupleArguments { provided_args.len() } else { 1 }; | |
29967ef6 XL |
126 | |
127 | // All the input types from the fn signature must outlive the call | |
128 | // so as to validate implied bounds. | |
a2a8927a | 129 | for (&fn_input_ty, arg_expr) in iter::zip(formal_input_tys, provided_args) { |
29967ef6 XL |
130 | self.register_wf_obligation(fn_input_ty.into(), arg_expr.span, traits::MiscObligation); |
131 | } | |
132 | ||
a2a8927a | 133 | let expected_arg_count = formal_input_tys.len(); |
29967ef6 | 134 | |
5099ac24 FG |
135 | // expected_count, arg_count, error_code, sugg_unit, sugg_tuple_wrap_args |
136 | let mut arg_count_error: Option<(usize, usize, &str, bool, Option<FnArgsAsTuple<'_>>)> = | |
137 | None; | |
29967ef6 | 138 | |
5099ac24 | 139 | // If the arguments should be wrapped in a tuple (ex: closures), unwrap them here |
a2a8927a XL |
140 | let (formal_input_tys, expected_input_tys) = if tuple_arguments == TupleArguments { |
141 | let tuple_type = self.structurally_resolved_type(call_span, formal_input_tys[0]); | |
29967ef6 | 142 | match tuple_type.kind() { |
5099ac24 | 143 | // We expected a tuple and got a tuple |
29967ef6 | 144 | ty::Tuple(arg_types) => { |
5099ac24 FG |
145 | // Argument length differs |
146 | if arg_types.len() != provided_args.len() { | |
147 | arg_count_error = | |
148 | Some((arg_types.len(), provided_args.len(), "E0057", false, None)); | |
149 | } | |
a2a8927a | 150 | let expected_input_tys = match expected_input_tys.get(0) { |
29967ef6 XL |
151 | Some(&ty) => match ty.kind() { |
152 | ty::Tuple(ref tys) => tys.iter().map(|k| k.expect_ty()).collect(), | |
153 | _ => vec![], | |
154 | }, | |
155 | None => vec![], | |
156 | }; | |
a2a8927a | 157 | (arg_types.iter().map(|k| k.expect_ty()).collect(), expected_input_tys) |
29967ef6 XL |
158 | } |
159 | _ => { | |
5099ac24 FG |
160 | // Otherwise, there's a mismatch, so clear out what we're expecting, and set |
161 | // our input typs to err_args so we don't blow up the error messages | |
29967ef6 XL |
162 | struct_span_err!( |
163 | tcx.sess, | |
a2a8927a | 164 | call_span, |
29967ef6 XL |
165 | E0059, |
166 | "cannot use call notation; the first type parameter \ | |
167 | for the function trait is neither a tuple nor unit" | |
168 | ) | |
169 | .emit(); | |
a2a8927a | 170 | (self.err_args(provided_args.len()), vec![]) |
29967ef6 XL |
171 | } |
172 | } | |
173 | } else if expected_arg_count == supplied_arg_count { | |
a2a8927a | 174 | (formal_input_tys.to_vec(), expected_input_tys) |
29967ef6 XL |
175 | } else if c_variadic { |
176 | if supplied_arg_count >= expected_arg_count { | |
a2a8927a | 177 | (formal_input_tys.to_vec(), expected_input_tys) |
29967ef6 | 178 | } else { |
5099ac24 FG |
179 | arg_count_error = |
180 | Some((expected_arg_count, supplied_arg_count, "E0060", false, None)); | |
a2a8927a | 181 | (self.err_args(supplied_arg_count), vec![]) |
29967ef6 XL |
182 | } |
183 | } else { | |
184 | // is the missing argument of type `()`? | |
a2a8927a XL |
185 | let sugg_unit = if expected_input_tys.len() == 1 && supplied_arg_count == 0 { |
186 | self.resolve_vars_if_possible(expected_input_tys[0]).is_unit() | |
187 | } else if formal_input_tys.len() == 1 && supplied_arg_count == 0 { | |
188 | self.resolve_vars_if_possible(formal_input_tys[0]).is_unit() | |
29967ef6 XL |
189 | } else { |
190 | false | |
191 | }; | |
29967ef6 | 192 | |
5099ac24 FG |
193 | // are we passing elements of a tuple without the tuple parentheses? |
194 | let expected_input_tys = if expected_input_tys.is_empty() { | |
195 | // In most cases we can use expected_input_tys, but some callers won't have the type | |
196 | // information, in which case we fall back to the types from the input expressions. | |
197 | formal_input_tys | |
198 | } else { | |
199 | &*expected_input_tys | |
200 | }; | |
201 | ||
202 | let sugg_tuple_wrap_args = self.suggested_tuple_wrap(expected_input_tys, provided_args); | |
203 | ||
204 | arg_count_error = Some(( | |
205 | expected_arg_count, | |
206 | supplied_arg_count, | |
207 | "E0061", | |
208 | sugg_unit, | |
209 | sugg_tuple_wrap_args, | |
210 | )); | |
a2a8927a | 211 | (self.err_args(supplied_arg_count), vec![]) |
29967ef6 XL |
212 | }; |
213 | ||
214 | debug!( | |
a2a8927a XL |
215 | "check_argument_types: formal_input_tys={:?}", |
216 | formal_input_tys.iter().map(|t| self.ty_to_string(*t)).collect::<Vec<String>>() | |
29967ef6 XL |
217 | ); |
218 | ||
a2a8927a XL |
219 | // If there is no expectation, expect formal_input_tys. |
220 | let expected_input_tys = if !expected_input_tys.is_empty() { | |
221 | expected_input_tys | |
222 | } else { | |
223 | formal_input_tys.clone() | |
224 | }; | |
29967ef6 | 225 | |
a2a8927a XL |
226 | assert_eq!(expected_input_tys.len(), formal_input_tys.len()); |
227 | ||
5099ac24 FG |
228 | let provided_arg_count: usize = provided_args.len(); |
229 | ||
a2a8927a | 230 | // Keep track of the fully coerced argument types |
5099ac24 | 231 | let mut final_arg_types: Vec<Option<(Ty<'_>, Ty<'_>)>> = vec![None; provided_arg_count]; |
29967ef6 | 232 | |
a2a8927a XL |
233 | // We introduce a helper function to demand that a given argument satisfy a given input |
234 | // This is more complicated than just checking type equality, as arguments could be coerced | |
235 | // This version writes those types back so further type checking uses the narrowed types | |
5099ac24 | 236 | let demand_compatible = |idx, final_arg_types: &mut Vec<Option<(Ty<'tcx>, Ty<'tcx>)>>| { |
a2a8927a XL |
237 | let formal_input_ty: Ty<'tcx> = formal_input_tys[idx]; |
238 | let expected_input_ty: Ty<'tcx> = expected_input_tys[idx]; | |
239 | let provided_arg = &provided_args[idx]; | |
240 | ||
241 | debug!("checking argument {}: {:?} = {:?}", idx, provided_arg, formal_input_ty); | |
242 | ||
243 | // The special-cased logic below has three functions: | |
244 | // 1. Provide as good of an expected type as possible. | |
245 | let expectation = Expectation::rvalue_hint(self, expected_input_ty); | |
246 | ||
247 | let checked_ty = self.check_expr_with_expectation(provided_arg, expectation); | |
248 | ||
249 | // 2. Coerce to the most detailed type that could be coerced | |
250 | // to, which is `expected_ty` if `rvalue_hint` returns an | |
251 | // `ExpectHasType(expected_ty)`, or the `formal_ty` otherwise. | |
252 | let coerced_ty = expectation.only_has_type(self).unwrap_or(formal_input_ty); | |
253 | ||
254 | // Keep track of these for below | |
5099ac24 | 255 | final_arg_types[idx] = Some((checked_ty, coerced_ty)); |
a2a8927a XL |
256 | |
257 | // Cause selection errors caused by resolving a single argument to point at the | |
258 | // argument and not the call. This is otherwise redundant with the `demand_coerce` | |
259 | // call immediately after, but it lets us customize the span pointed to in the | |
260 | // fulfillment error to be more accurate. | |
5099ac24 | 261 | let coerced_ty = |
a2a8927a XL |
262 | self.resolve_vars_with_obligations_and_mutate_fulfillment(coerced_ty, |errors| { |
263 | self.point_at_type_arg_instead_of_call_if_possible(errors, call_expr); | |
264 | self.point_at_arg_instead_of_call_if_possible( | |
265 | errors, | |
266 | &final_arg_types, | |
267 | call_expr, | |
268 | call_span, | |
269 | provided_args, | |
270 | ); | |
271 | }); | |
272 | ||
5099ac24 FG |
273 | final_arg_types[idx] = Some((checked_ty, coerced_ty)); |
274 | ||
a2a8927a XL |
275 | // We're processing function arguments so we definitely want to use |
276 | // two-phase borrows. | |
277 | self.demand_coerce(&provided_arg, checked_ty, coerced_ty, None, AllowTwoPhase::Yes); | |
278 | ||
279 | // 3. Relate the expected type and the formal one, | |
280 | // if the expected type was used for the coercion. | |
281 | self.demand_suptype(provided_arg.span, formal_input_ty, coerced_ty); | |
282 | }; | |
283 | ||
29967ef6 XL |
284 | // Check the arguments. |
285 | // We do this in a pretty awful way: first we type-check any arguments | |
286 | // that are not closures, then we type-check the closures. This is so | |
287 | // that we have more information about the types of arguments when we | |
288 | // type-check the functions. This isn't really the right way to do this. | |
136023e0 | 289 | for check_closures in [false, true] { |
29967ef6 XL |
290 | // More awful hacks: before we check argument types, try to do |
291 | // an "opportunistic" trait resolution of any trait bounds on | |
292 | // the call. This helps coercions. | |
293 | if check_closures { | |
294 | self.select_obligations_where_possible(false, |errors| { | |
a2a8927a | 295 | self.point_at_type_arg_instead_of_call_if_possible(errors, call_expr); |
29967ef6 XL |
296 | self.point_at_arg_instead_of_call_if_possible( |
297 | errors, | |
a2a8927a XL |
298 | &final_arg_types, |
299 | call_expr, | |
300 | call_span, | |
301 | &provided_args, | |
29967ef6 XL |
302 | ); |
303 | }) | |
304 | } | |
305 | ||
a2a8927a XL |
306 | let minimum_input_count = formal_input_tys.len(); |
307 | for (idx, arg) in provided_args.iter().enumerate() { | |
29967ef6 XL |
308 | // Warn only for the first loop (the "no closures" one). |
309 | // Closure arguments themselves can't be diverging, but | |
310 | // a previous argument can, e.g., `foo(panic!(), || {})`. | |
311 | if !check_closures { | |
312 | self.warn_if_unreachable(arg.hir_id, arg.span, "expression"); | |
313 | } | |
314 | ||
a2a8927a XL |
315 | // For C-variadic functions, we don't have a declared type for all of |
316 | // the arguments hence we only do our usual type checking with | |
317 | // the arguments who's types we do know. However, we *can* check | |
318 | // for unreachable expressions (see above). | |
319 | // FIXME: unreachable warning current isn't emitted | |
320 | if idx >= minimum_input_count { | |
321 | continue; | |
322 | } | |
29967ef6 | 323 | |
a2a8927a | 324 | let is_closure = matches!(arg.kind, ExprKind::Closure(..)); |
29967ef6 XL |
325 | if is_closure != check_closures { |
326 | continue; | |
327 | } | |
328 | ||
a2a8927a | 329 | demand_compatible(idx, &mut final_arg_types); |
29967ef6 XL |
330 | } |
331 | } | |
332 | ||
5099ac24 FG |
333 | // If there was an error in parameter count, emit that here |
334 | if let Some((expected_count, arg_count, err_code, sugg_unit, sugg_tuple_wrap_args)) = | |
335 | arg_count_error | |
336 | { | |
337 | let (span, start_span, args, ctor_of) = match &call_expr.kind { | |
338 | hir::ExprKind::Call( | |
339 | hir::Expr { | |
340 | span, | |
341 | kind: | |
342 | hir::ExprKind::Path(hir::QPath::Resolved( | |
343 | _, | |
344 | hir::Path { res: Res::Def(DefKind::Ctor(of, _), _), .. }, | |
345 | )), | |
346 | .. | |
347 | }, | |
348 | args, | |
349 | ) => (*span, *span, &args[..], Some(of)), | |
350 | hir::ExprKind::Call(hir::Expr { span, .. }, args) => { | |
351 | (*span, *span, &args[..], None) | |
352 | } | |
353 | hir::ExprKind::MethodCall(path_segment, args, _) => ( | |
354 | path_segment.ident.span, | |
355 | // `sp` doesn't point at the whole `foo.bar()`, only at `bar`. | |
356 | path_segment | |
357 | .args | |
358 | .and_then(|args| args.args.iter().last()) | |
359 | // Account for `foo.bar::<T>()`. | |
360 | .map(|arg| { | |
361 | // Skip the closing `>`. | |
362 | tcx.sess | |
363 | .source_map() | |
364 | .next_point(tcx.sess.source_map().next_point(arg.span())) | |
365 | }) | |
366 | .unwrap_or(path_segment.ident.span), | |
367 | &args[1..], // Skip the receiver. | |
368 | None, // methods are never ctors | |
369 | ), | |
370 | k => span_bug!(call_span, "checking argument types on a non-call: `{:?}`", k), | |
371 | }; | |
372 | let arg_spans = if provided_args.is_empty() { | |
373 | // foo() | |
374 | // ^^^-- supplied 0 arguments | |
375 | // | | |
376 | // expected 2 arguments | |
377 | vec![tcx.sess.source_map().next_point(start_span).with_hi(call_span.hi())] | |
378 | } else { | |
379 | // foo(1, 2, 3) | |
380 | // ^^^ - - - supplied 3 arguments | |
381 | // | | |
382 | // expected 2 arguments | |
383 | args.iter().map(|arg| arg.span).collect::<Vec<Span>>() | |
384 | }; | |
385 | let call_name = match ctor_of { | |
386 | Some(CtorOf::Struct) => "struct", | |
387 | Some(CtorOf::Variant) => "enum variant", | |
388 | None => "function", | |
389 | }; | |
390 | let mut err = tcx.sess.struct_span_err_with_code( | |
391 | span, | |
392 | &format!( | |
393 | "this {} takes {}{} but {} {} supplied", | |
394 | call_name, | |
395 | if c_variadic { "at least " } else { "" }, | |
396 | potentially_plural_count(expected_count, "argument"), | |
397 | potentially_plural_count(arg_count, "argument"), | |
398 | if arg_count == 1 { "was" } else { "were" } | |
399 | ), | |
400 | DiagnosticId::Error(err_code.to_owned()), | |
401 | ); | |
402 | let label = format!("supplied {}", potentially_plural_count(arg_count, "argument")); | |
403 | for (i, span) in arg_spans.into_iter().enumerate() { | |
404 | err.span_label( | |
405 | span, | |
406 | if arg_count == 0 || i + 1 == arg_count { &label } else { "" }, | |
407 | ); | |
408 | } | |
409 | if let Some(def_id) = fn_def_id { | |
410 | if let Some(def_span) = tcx.def_ident_span(def_id) { | |
411 | let mut spans: MultiSpan = def_span.into(); | |
412 | ||
413 | let params = tcx | |
414 | .hir() | |
415 | .get_if_local(def_id) | |
416 | .and_then(|node| node.body_id()) | |
417 | .into_iter() | |
418 | .map(|id| tcx.hir().body(id).params) | |
419 | .flatten(); | |
420 | ||
421 | for param in params { | |
422 | spans.push_span_label(param.span, String::new()); | |
423 | } | |
424 | ||
425 | let def_kind = tcx.def_kind(def_id); | |
426 | err.span_note(spans, &format!("{} defined here", def_kind.descr(def_id))); | |
427 | } | |
428 | } | |
429 | if sugg_unit { | |
430 | let sugg_span = tcx.sess.source_map().end_point(call_expr.span); | |
431 | // remove closing `)` from the span | |
432 | let sugg_span = sugg_span.shrink_to_lo(); | |
433 | err.span_suggestion( | |
434 | sugg_span, | |
435 | "expected the unit value `()`; create it with empty parentheses", | |
436 | String::from("()"), | |
437 | Applicability::MachineApplicable, | |
438 | ); | |
439 | } else if let Some(FnArgsAsTuple { first, last }) = sugg_tuple_wrap_args { | |
440 | err.multipart_suggestion( | |
441 | "use parentheses to construct a tuple", | |
442 | vec![ | |
443 | (first.span.shrink_to_lo(), '('.to_string()), | |
444 | (last.span.shrink_to_hi(), ')'.to_string()), | |
445 | ], | |
446 | Applicability::MachineApplicable, | |
447 | ); | |
448 | } else { | |
449 | err.span_label( | |
450 | span, | |
451 | format!( | |
452 | "expected {}{}", | |
453 | if c_variadic { "at least " } else { "" }, | |
454 | potentially_plural_count(expected_count, "argument") | |
455 | ), | |
456 | ); | |
457 | } | |
458 | err.emit(); | |
459 | } | |
460 | ||
29967ef6 XL |
461 | // We also need to make sure we at least write the ty of the other |
462 | // arguments which we skipped above. | |
463 | if c_variadic { | |
5869c6ff XL |
464 | fn variadic_error<'tcx>(sess: &Session, span: Span, ty: Ty<'tcx>, cast_ty: &str) { |
465 | use crate::structured_errors::MissingCastForVariadicArg; | |
466 | ||
467 | MissingCastForVariadicArg { sess, span, ty, cast_ty }.diagnostic().emit() | |
29967ef6 XL |
468 | } |
469 | ||
a2a8927a | 470 | for arg in provided_args.iter().skip(expected_arg_count) { |
29967ef6 XL |
471 | let arg_ty = self.check_expr(&arg); |
472 | ||
473 | // There are a few types which get autopromoted when passed via varargs | |
474 | // in C but we just error out instead and require explicit casts. | |
475 | let arg_ty = self.structurally_resolved_type(arg.span, arg_ty); | |
476 | match arg_ty.kind() { | |
5869c6ff | 477 | ty::Float(ty::FloatTy::F32) => { |
29967ef6 XL |
478 | variadic_error(tcx.sess, arg.span, arg_ty, "c_double"); |
479 | } | |
5869c6ff | 480 | ty::Int(ty::IntTy::I8 | ty::IntTy::I16) | ty::Bool => { |
29967ef6 XL |
481 | variadic_error(tcx.sess, arg.span, arg_ty, "c_int"); |
482 | } | |
5869c6ff | 483 | ty::Uint(ty::UintTy::U8 | ty::UintTy::U16) => { |
29967ef6 XL |
484 | variadic_error(tcx.sess, arg.span, arg_ty, "c_uint"); |
485 | } | |
486 | ty::FnDef(..) => { | |
487 | let ptr_ty = self.tcx.mk_fn_ptr(arg_ty.fn_sig(self.tcx)); | |
fc512014 | 488 | let ptr_ty = self.resolve_vars_if_possible(ptr_ty); |
29967ef6 XL |
489 | variadic_error(tcx.sess, arg.span, arg_ty, &ptr_ty.to_string()); |
490 | } | |
491 | _ => {} | |
492 | } | |
493 | } | |
494 | } | |
495 | } | |
496 | ||
5099ac24 FG |
497 | fn suggested_tuple_wrap( |
498 | &self, | |
499 | expected_input_tys: &[Ty<'tcx>], | |
500 | provided_args: &'tcx [hir::Expr<'tcx>], | |
501 | ) -> Option<FnArgsAsTuple<'_>> { | |
502 | let [expected_arg_type] = &expected_input_tys[..] else { return None }; | |
503 | ||
504 | let ty::Tuple(expected_elems) = self.resolve_vars_if_possible(*expected_arg_type).kind() | |
505 | else { return None }; | |
506 | ||
507 | let expected_types: Vec<_> = expected_elems.iter().map(|k| k.expect_ty()).collect(); | |
508 | let supplied_types: Vec<_> = provided_args.iter().map(|arg| self.check_expr(arg)).collect(); | |
509 | ||
510 | let all_match = iter::zip(expected_types, supplied_types) | |
511 | .all(|(expected, supplied)| self.can_eq(self.param_env, expected, supplied).is_ok()); | |
512 | ||
513 | if all_match { | |
514 | match provided_args { | |
515 | [] => None, | |
516 | [_] => unreachable!( | |
517 | "shouldn't reach here - need count mismatch between 1-tuple and 1-argument" | |
518 | ), | |
519 | [first, .., last] => Some(FnArgsAsTuple { first, last }), | |
520 | } | |
521 | } else { | |
522 | None | |
523 | } | |
524 | } | |
525 | ||
29967ef6 XL |
526 | // AST fragment checking |
527 | pub(in super::super) fn check_lit( | |
528 | &self, | |
529 | lit: &hir::Lit, | |
530 | expected: Expectation<'tcx>, | |
531 | ) -> Ty<'tcx> { | |
532 | let tcx = self.tcx; | |
533 | ||
534 | match lit.node { | |
535 | ast::LitKind::Str(..) => tcx.mk_static_str(), | |
536 | ast::LitKind::ByteStr(ref v) => { | |
537 | tcx.mk_imm_ref(tcx.lifetimes.re_static, tcx.mk_array(tcx.types.u8, v.len() as u64)) | |
538 | } | |
539 | ast::LitKind::Byte(_) => tcx.types.u8, | |
540 | ast::LitKind::Char(_) => tcx.types.char, | |
5869c6ff XL |
541 | ast::LitKind::Int(_, ast::LitIntType::Signed(t)) => tcx.mk_mach_int(ty::int_ty(t)), |
542 | ast::LitKind::Int(_, ast::LitIntType::Unsigned(t)) => tcx.mk_mach_uint(ty::uint_ty(t)), | |
29967ef6 XL |
543 | ast::LitKind::Int(_, ast::LitIntType::Unsuffixed) => { |
544 | let opt_ty = expected.to_option(self).and_then(|ty| match ty.kind() { | |
545 | ty::Int(_) | ty::Uint(_) => Some(ty), | |
546 | ty::Char => Some(tcx.types.u8), | |
547 | ty::RawPtr(..) => Some(tcx.types.usize), | |
548 | ty::FnDef(..) | ty::FnPtr(_) => Some(tcx.types.usize), | |
549 | _ => None, | |
550 | }); | |
551 | opt_ty.unwrap_or_else(|| self.next_int_var()) | |
552 | } | |
5869c6ff XL |
553 | ast::LitKind::Float(_, ast::LitFloatType::Suffixed(t)) => { |
554 | tcx.mk_mach_float(ty::float_ty(t)) | |
555 | } | |
29967ef6 XL |
556 | ast::LitKind::Float(_, ast::LitFloatType::Unsuffixed) => { |
557 | let opt_ty = expected.to_option(self).and_then(|ty| match ty.kind() { | |
558 | ty::Float(_) => Some(ty), | |
559 | _ => None, | |
560 | }); | |
561 | opt_ty.unwrap_or_else(|| self.next_float_var()) | |
562 | } | |
563 | ast::LitKind::Bool(_) => tcx.types.bool, | |
564 | ast::LitKind::Err(_) => tcx.ty_error(), | |
565 | } | |
566 | } | |
567 | ||
568 | pub fn check_struct_path( | |
569 | &self, | |
570 | qpath: &QPath<'_>, | |
571 | hir_id: hir::HirId, | |
572 | ) -> Option<(&'tcx ty::VariantDef, Ty<'tcx>)> { | |
6a06907d | 573 | let path_span = qpath.span(); |
29967ef6 XL |
574 | let (def, ty) = self.finish_resolving_struct_path(qpath, path_span, hir_id); |
575 | let variant = match def { | |
576 | Res::Err => { | |
577 | self.set_tainted_by_errors(); | |
578 | return None; | |
579 | } | |
580 | Res::Def(DefKind::Variant, _) => match ty.kind() { | |
581 | ty::Adt(adt, substs) => Some((adt.variant_of_res(def), adt.did, substs)), | |
582 | _ => bug!("unexpected type: {:?}", ty), | |
583 | }, | |
584 | Res::Def(DefKind::Struct | DefKind::Union | DefKind::TyAlias | DefKind::AssocTy, _) | |
5099ac24 | 585 | | Res::SelfTy { .. } => match ty.kind() { |
29967ef6 XL |
586 | ty::Adt(adt, substs) if !adt.is_enum() => { |
587 | Some((adt.non_enum_variant(), adt.did, substs)) | |
588 | } | |
589 | _ => None, | |
590 | }, | |
591 | _ => bug!("unexpected definition: {:?}", def), | |
592 | }; | |
593 | ||
594 | if let Some((variant, did, substs)) = variant { | |
595 | debug!("check_struct_path: did={:?} substs={:?}", did, substs); | |
596 | self.write_user_type_annotation_from_substs(hir_id, did, substs, None); | |
597 | ||
598 | // Check bounds on type arguments used in the path. | |
3c0e092e | 599 | self.add_required_obligations(path_span, did, substs); |
29967ef6 XL |
600 | |
601 | Some((variant, ty)) | |
602 | } else { | |
c295e0f8 XL |
603 | match ty.kind() { |
604 | ty::Error(_) => { | |
605 | // E0071 might be caused by a spelling error, which will have | |
606 | // already caused an error message and probably a suggestion | |
607 | // elsewhere. Refrain from emitting more unhelpful errors here | |
608 | // (issue #88844). | |
609 | } | |
610 | _ => { | |
611 | struct_span_err!( | |
612 | self.tcx.sess, | |
613 | path_span, | |
614 | E0071, | |
615 | "expected struct, variant or union type, found {}", | |
616 | ty.sort_string(self.tcx) | |
617 | ) | |
618 | .span_label(path_span, "not a struct") | |
619 | .emit(); | |
620 | } | |
621 | } | |
29967ef6 XL |
622 | None |
623 | } | |
624 | } | |
625 | ||
626 | pub fn check_decl_initializer( | |
627 | &self, | |
a2a8927a XL |
628 | hir_id: hir::HirId, |
629 | pat: &'tcx hir::Pat<'tcx>, | |
29967ef6 XL |
630 | init: &'tcx hir::Expr<'tcx>, |
631 | ) -> Ty<'tcx> { | |
632 | // FIXME(tschottdorf): `contains_explicit_ref_binding()` must be removed | |
633 | // for #42640 (default match binding modes). | |
634 | // | |
635 | // See #44848. | |
a2a8927a | 636 | let ref_bindings = pat.contains_explicit_ref_binding(); |
29967ef6 | 637 | |
a2a8927a | 638 | let local_ty = self.local_ty(init.span, hir_id).revealed_ty; |
29967ef6 XL |
639 | if let Some(m) = ref_bindings { |
640 | // Somewhat subtle: if we have a `ref` binding in the pattern, | |
641 | // we want to avoid introducing coercions for the RHS. This is | |
642 | // both because it helps preserve sanity and, in the case of | |
643 | // ref mut, for soundness (issue #23116). In particular, in | |
644 | // the latter case, we need to be clear that the type of the | |
645 | // referent for the reference that results is *equal to* the | |
646 | // type of the place it is referencing, and not some | |
647 | // supertype thereof. | |
648 | let init_ty = self.check_expr_with_needs(init, Needs::maybe_mut_place(m)); | |
649 | self.demand_eqtype(init.span, local_ty, init_ty); | |
650 | init_ty | |
651 | } else { | |
652 | self.check_expr_coercable_to_type(init, local_ty, None) | |
653 | } | |
654 | } | |
655 | ||
a2a8927a | 656 | pub(in super::super) fn check_decl(&self, decl: Declaration<'tcx>) { |
29967ef6 | 657 | // Determine and write the type which we'll check the pattern against. |
a2a8927a XL |
658 | let decl_ty = self.local_ty(decl.span, decl.hir_id).decl_ty; |
659 | self.write_ty(decl.hir_id, decl_ty); | |
29967ef6 XL |
660 | |
661 | // Type check the initializer. | |
a2a8927a XL |
662 | if let Some(ref init) = decl.init { |
663 | let init_ty = self.check_decl_initializer(decl.hir_id, decl.pat, &init); | |
664 | self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, decl_ty, init_ty); | |
29967ef6 XL |
665 | } |
666 | ||
667 | // Does the expected pattern type originate from an expression and what is the span? | |
a2a8927a | 668 | let (origin_expr, ty_span) = match (decl.ty, decl.init) { |
29967ef6 XL |
669 | (Some(ty), _) => (false, Some(ty.span)), // Bias towards the explicit user type. |
670 | (_, Some(init)) => (true, Some(init.span)), // No explicit type; so use the scrutinee. | |
671 | _ => (false, None), // We have `let $pat;`, so the expected type is unconstrained. | |
672 | }; | |
673 | ||
674 | // Type check the pattern. Override if necessary to avoid knock-on errors. | |
a2a8927a XL |
675 | self.check_pat_top(&decl.pat, decl_ty, ty_span, origin_expr); |
676 | let pat_ty = self.node_ty(decl.pat.hir_id); | |
677 | self.overwrite_local_ty_if_err(decl.hir_id, decl.pat, decl_ty, pat_ty); | |
678 | } | |
679 | ||
680 | /// Type check a `let` statement. | |
681 | pub fn check_decl_local(&self, local: &'tcx hir::Local<'tcx>) { | |
682 | self.check_decl(local.into()); | |
29967ef6 XL |
683 | } |
684 | ||
6a06907d | 685 | pub fn check_stmt(&self, stmt: &'tcx hir::Stmt<'tcx>, is_last: bool) { |
29967ef6 XL |
686 | // Don't do all the complex logic below for `DeclItem`. |
687 | match stmt.kind { | |
688 | hir::StmtKind::Item(..) => return, | |
689 | hir::StmtKind::Local(..) | hir::StmtKind::Expr(..) | hir::StmtKind::Semi(..) => {} | |
690 | } | |
691 | ||
692 | self.warn_if_unreachable(stmt.hir_id, stmt.span, "statement"); | |
693 | ||
694 | // Hide the outer diverging and `has_errors` flags. | |
695 | let old_diverges = self.diverges.replace(Diverges::Maybe); | |
696 | let old_has_errors = self.has_errors.replace(false); | |
697 | ||
698 | match stmt.kind { | |
699 | hir::StmtKind::Local(ref l) => { | |
700 | self.check_decl_local(&l); | |
701 | } | |
702 | // Ignore for now. | |
703 | hir::StmtKind::Item(_) => {} | |
704 | hir::StmtKind::Expr(ref expr) => { | |
705 | // Check with expected type of `()`. | |
706 | self.check_expr_has_type_or_error(&expr, self.tcx.mk_unit(), |err| { | |
6a06907d XL |
707 | if expr.can_have_side_effects() { |
708 | self.suggest_semicolon_at_end(expr.span, err); | |
709 | } | |
29967ef6 XL |
710 | }); |
711 | } | |
712 | hir::StmtKind::Semi(ref expr) => { | |
6a06907d XL |
713 | // All of this is equivalent to calling `check_expr`, but it is inlined out here |
714 | // in order to capture the fact that this `match` is the last statement in its | |
715 | // function. This is done for better suggestions to remove the `;`. | |
716 | let expectation = match expr.kind { | |
717 | hir::ExprKind::Match(..) if is_last => IsLast(stmt.span), | |
718 | _ => NoExpectation, | |
719 | }; | |
720 | self.check_expr_with_expectation(expr, expectation); | |
29967ef6 XL |
721 | } |
722 | } | |
723 | ||
724 | // Combine the diverging and `has_error` flags. | |
725 | self.diverges.set(self.diverges.get() | old_diverges); | |
726 | self.has_errors.set(self.has_errors.get() | old_has_errors); | |
727 | } | |
728 | ||
729 | pub fn check_block_no_value(&self, blk: &'tcx hir::Block<'tcx>) { | |
730 | let unit = self.tcx.mk_unit(); | |
731 | let ty = self.check_block_with_expected(blk, ExpectHasType(unit)); | |
732 | ||
733 | // if the block produces a `!` value, that can always be | |
734 | // (effectively) coerced to unit. | |
735 | if !ty.is_never() { | |
736 | self.demand_suptype(blk.span, unit, ty); | |
737 | } | |
738 | } | |
739 | ||
740 | pub(in super::super) fn check_block_with_expected( | |
741 | &self, | |
742 | blk: &'tcx hir::Block<'tcx>, | |
743 | expected: Expectation<'tcx>, | |
744 | ) -> Ty<'tcx> { | |
5869c6ff | 745 | let prev = self.ps.replace(self.ps.get().recurse(blk)); |
29967ef6 XL |
746 | |
747 | // In some cases, blocks have just one exit, but other blocks | |
748 | // can be targeted by multiple breaks. This can happen both | |
749 | // with labeled blocks as well as when we desugar | |
750 | // a `try { ... }` expression. | |
751 | // | |
752 | // Example 1: | |
753 | // | |
754 | // 'a: { if true { break 'a Err(()); } Ok(()) } | |
755 | // | |
756 | // Here we would wind up with two coercions, one from | |
757 | // `Err(())` and the other from the tail expression | |
758 | // `Ok(())`. If the tail expression is omitted, that's a | |
759 | // "forced unit" -- unless the block diverges, in which | |
760 | // case we can ignore the tail expression (e.g., `'a: { | |
761 | // break 'a 22; }` would not force the type of the block | |
762 | // to be `()`). | |
763 | let tail_expr = blk.expr.as_ref(); | |
764 | let coerce_to_ty = expected.coercion_target_type(self, blk.span); | |
765 | let coerce = if blk.targeted_by_break { | |
766 | CoerceMany::new(coerce_to_ty) | |
767 | } else { | |
768 | let tail_expr: &[&hir::Expr<'_>] = match tail_expr { | |
769 | Some(e) => slice::from_ref(e), | |
770 | None => &[], | |
771 | }; | |
772 | CoerceMany::with_coercion_sites(coerce_to_ty, tail_expr) | |
773 | }; | |
774 | ||
775 | let prev_diverges = self.diverges.get(); | |
776 | let ctxt = BreakableCtxt { coerce: Some(coerce), may_break: false }; | |
777 | ||
778 | let (ctxt, ()) = self.with_breakable_ctxt(blk.hir_id, ctxt, || { | |
6a06907d XL |
779 | for (pos, s) in blk.stmts.iter().enumerate() { |
780 | self.check_stmt(s, blk.stmts.len() - 1 == pos); | |
29967ef6 XL |
781 | } |
782 | ||
783 | // check the tail expression **without** holding the | |
784 | // `enclosing_breakables` lock below. | |
785 | let tail_expr_ty = tail_expr.map(|t| self.check_expr_with_expectation(t, expected)); | |
786 | ||
787 | let mut enclosing_breakables = self.enclosing_breakables.borrow_mut(); | |
788 | let ctxt = enclosing_breakables.find_breakable(blk.hir_id); | |
789 | let coerce = ctxt.coerce.as_mut().unwrap(); | |
790 | if let Some(tail_expr_ty) = tail_expr_ty { | |
791 | let tail_expr = tail_expr.unwrap(); | |
792 | let span = self.get_expr_coercion_span(tail_expr); | |
793 | let cause = self.cause(span, ObligationCauseCode::BlockTailExpression(blk.hir_id)); | |
794 | coerce.coerce(self, &cause, tail_expr, tail_expr_ty); | |
795 | } else { | |
796 | // Subtle: if there is no explicit tail expression, | |
797 | // that is typically equivalent to a tail expression | |
798 | // of `()` -- except if the block diverges. In that | |
799 | // case, there is no value supplied from the tail | |
800 | // expression (assuming there are no other breaks, | |
801 | // this implies that the type of the block will be | |
802 | // `!`). | |
803 | // | |
804 | // #41425 -- label the implicit `()` as being the | |
805 | // "found type" here, rather than the "expected type". | |
806 | if !self.diverges.get().is_always() { | |
807 | // #50009 -- Do not point at the entire fn block span, point at the return type | |
808 | // span, as it is the cause of the requirement, and | |
809 | // `consider_hint_about_removing_semicolon` will point at the last expression | |
810 | // if it were a relevant part of the error. This improves usability in editors | |
811 | // that highlight errors inline. | |
812 | let mut sp = blk.span; | |
813 | let mut fn_span = None; | |
814 | if let Some((decl, ident)) = self.get_parent_fn_decl(blk.hir_id) { | |
815 | let ret_sp = decl.output.span(); | |
816 | if let Some(block_sp) = self.parent_item_span(blk.hir_id) { | |
817 | // HACK: on some cases (`ui/liveness/liveness-issue-2163.rs`) the | |
818 | // output would otherwise be incorrect and even misleading. Make sure | |
819 | // the span we're aiming at correspond to a `fn` body. | |
820 | if block_sp == blk.span { | |
821 | sp = ret_sp; | |
822 | fn_span = Some(ident.span); | |
823 | } | |
824 | } | |
825 | } | |
826 | coerce.coerce_forced_unit( | |
827 | self, | |
828 | &self.misc(sp), | |
829 | &mut |err| { | |
830 | if let Some(expected_ty) = expected.only_has_type(self) { | |
831 | self.consider_hint_about_removing_semicolon(blk, expected_ty, err); | |
3c0e092e XL |
832 | if expected_ty == self.tcx.types.bool { |
833 | // If this is caused by a missing `let` in a `while let`, | |
834 | // silence this redundant error, as we already emit E0070. | |
835 | let parent = self.tcx.hir().get_parent_node(blk.hir_id); | |
836 | let parent = self.tcx.hir().get_parent_node(parent); | |
837 | let parent = self.tcx.hir().get_parent_node(parent); | |
838 | let parent = self.tcx.hir().get_parent_node(parent); | |
839 | let parent = self.tcx.hir().get_parent_node(parent); | |
840 | match self.tcx.hir().find(parent) { | |
841 | Some(hir::Node::Expr(hir::Expr { | |
842 | kind: hir::ExprKind::Loop(_, _, hir::LoopSource::While, _), | |
843 | .. | |
844 | })) => { | |
845 | err.delay_as_bug(); | |
846 | } | |
847 | _ => {} | |
848 | } | |
849 | } | |
29967ef6 XL |
850 | } |
851 | if let Some(fn_span) = fn_span { | |
852 | err.span_label( | |
853 | fn_span, | |
854 | "implicitly returns `()` as its body has no tail or `return` \ | |
855 | expression", | |
856 | ); | |
857 | } | |
858 | }, | |
859 | false, | |
860 | ); | |
861 | } | |
862 | } | |
863 | }); | |
864 | ||
865 | if ctxt.may_break { | |
866 | // If we can break from the block, then the block's exit is always reachable | |
867 | // (... as long as the entry is reachable) - regardless of the tail of the block. | |
868 | self.diverges.set(prev_diverges); | |
869 | } | |
870 | ||
871 | let mut ty = ctxt.coerce.unwrap().complete(self); | |
872 | ||
873 | if self.has_errors.get() || ty.references_error() { | |
874 | ty = self.tcx.ty_error() | |
875 | } | |
876 | ||
877 | self.write_ty(blk.hir_id, ty); | |
878 | ||
5869c6ff | 879 | self.ps.set(prev); |
29967ef6 XL |
880 | ty |
881 | } | |
882 | ||
29967ef6 XL |
883 | /// A common error is to add an extra semicolon: |
884 | /// | |
885 | /// ``` | |
886 | /// fn foo() -> usize { | |
887 | /// 22; | |
888 | /// } | |
889 | /// ``` | |
890 | /// | |
891 | /// This routine checks if the final statement in a block is an | |
892 | /// expression with an explicit semicolon whose type is compatible | |
893 | /// with `expected_ty`. If so, it suggests removing the semicolon. | |
894 | fn consider_hint_about_removing_semicolon( | |
895 | &self, | |
896 | blk: &'tcx hir::Block<'tcx>, | |
897 | expected_ty: Ty<'tcx>, | |
898 | err: &mut DiagnosticBuilder<'_>, | |
899 | ) { | |
900 | if let Some((span_semi, boxed)) = self.could_remove_semicolon(blk, expected_ty) { | |
901 | if let StatementAsExpression::NeedsBoxing = boxed { | |
902 | err.span_suggestion_verbose( | |
903 | span_semi, | |
904 | "consider removing this semicolon and boxing the expression", | |
905 | String::new(), | |
906 | Applicability::HasPlaceholders, | |
907 | ); | |
908 | } else { | |
909 | err.span_suggestion_short( | |
910 | span_semi, | |
911 | "consider removing this semicolon", | |
912 | String::new(), | |
913 | Applicability::MachineApplicable, | |
914 | ); | |
915 | } | |
916 | } | |
917 | } | |
918 | ||
919 | fn parent_item_span(&self, id: hir::HirId) -> Option<Span> { | |
5099ac24 | 920 | let node = self.tcx.hir().get_by_def_id(self.tcx.hir().get_parent_item(id)); |
29967ef6 XL |
921 | match node { |
922 | Node::Item(&hir::Item { kind: hir::ItemKind::Fn(_, _, body_id), .. }) | |
923 | | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(_, body_id), .. }) => { | |
924 | let body = self.tcx.hir().body(body_id); | |
925 | if let ExprKind::Block(block, _) = &body.value.kind { | |
926 | return Some(block.span); | |
927 | } | |
928 | } | |
929 | _ => {} | |
930 | } | |
931 | None | |
932 | } | |
933 | ||
934 | /// Given a function block's `HirId`, returns its `FnDecl` if it exists, or `None` otherwise. | |
935 | fn get_parent_fn_decl(&self, blk_id: hir::HirId) -> Option<(&'tcx hir::FnDecl<'tcx>, Ident)> { | |
5099ac24 | 936 | let parent = self.tcx.hir().get_by_def_id(self.tcx.hir().get_parent_item(blk_id)); |
29967ef6 XL |
937 | self.get_node_fn_decl(parent).map(|(fn_decl, ident, _)| (fn_decl, ident)) |
938 | } | |
939 | ||
940 | /// If `expr` is a `match` expression that has only one non-`!` arm, use that arm's tail | |
941 | /// expression's `Span`, otherwise return `expr.span`. This is done to give better errors | |
942 | /// when given code like the following: | |
943 | /// ```text | |
944 | /// if false { return 0i32; } else { 1u32 } | |
945 | /// // ^^^^ point at this instead of the whole `if` expression | |
946 | /// ``` | |
947 | fn get_expr_coercion_span(&self, expr: &hir::Expr<'_>) -> rustc_span::Span { | |
5869c6ff XL |
948 | let check_in_progress = |elem: &hir::Expr<'_>| { |
949 | self.in_progress_typeck_results | |
950 | .and_then(|typeck_results| typeck_results.borrow().node_type_opt(elem.hir_id)) | |
951 | .and_then(|ty| { | |
952 | if ty.is_never() { | |
953 | None | |
954 | } else { | |
955 | Some(match elem.kind { | |
956 | // Point at the tail expression when possible. | |
957 | hir::ExprKind::Block(block, _) => { | |
958 | block.expr.map_or(block.span, |e| e.span) | |
29967ef6 | 959 | } |
5869c6ff | 960 | _ => elem.span, |
29967ef6 | 961 | }) |
5869c6ff | 962 | } |
29967ef6 | 963 | }) |
5869c6ff XL |
964 | }; |
965 | ||
966 | if let hir::ExprKind::If(_, _, Some(el)) = expr.kind { | |
967 | if let Some(rslt) = check_in_progress(el) { | |
968 | return rslt; | |
29967ef6 XL |
969 | } |
970 | } | |
5869c6ff XL |
971 | |
972 | if let hir::ExprKind::Match(_, arms, _) = expr.kind { | |
973 | let mut iter = arms.iter().filter_map(|arm| check_in_progress(arm.body)); | |
974 | if let Some(span) = iter.next() { | |
975 | if iter.next().is_none() { | |
976 | return span; | |
977 | } | |
978 | } | |
979 | } | |
980 | ||
29967ef6 XL |
981 | expr.span |
982 | } | |
983 | ||
984 | fn overwrite_local_ty_if_err( | |
985 | &self, | |
a2a8927a XL |
986 | hir_id: hir::HirId, |
987 | pat: &'tcx hir::Pat<'tcx>, | |
29967ef6 XL |
988 | decl_ty: Ty<'tcx>, |
989 | ty: Ty<'tcx>, | |
990 | ) { | |
991 | if ty.references_error() { | |
992 | // Override the types everywhere with `err()` to avoid knock on errors. | |
a2a8927a XL |
993 | self.write_ty(hir_id, ty); |
994 | self.write_ty(pat.hir_id, ty); | |
29967ef6 | 995 | let local_ty = LocalTy { decl_ty, revealed_ty: ty }; |
a2a8927a XL |
996 | self.locals.borrow_mut().insert(hir_id, local_ty); |
997 | self.locals.borrow_mut().insert(pat.hir_id, local_ty); | |
29967ef6 XL |
998 | } |
999 | } | |
1000 | ||
1001 | // Finish resolving a path in a struct expression or pattern `S::A { .. }` if necessary. | |
1002 | // The newly resolved definition is written into `type_dependent_defs`. | |
1003 | fn finish_resolving_struct_path( | |
1004 | &self, | |
1005 | qpath: &QPath<'_>, | |
1006 | path_span: Span, | |
1007 | hir_id: hir::HirId, | |
1008 | ) -> (Res, Ty<'tcx>) { | |
1009 | match *qpath { | |
1010 | QPath::Resolved(ref maybe_qself, ref path) => { | |
1011 | let self_ty = maybe_qself.as_ref().map(|qself| self.to_ty(qself)); | |
6a06907d | 1012 | let ty = <dyn AstConv<'_>>::res_to_ty(self, self_ty, path, true); |
29967ef6 XL |
1013 | (path.res, ty) |
1014 | } | |
1015 | QPath::TypeRelative(ref qself, ref segment) => { | |
1016 | let ty = self.to_ty(qself); | |
1017 | ||
1018 | let res = if let hir::TyKind::Path(QPath::Resolved(_, ref path)) = qself.kind { | |
1019 | path.res | |
1020 | } else { | |
1021 | Res::Err | |
1022 | }; | |
6a06907d XL |
1023 | let result = <dyn AstConv<'_>>::associated_path_to_ty( |
1024 | self, hir_id, path_span, ty, res, segment, true, | |
1025 | ); | |
29967ef6 XL |
1026 | let ty = result.map(|(ty, _, _)| ty).unwrap_or_else(|_| self.tcx().ty_error()); |
1027 | let result = result.map(|(_, kind, def_id)| (kind, def_id)); | |
1028 | ||
1029 | // Write back the new resolution. | |
1030 | self.write_resolution(hir_id, result); | |
1031 | ||
5869c6ff | 1032 | (result.map_or(Res::Err, |(kind, def_id)| Res::Def(kind, def_id)), ty) |
29967ef6 | 1033 | } |
a2a8927a XL |
1034 | QPath::LangItem(lang_item, span, id) => { |
1035 | self.resolve_lang_item_path(lang_item, span, hir_id, id) | |
29967ef6 XL |
1036 | } |
1037 | } | |
1038 | } | |
1039 | ||
1040 | /// Given a vec of evaluated `FulfillmentError`s and an `fn` call argument expressions, we walk | |
1041 | /// the checked and coerced types for each argument to see if any of the `FulfillmentError`s | |
1042 | /// reference a type argument. The reason to walk also the checked type is that the coerced type | |
1043 | /// can be not easily comparable with predicate type (because of coercion). If the types match | |
1044 | /// for either checked or coerced type, and there's only *one* argument that does, we point at | |
1045 | /// the corresponding argument's expression span instead of the `fn` call path span. | |
1046 | fn point_at_arg_instead_of_call_if_possible( | |
1047 | &self, | |
1048 | errors: &mut Vec<traits::FulfillmentError<'tcx>>, | |
5099ac24 | 1049 | final_arg_types: &[Option<(Ty<'tcx>, Ty<'tcx>)>], |
c295e0f8 | 1050 | expr: &'tcx hir::Expr<'tcx>, |
29967ef6 XL |
1051 | call_sp: Span, |
1052 | args: &'tcx [hir::Expr<'tcx>], | |
1053 | ) { | |
1054 | // We *do not* do this for desugared call spans to keep good diagnostics when involving | |
1055 | // the `?` operator. | |
1056 | if call_sp.desugaring_kind().is_some() { | |
1057 | return; | |
1058 | } | |
1059 | ||
1060 | for error in errors { | |
1061 | // Only if the cause is somewhere inside the expression we want try to point at arg. | |
1062 | // Otherwise, it means that the cause is somewhere else and we should not change | |
1063 | // anything because we can break the correct span. | |
1064 | if !call_sp.contains(error.obligation.cause.span) { | |
1065 | continue; | |
1066 | } | |
1067 | ||
3c0e092e XL |
1068 | // Peel derived obligation, because it's the type that originally |
1069 | // started this inference chain that matters, not the one we wound | |
1070 | // up with at the end. | |
1071 | fn unpeel_to_top( | |
1072 | mut code: Lrc<ObligationCauseCode<'_>>, | |
1073 | ) -> Lrc<ObligationCauseCode<'_>> { | |
1074 | let mut result_code = code.clone(); | |
1075 | loop { | |
1076 | let parent = match &*code { | |
1077 | ObligationCauseCode::BuiltinDerivedObligation(c) | |
1078 | | ObligationCauseCode::ImplDerivedObligation(c) | |
1079 | | ObligationCauseCode::DerivedObligation(c) => c.parent_code.clone(), | |
1080 | _ => break, | |
1081 | }; | |
1082 | result_code = std::mem::replace(&mut code, parent); | |
1083 | } | |
1084 | result_code | |
1085 | } | |
a2a8927a | 1086 | let self_: ty::subst::GenericArg<'_> = match &*unpeel_to_top(error.obligation.cause.clone_code()) { |
3c0e092e XL |
1087 | ObligationCauseCode::BuiltinDerivedObligation(code) | |
1088 | ObligationCauseCode::ImplDerivedObligation(code) | | |
1089 | ObligationCauseCode::DerivedObligation(code) => { | |
5099ac24 | 1090 | code.parent_trait_pred.self_ty().skip_binder().into() |
3c0e092e XL |
1091 | } |
1092 | _ if let ty::PredicateKind::Trait(predicate) = | |
1093 | error.obligation.predicate.kind().skip_binder() => { | |
1094 | predicate.self_ty().into() | |
1095 | } | |
1096 | _ => continue, | |
1097 | }; | |
1098 | let self_ = self.resolve_vars_if_possible(self_); | |
1099 | ||
1100 | // Collect the argument position for all arguments that could have caused this | |
1101 | // `FulfillmentError`. | |
1102 | let mut referenced_in = final_arg_types | |
1103 | .iter() | |
5099ac24 FG |
1104 | .enumerate() |
1105 | .filter_map(|(i, arg)| match arg { | |
1106 | Some((checked_ty, coerce_ty)) => Some([(i, *checked_ty), (i, *coerce_ty)]), | |
1107 | _ => None, | |
1108 | }) | |
1109 | .flatten() | |
3c0e092e XL |
1110 | .flat_map(|(i, ty)| { |
1111 | let ty = self.resolve_vars_if_possible(ty); | |
1112 | // We walk the argument type because the argument's type could have | |
1113 | // been `Option<T>`, but the `FulfillmentError` references `T`. | |
5099ac24 | 1114 | if ty.walk().any(|arg| arg == self_) { Some(i) } else { None } |
3c0e092e XL |
1115 | }) |
1116 | .collect::<Vec<usize>>(); | |
1117 | ||
1118 | // Both checked and coerced types could have matched, thus we need to remove | |
1119 | // duplicates. | |
1120 | ||
1121 | // We sort primitive type usize here and can use unstable sort | |
1122 | referenced_in.sort_unstable(); | |
1123 | referenced_in.dedup(); | |
1124 | ||
1125 | if let (Some(ref_in), None) = (referenced_in.pop(), referenced_in.pop()) { | |
1126 | // Do not point at the inside of a macro. | |
1127 | // That would often result in poor error messages. | |
1128 | if args[ref_in].span.from_expansion() { | |
1129 | return; | |
1130 | } | |
1131 | // We make sure that only *one* argument matches the obligation failure | |
1132 | // and we assign the obligation's span to its expression's. | |
a2a8927a XL |
1133 | error.obligation.cause.span = args[ref_in].span; |
1134 | let parent_code = error.obligation.cause.clone_code(); | |
1135 | *error.obligation.cause.make_mut_code() = | |
3c0e092e XL |
1136 | ObligationCauseCode::FunctionArgumentObligation { |
1137 | arg_hir_id: args[ref_in].hir_id, | |
1138 | call_hir_id: expr.hir_id, | |
a2a8927a | 1139 | parent_code, |
3c0e092e | 1140 | }; |
a2a8927a | 1141 | } else if error.obligation.cause.span == call_sp { |
3c0e092e XL |
1142 | // Make function calls point at the callee, not the whole thing. |
1143 | if let hir::ExprKind::Call(callee, _) = expr.kind { | |
a2a8927a | 1144 | error.obligation.cause.span = callee.span; |
29967ef6 XL |
1145 | } |
1146 | } | |
1147 | } | |
1148 | } | |
1149 | ||
1150 | /// Given a vec of evaluated `FulfillmentError`s and an `fn` call expression, we walk the | |
1151 | /// `PathSegment`s and resolve their type parameters to see if any of the `FulfillmentError`s | |
1152 | /// were caused by them. If they were, we point at the corresponding type argument's span | |
1153 | /// instead of the `fn` call path span. | |
1154 | fn point_at_type_arg_instead_of_call_if_possible( | |
1155 | &self, | |
1156 | errors: &mut Vec<traits::FulfillmentError<'tcx>>, | |
1157 | call_expr: &'tcx hir::Expr<'tcx>, | |
1158 | ) { | |
1159 | if let hir::ExprKind::Call(path, _) = &call_expr.kind { | |
3c0e092e XL |
1160 | if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = &path.kind { |
1161 | for error in errors { | |
1162 | if let ty::PredicateKind::Trait(predicate) = | |
1163 | error.obligation.predicate.kind().skip_binder() | |
1164 | { | |
1165 | // If any of the type arguments in this path segment caused the | |
1166 | // `FulfillmentError`, point at its span (#61860). | |
1167 | for arg in path | |
1168 | .segments | |
1169 | .iter() | |
1170 | .filter_map(|seg| seg.args.as_ref()) | |
1171 | .flat_map(|a| a.args.iter()) | |
29967ef6 | 1172 | { |
3c0e092e XL |
1173 | if let hir::GenericArg::Type(hir_ty) = &arg { |
1174 | if let hir::TyKind::Path(hir::QPath::TypeRelative(..)) = | |
1175 | &hir_ty.kind | |
1176 | { | |
1177 | // Avoid ICE with associated types. As this is best | |
1178 | // effort only, it's ok to ignore the case. It | |
1179 | // would trigger in `is_send::<T::AssocType>();` | |
1180 | // from `typeck-default-trait-impl-assoc-type.rs`. | |
1181 | } else { | |
1182 | let ty = <dyn AstConv<'_>>::ast_ty_to_ty(self, hir_ty); | |
1183 | let ty = self.resolve_vars_if_possible(ty); | |
1184 | if ty == predicate.self_ty() { | |
a2a8927a | 1185 | error.obligation.cause.span = hir_ty.span; |
29967ef6 XL |
1186 | } |
1187 | } | |
1188 | } | |
1189 | } | |
1190 | } | |
1191 | } | |
1192 | } | |
1193 | } | |
1194 | } | |
1195 | } |