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