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