]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_typeck/src/check/expr.rs
New upstream version 1.49.0~beta.4+dfsg1
[rustc.git] / compiler / rustc_typeck / src / check / expr.rs
1 //! Type checking expressions.
2 //!
3 //! See `mod.rs` for more context on type checking in general.
4
5 use crate::astconv::AstConv as _;
6 use crate::check::cast;
7 use crate::check::coercion::CoerceMany;
8 use crate::check::fatally_break_rust;
9 use crate::check::method::{probe, MethodError, SelfSource};
10 use crate::check::report_unexpected_variant_res;
11 use crate::check::BreakableCtxt;
12 use crate::check::Diverges;
13 use crate::check::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExpectation};
14 use crate::check::FnCtxt;
15 use crate::check::Needs;
16 use crate::check::TupleArgumentsFlag::DontTupleArguments;
17 use crate::errors::{
18 FieldMultiplySpecifiedInInitializer, FunctionalRecordUpdateOnNonStruct,
19 YieldExprOutsideOfGenerator,
20 };
21 use crate::type_error_struct;
22
23 use crate::errors::{AddressOfTemporaryTaken, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive};
24 use rustc_ast as ast;
25 use rustc_ast::util::lev_distance::find_best_match_for_name;
26 use rustc_data_structures::fx::FxHashMap;
27 use rustc_data_structures::stack::ensure_sufficient_stack;
28 use rustc_errors::ErrorReported;
29 use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, DiagnosticId};
30 use rustc_hir as hir;
31 use rustc_hir::def::{CtorKind, DefKind, Res};
32 use rustc_hir::def_id::DefId;
33 use rustc_hir::lang_items::LangItem;
34 use rustc_hir::{ExprKind, QPath};
35 use rustc_infer::infer;
36 use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
37 use rustc_middle::ty;
38 use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
39 use rustc_middle::ty::Ty;
40 use rustc_middle::ty::TypeFoldable;
41 use rustc_middle::ty::{AdtKind, Visibility};
42 use rustc_span::hygiene::DesugaringKind;
43 use rustc_span::source_map::Span;
44 use rustc_span::symbol::{kw, sym, Ident, Symbol};
45 use rustc_trait_selection::traits::{self, ObligationCauseCode};
46
47 use std::fmt::Display;
48
49 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
50 fn check_expr_eq_type(&self, expr: &'tcx hir::Expr<'tcx>, expected: Ty<'tcx>) {
51 let ty = self.check_expr_with_hint(expr, expected);
52 self.demand_eqtype(expr.span, expected, ty);
53 }
54
55 pub fn check_expr_has_type_or_error(
56 &self,
57 expr: &'tcx hir::Expr<'tcx>,
58 expected: Ty<'tcx>,
59 extend_err: impl Fn(&mut DiagnosticBuilder<'_>),
60 ) -> Ty<'tcx> {
61 self.check_expr_meets_expectation_or_error(expr, ExpectHasType(expected), extend_err)
62 }
63
64 fn check_expr_meets_expectation_or_error(
65 &self,
66 expr: &'tcx hir::Expr<'tcx>,
67 expected: Expectation<'tcx>,
68 extend_err: impl Fn(&mut DiagnosticBuilder<'_>),
69 ) -> Ty<'tcx> {
70 let expected_ty = expected.to_option(&self).unwrap_or(self.tcx.types.bool);
71 let mut ty = self.check_expr_with_expectation(expr, expected);
72
73 // While we don't allow *arbitrary* coercions here, we *do* allow
74 // coercions from ! to `expected`.
75 if ty.is_never() {
76 assert!(
77 !self.typeck_results.borrow().adjustments().contains_key(expr.hir_id),
78 "expression with never type wound up being adjusted"
79 );
80 let adj_ty = self.next_diverging_ty_var(TypeVariableOrigin {
81 kind: TypeVariableOriginKind::AdjustmentType,
82 span: expr.span,
83 });
84 self.apply_adjustments(
85 expr,
86 vec![Adjustment { kind: Adjust::NeverToAny, target: adj_ty }],
87 );
88 ty = adj_ty;
89 }
90
91 if let Some(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
92 let expr = expr.peel_drop_temps();
93 self.suggest_deref_ref_or_into(&mut err, expr, expected_ty, ty, None);
94 extend_err(&mut err);
95 // Error possibly reported in `check_assign` so avoid emitting error again.
96 err.emit_unless(self.is_assign_to_bool(expr, expected_ty));
97 }
98 ty
99 }
100
101 pub(super) fn check_expr_coercable_to_type(
102 &self,
103 expr: &'tcx hir::Expr<'tcx>,
104 expected: Ty<'tcx>,
105 expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
106 ) -> Ty<'tcx> {
107 let ty = self.check_expr_with_hint(expr, expected);
108 // checks don't need two phase
109 self.demand_coerce(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No)
110 }
111
112 pub(super) fn check_expr_with_hint(
113 &self,
114 expr: &'tcx hir::Expr<'tcx>,
115 expected: Ty<'tcx>,
116 ) -> Ty<'tcx> {
117 self.check_expr_with_expectation(expr, ExpectHasType(expected))
118 }
119
120 fn check_expr_with_expectation_and_needs(
121 &self,
122 expr: &'tcx hir::Expr<'tcx>,
123 expected: Expectation<'tcx>,
124 needs: Needs,
125 ) -> Ty<'tcx> {
126 let ty = self.check_expr_with_expectation(expr, expected);
127
128 // If the expression is used in a place whether mutable place is required
129 // e.g. LHS of assignment, perform the conversion.
130 if let Needs::MutPlace = needs {
131 self.convert_place_derefs_to_mutable(expr);
132 }
133
134 ty
135 }
136
137 pub(super) fn check_expr(&self, expr: &'tcx hir::Expr<'tcx>) -> Ty<'tcx> {
138 self.check_expr_with_expectation(expr, NoExpectation)
139 }
140
141 pub(super) fn check_expr_with_needs(
142 &self,
143 expr: &'tcx hir::Expr<'tcx>,
144 needs: Needs,
145 ) -> Ty<'tcx> {
146 self.check_expr_with_expectation_and_needs(expr, NoExpectation, needs)
147 }
148
149 /// Invariant:
150 /// If an expression has any sub-expressions that result in a type error,
151 /// inspecting that expression's type with `ty.references_error()` will return
152 /// true. Likewise, if an expression is known to diverge, inspecting its
153 /// type with `ty::type_is_bot` will return true (n.b.: since Rust is
154 /// strict, _|_ can appear in the type of an expression that does not,
155 /// itself, diverge: for example, fn() -> _|_.)
156 /// Note that inspecting a type's structure *directly* may expose the fact
157 /// that there are actually multiple representations for `Error`, so avoid
158 /// that when err needs to be handled differently.
159 pub(super) fn check_expr_with_expectation(
160 &self,
161 expr: &'tcx hir::Expr<'tcx>,
162 expected: Expectation<'tcx>,
163 ) -> Ty<'tcx> {
164 debug!(">> type-checking: expr={:?} expected={:?}", expr, expected);
165
166 // True if `expr` is a `Try::from_ok(())` that is a result of desugaring a try block
167 // without the final expr (e.g. `try { return; }`). We don't want to generate an
168 // unreachable_code lint for it since warnings for autogenerated code are confusing.
169 let is_try_block_generated_unit_expr = match expr.kind {
170 ExprKind::Call(_, ref args) if expr.span.is_desugaring(DesugaringKind::TryBlock) => {
171 args.len() == 1 && args[0].span.is_desugaring(DesugaringKind::TryBlock)
172 }
173
174 _ => false,
175 };
176
177 // Warn for expressions after diverging siblings.
178 if !is_try_block_generated_unit_expr {
179 self.warn_if_unreachable(expr.hir_id, expr.span, "expression");
180 }
181
182 // Hide the outer diverging and has_errors flags.
183 let old_diverges = self.diverges.replace(Diverges::Maybe);
184 let old_has_errors = self.has_errors.replace(false);
185
186 let ty = ensure_sufficient_stack(|| self.check_expr_kind(expr, expected));
187
188 // Warn for non-block expressions with diverging children.
189 match expr.kind {
190 ExprKind::Block(..) | ExprKind::Loop(..) | ExprKind::Match(..) => {}
191 // If `expr` is a result of desugaring the try block and is an ok-wrapped
192 // diverging expression (e.g. it arose from desugaring of `try { return }`),
193 // we skip issuing a warning because it is autogenerated code.
194 ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::TryBlock) => {}
195 ExprKind::Call(ref callee, _) => {
196 self.warn_if_unreachable(expr.hir_id, callee.span, "call")
197 }
198 ExprKind::MethodCall(_, ref span, _, _) => {
199 self.warn_if_unreachable(expr.hir_id, *span, "call")
200 }
201 _ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
202 }
203
204 // Any expression that produces a value of type `!` must have diverged
205 if ty.is_never() {
206 self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
207 }
208
209 // Record the type, which applies it effects.
210 // We need to do this after the warning above, so that
211 // we don't warn for the diverging expression itself.
212 self.write_ty(expr.hir_id, ty);
213
214 // Combine the diverging and has_error flags.
215 self.diverges.set(self.diverges.get() | old_diverges);
216 self.has_errors.set(self.has_errors.get() | old_has_errors);
217
218 debug!("type of {} is...", self.tcx.hir().node_to_string(expr.hir_id));
219 debug!("... {:?}, expected is {:?}", ty, expected);
220
221 ty
222 }
223
224 fn check_expr_kind(
225 &self,
226 expr: &'tcx hir::Expr<'tcx>,
227 expected: Expectation<'tcx>,
228 ) -> Ty<'tcx> {
229 debug!("check_expr_kind(expr={:?}, expected={:?})", expr, expected);
230
231 let tcx = self.tcx;
232 match expr.kind {
233 ExprKind::Box(ref subexpr) => self.check_expr_box(subexpr, expected),
234 ExprKind::Lit(ref lit) => self.check_lit(&lit, expected),
235 ExprKind::Binary(op, ref lhs, ref rhs) => self.check_binop(expr, op, lhs, rhs),
236 ExprKind::Assign(ref lhs, ref rhs, ref span) => {
237 self.check_expr_assign(expr, expected, lhs, rhs, span)
238 }
239 ExprKind::AssignOp(op, ref lhs, ref rhs) => self.check_binop_assign(expr, op, lhs, rhs),
240 ExprKind::Unary(unop, ref oprnd) => self.check_expr_unary(unop, oprnd, expected, expr),
241 ExprKind::AddrOf(kind, mutbl, ref oprnd) => {
242 self.check_expr_addr_of(kind, mutbl, oprnd, expected, expr)
243 }
244 ExprKind::Path(QPath::LangItem(lang_item, _)) => {
245 self.check_lang_item_path(lang_item, expr)
246 }
247 ExprKind::Path(ref qpath) => self.check_expr_path(qpath, expr),
248 ExprKind::InlineAsm(asm) => self.check_expr_asm(asm),
249 ExprKind::LlvmInlineAsm(ref asm) => {
250 for expr in asm.outputs_exprs.iter().chain(asm.inputs_exprs.iter()) {
251 self.check_expr(expr);
252 }
253 tcx.mk_unit()
254 }
255 ExprKind::Break(destination, ref expr_opt) => {
256 self.check_expr_break(destination, expr_opt.as_deref(), expr)
257 }
258 ExprKind::Continue(destination) => {
259 if destination.target_id.is_ok() {
260 tcx.types.never
261 } else {
262 // There was an error; make type-check fail.
263 tcx.ty_error()
264 }
265 }
266 ExprKind::Ret(ref expr_opt) => self.check_expr_return(expr_opt.as_deref(), expr),
267 ExprKind::Loop(ref body, _, source) => {
268 self.check_expr_loop(body, source, expected, expr)
269 }
270 ExprKind::Match(ref discrim, ref arms, match_src) => {
271 self.check_match(expr, &discrim, arms, expected, match_src)
272 }
273 ExprKind::Closure(capture, ref decl, body_id, _, gen) => {
274 self.check_expr_closure(expr, capture, &decl, body_id, gen, expected)
275 }
276 ExprKind::Block(ref body, _) => self.check_block_with_expected(&body, expected),
277 ExprKind::Call(ref callee, ref args) => self.check_call(expr, &callee, args, expected),
278 ExprKind::MethodCall(ref segment, span, ref args, _) => {
279 self.check_method_call(expr, segment, span, args, expected)
280 }
281 ExprKind::Cast(ref e, ref t) => self.check_expr_cast(e, t, expr),
282 ExprKind::Type(ref e, ref t) => {
283 let ty = self.to_ty_saving_user_provided_ty(&t);
284 self.check_expr_eq_type(&e, ty);
285 ty
286 }
287 ExprKind::DropTemps(ref e) => self.check_expr_with_expectation(e, expected),
288 ExprKind::Array(ref args) => self.check_expr_array(args, expected, expr),
289 ExprKind::ConstBlock(ref anon_const) => self.to_const(anon_const).ty,
290 ExprKind::Repeat(ref element, ref count) => {
291 self.check_expr_repeat(element, count, expected, expr)
292 }
293 ExprKind::Tup(ref elts) => self.check_expr_tuple(elts, expected, expr),
294 ExprKind::Struct(ref qpath, fields, ref base_expr) => {
295 self.check_expr_struct(expr, expected, qpath, fields, base_expr)
296 }
297 ExprKind::Field(ref base, field) => self.check_field(expr, &base, field),
298 ExprKind::Index(ref base, ref idx) => self.check_expr_index(base, idx, expr),
299 ExprKind::Yield(ref value, ref src) => self.check_expr_yield(value, expr, src),
300 hir::ExprKind::Err => tcx.ty_error(),
301 }
302 }
303
304 fn check_expr_box(&self, expr: &'tcx hir::Expr<'tcx>, expected: Expectation<'tcx>) -> Ty<'tcx> {
305 let expected_inner = expected.to_option(self).map_or(NoExpectation, |ty| match ty.kind() {
306 ty::Adt(def, _) if def.is_box() => Expectation::rvalue_hint(self, ty.boxed_ty()),
307 _ => NoExpectation,
308 });
309 let referent_ty = self.check_expr_with_expectation(expr, expected_inner);
310 self.tcx.mk_box(referent_ty)
311 }
312
313 fn check_expr_unary(
314 &self,
315 unop: hir::UnOp,
316 oprnd: &'tcx hir::Expr<'tcx>,
317 expected: Expectation<'tcx>,
318 expr: &'tcx hir::Expr<'tcx>,
319 ) -> Ty<'tcx> {
320 let tcx = self.tcx;
321 let expected_inner = match unop {
322 hir::UnOp::UnNot | hir::UnOp::UnNeg => expected,
323 hir::UnOp::UnDeref => NoExpectation,
324 };
325 let mut oprnd_t = self.check_expr_with_expectation(&oprnd, expected_inner);
326
327 if !oprnd_t.references_error() {
328 oprnd_t = self.structurally_resolved_type(expr.span, oprnd_t);
329 match unop {
330 hir::UnOp::UnDeref => {
331 if let Some(ty) = self.lookup_derefing(expr, oprnd, oprnd_t) {
332 oprnd_t = ty;
333 } else {
334 let mut err = type_error_struct!(
335 tcx.sess,
336 expr.span,
337 oprnd_t,
338 E0614,
339 "type `{}` cannot be dereferenced",
340 oprnd_t,
341 );
342 let sp = tcx.sess.source_map().start_point(expr.span);
343 if let Some(sp) =
344 tcx.sess.parse_sess.ambiguous_block_expr_parse.borrow().get(&sp)
345 {
346 tcx.sess.parse_sess.expr_parentheses_needed(&mut err, *sp, None);
347 }
348 err.emit();
349 oprnd_t = tcx.ty_error();
350 }
351 }
352 hir::UnOp::UnNot => {
353 let result = self.check_user_unop(expr, oprnd_t, unop);
354 // If it's builtin, we can reuse the type, this helps inference.
355 if !(oprnd_t.is_integral() || *oprnd_t.kind() == ty::Bool) {
356 oprnd_t = result;
357 }
358 }
359 hir::UnOp::UnNeg => {
360 let result = self.check_user_unop(expr, oprnd_t, unop);
361 // If it's builtin, we can reuse the type, this helps inference.
362 if !oprnd_t.is_numeric() {
363 oprnd_t = result;
364 }
365 }
366 }
367 }
368 oprnd_t
369 }
370
371 fn check_expr_addr_of(
372 &self,
373 kind: hir::BorrowKind,
374 mutbl: hir::Mutability,
375 oprnd: &'tcx hir::Expr<'tcx>,
376 expected: Expectation<'tcx>,
377 expr: &'tcx hir::Expr<'tcx>,
378 ) -> Ty<'tcx> {
379 let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| {
380 match ty.kind() {
381 ty::Ref(_, ty, _) | ty::RawPtr(ty::TypeAndMut { ty, .. }) => {
382 if oprnd.is_syntactic_place_expr() {
383 // Places may legitimately have unsized types.
384 // For example, dereferences of a fat pointer and
385 // the last field of a struct can be unsized.
386 ExpectHasType(ty)
387 } else {
388 Expectation::rvalue_hint(self, ty)
389 }
390 }
391 _ => NoExpectation,
392 }
393 });
394 let ty =
395 self.check_expr_with_expectation_and_needs(&oprnd, hint, Needs::maybe_mut_place(mutbl));
396
397 let tm = ty::TypeAndMut { ty, mutbl };
398 match kind {
399 _ if tm.ty.references_error() => self.tcx.ty_error(),
400 hir::BorrowKind::Raw => {
401 self.check_named_place_expr(oprnd);
402 self.tcx.mk_ptr(tm)
403 }
404 hir::BorrowKind::Ref => {
405 // Note: at this point, we cannot say what the best lifetime
406 // is to use for resulting pointer. We want to use the
407 // shortest lifetime possible so as to avoid spurious borrowck
408 // errors. Moreover, the longest lifetime will depend on the
409 // precise details of the value whose address is being taken
410 // (and how long it is valid), which we don't know yet until
411 // type inference is complete.
412 //
413 // Therefore, here we simply generate a region variable. The
414 // region inferencer will then select a suitable value.
415 // Finally, borrowck will infer the value of the region again,
416 // this time with enough precision to check that the value
417 // whose address was taken can actually be made to live as long
418 // as it needs to live.
419 let region = self.next_region_var(infer::AddrOfRegion(expr.span));
420 self.tcx.mk_ref(region, tm)
421 }
422 }
423 }
424
425 /// Does this expression refer to a place that either:
426 /// * Is based on a local or static.
427 /// * Contains a dereference
428 /// Note that the adjustments for the children of `expr` should already
429 /// have been resolved.
430 fn check_named_place_expr(&self, oprnd: &'tcx hir::Expr<'tcx>) {
431 let is_named = oprnd.is_place_expr(|base| {
432 // Allow raw borrows if there are any deref adjustments.
433 //
434 // const VAL: (i32,) = (0,);
435 // const REF: &(i32,) = &(0,);
436 //
437 // &raw const VAL.0; // ERROR
438 // &raw const REF.0; // OK, same as &raw const (*REF).0;
439 //
440 // This is maybe too permissive, since it allows
441 // `let u = &raw const Box::new((1,)).0`, which creates an
442 // immediately dangling raw pointer.
443 self.typeck_results
444 .borrow()
445 .adjustments()
446 .get(base.hir_id)
447 .map_or(false, |x| x.iter().any(|adj| matches!(adj.kind, Adjust::Deref(_))))
448 });
449 if !is_named {
450 self.tcx.sess.emit_err(AddressOfTemporaryTaken { span: oprnd.span })
451 }
452 }
453
454 fn check_lang_item_path(
455 &self,
456 lang_item: hir::LangItem,
457 expr: &'tcx hir::Expr<'tcx>,
458 ) -> Ty<'tcx> {
459 self.resolve_lang_item_path(lang_item, expr.span, expr.hir_id).1
460 }
461
462 fn check_expr_path(&self, qpath: &hir::QPath<'_>, expr: &'tcx hir::Expr<'tcx>) -> Ty<'tcx> {
463 let tcx = self.tcx;
464 let (res, opt_ty, segs) = self.resolve_ty_and_res_ufcs(qpath, expr.hir_id, expr.span);
465 let ty = match res {
466 Res::Err => {
467 self.set_tainted_by_errors();
468 tcx.ty_error()
469 }
470 Res::Def(DefKind::Ctor(_, CtorKind::Fictive), _) => {
471 report_unexpected_variant_res(tcx, res, expr.span);
472 tcx.ty_error()
473 }
474 _ => self.instantiate_value_path(segs, opt_ty, res, expr.span, expr.hir_id).0,
475 };
476
477 if let ty::FnDef(..) = ty.kind() {
478 let fn_sig = ty.fn_sig(tcx);
479 if !tcx.features().unsized_fn_params {
480 // We want to remove some Sized bounds from std functions,
481 // but don't want to expose the removal to stable Rust.
482 // i.e., we don't want to allow
483 //
484 // ```rust
485 // drop as fn(str);
486 // ```
487 //
488 // to work in stable even if the Sized bound on `drop` is relaxed.
489 for i in 0..fn_sig.inputs().skip_binder().len() {
490 // We just want to check sizedness, so instead of introducing
491 // placeholder lifetimes with probing, we just replace higher lifetimes
492 // with fresh vars.
493 let input = self
494 .replace_bound_vars_with_fresh_vars(
495 expr.span,
496 infer::LateBoundRegionConversionTime::FnCall,
497 &fn_sig.input(i),
498 )
499 .0;
500 self.require_type_is_sized_deferred(
501 input,
502 expr.span,
503 traits::SizedArgumentType(None),
504 );
505 }
506 }
507 // Here we want to prevent struct constructors from returning unsized types.
508 // There were two cases this happened: fn pointer coercion in stable
509 // and usual function call in presence of unsized_locals.
510 // Also, as we just want to check sizedness, instead of introducing
511 // placeholder lifetimes with probing, we just replace higher lifetimes
512 // with fresh vars.
513 let output = self
514 .replace_bound_vars_with_fresh_vars(
515 expr.span,
516 infer::LateBoundRegionConversionTime::FnCall,
517 &fn_sig.output(),
518 )
519 .0;
520 self.require_type_is_sized_deferred(output, expr.span, traits::SizedReturnType);
521 }
522
523 // We always require that the type provided as the value for
524 // a type parameter outlives the moment of instantiation.
525 let substs = self.typeck_results.borrow().node_substs(expr.hir_id);
526 self.add_wf_bounds(substs, expr);
527
528 ty
529 }
530
531 fn check_expr_break(
532 &self,
533 destination: hir::Destination,
534 expr_opt: Option<&'tcx hir::Expr<'tcx>>,
535 expr: &'tcx hir::Expr<'tcx>,
536 ) -> Ty<'tcx> {
537 let tcx = self.tcx;
538 if let Ok(target_id) = destination.target_id {
539 let (e_ty, cause);
540 if let Some(ref e) = expr_opt {
541 // If this is a break with a value, we need to type-check
542 // the expression. Get an expected type from the loop context.
543 let opt_coerce_to = {
544 // We should release `enclosing_breakables` before the `check_expr_with_hint`
545 // below, so can't move this block of code to the enclosing scope and share
546 // `ctxt` with the second `encloding_breakables` borrow below.
547 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
548 match enclosing_breakables.opt_find_breakable(target_id) {
549 Some(ctxt) => ctxt.coerce.as_ref().map(|coerce| coerce.expected_ty()),
550 None => {
551 // Avoid ICE when `break` is inside a closure (#65383).
552 return tcx.ty_error_with_message(
553 expr.span,
554 "break was outside loop, but no error was emitted",
555 );
556 }
557 }
558 };
559
560 // If the loop context is not a `loop { }`, then break with
561 // a value is illegal, and `opt_coerce_to` will be `None`.
562 // Just set expectation to error in that case.
563 let coerce_to = opt_coerce_to.unwrap_or_else(|| tcx.ty_error());
564
565 // Recurse without `enclosing_breakables` borrowed.
566 e_ty = self.check_expr_with_hint(e, coerce_to);
567 cause = self.misc(e.span);
568 } else {
569 // Otherwise, this is a break *without* a value. That's
570 // always legal, and is equivalent to `break ()`.
571 e_ty = tcx.mk_unit();
572 cause = self.misc(expr.span);
573 }
574
575 // Now that we have type-checked `expr_opt`, borrow
576 // the `enclosing_loops` field and let's coerce the
577 // type of `expr_opt` into what is expected.
578 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
579 let ctxt = match enclosing_breakables.opt_find_breakable(target_id) {
580 Some(ctxt) => ctxt,
581 None => {
582 // Avoid ICE when `break` is inside a closure (#65383).
583 return tcx.ty_error_with_message(
584 expr.span,
585 "break was outside loop, but no error was emitted",
586 );
587 }
588 };
589
590 if let Some(ref mut coerce) = ctxt.coerce {
591 if let Some(ref e) = expr_opt {
592 coerce.coerce(self, &cause, e, e_ty);
593 } else {
594 assert!(e_ty.is_unit());
595 let ty = coerce.expected_ty();
596 coerce.coerce_forced_unit(
597 self,
598 &cause,
599 &mut |mut err| {
600 self.suggest_mismatched_types_on_tail(
601 &mut err, expr, ty, e_ty, cause.span, target_id,
602 );
603 if let Some(val) = ty_kind_suggestion(ty) {
604 let label = destination
605 .label
606 .map(|l| format!(" {}", l.ident))
607 .unwrap_or_else(String::new);
608 err.span_suggestion(
609 expr.span,
610 "give it a value of the expected type",
611 format!("break{} {}", label, val),
612 Applicability::HasPlaceholders,
613 );
614 }
615 },
616 false,
617 );
618 }
619 } else {
620 // If `ctxt.coerce` is `None`, we can just ignore
621 // the type of the expression. This is because
622 // either this was a break *without* a value, in
623 // which case it is always a legal type (`()`), or
624 // else an error would have been flagged by the
625 // `loops` pass for using break with an expression
626 // where you are not supposed to.
627 assert!(expr_opt.is_none() || self.tcx.sess.has_errors());
628 }
629
630 // If we encountered a `break`, then (no surprise) it may be possible to break from the
631 // loop... unless the value being returned from the loop diverges itself, e.g.
632 // `break return 5` or `break loop {}`.
633 ctxt.may_break |= !self.diverges.get().is_always();
634
635 // the type of a `break` is always `!`, since it diverges
636 tcx.types.never
637 } else {
638 // Otherwise, we failed to find the enclosing loop;
639 // this can only happen if the `break` was not
640 // inside a loop at all, which is caught by the
641 // loop-checking pass.
642 let err = self.tcx.ty_error_with_message(
643 expr.span,
644 "break was outside loop, but no error was emitted",
645 );
646
647 // We still need to assign a type to the inner expression to
648 // prevent the ICE in #43162.
649 if let Some(ref e) = expr_opt {
650 self.check_expr_with_hint(e, err);
651
652 // ... except when we try to 'break rust;'.
653 // ICE this expression in particular (see #43162).
654 if let ExprKind::Path(QPath::Resolved(_, ref path)) = e.kind {
655 if path.segments.len() == 1 && path.segments[0].ident.name == sym::rust {
656 fatally_break_rust(self.tcx.sess);
657 }
658 }
659 }
660
661 // There was an error; make type-check fail.
662 err
663 }
664 }
665
666 fn check_expr_return(
667 &self,
668 expr_opt: Option<&'tcx hir::Expr<'tcx>>,
669 expr: &'tcx hir::Expr<'tcx>,
670 ) -> Ty<'tcx> {
671 if self.ret_coercion.is_none() {
672 self.tcx.sess.emit_err(ReturnStmtOutsideOfFnBody { span: expr.span });
673 } else if let Some(ref e) = expr_opt {
674 if self.ret_coercion_span.borrow().is_none() {
675 *self.ret_coercion_span.borrow_mut() = Some(e.span);
676 }
677 self.check_return_expr(e);
678 } else {
679 let mut coercion = self.ret_coercion.as_ref().unwrap().borrow_mut();
680 if self.ret_coercion_span.borrow().is_none() {
681 *self.ret_coercion_span.borrow_mut() = Some(expr.span);
682 }
683 let cause = self.cause(expr.span, ObligationCauseCode::ReturnNoExpression);
684 if let Some((fn_decl, _)) = self.get_fn_decl(expr.hir_id) {
685 coercion.coerce_forced_unit(
686 self,
687 &cause,
688 &mut |db| {
689 let span = fn_decl.output.span();
690 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
691 db.span_label(
692 span,
693 format!("expected `{}` because of this return type", snippet),
694 );
695 }
696 },
697 true,
698 );
699 } else {
700 coercion.coerce_forced_unit(self, &cause, &mut |_| (), true);
701 }
702 }
703 self.tcx.types.never
704 }
705
706 pub(super) fn check_return_expr(&self, return_expr: &'tcx hir::Expr<'tcx>) {
707 let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
708 span_bug!(return_expr.span, "check_return_expr called outside fn body")
709 });
710
711 let ret_ty = ret_coercion.borrow().expected_ty();
712 let return_expr_ty = self.check_expr_with_hint(return_expr, ret_ty.clone());
713 ret_coercion.borrow_mut().coerce(
714 self,
715 &self.cause(return_expr.span, ObligationCauseCode::ReturnValue(return_expr.hir_id)),
716 return_expr,
717 return_expr_ty,
718 );
719 }
720
721 pub(crate) fn check_lhs_assignable(
722 &self,
723 lhs: &'tcx hir::Expr<'tcx>,
724 err_code: &'static str,
725 expr_span: &Span,
726 ) {
727 if lhs.is_syntactic_place_expr() {
728 return;
729 }
730
731 // FIXME: Make this use SessionDiagnostic once error codes can be dynamically set.
732 let mut err = self.tcx.sess.struct_span_err_with_code(
733 *expr_span,
734 "invalid left-hand side of assignment",
735 DiagnosticId::Error(err_code.into()),
736 );
737 err.span_label(lhs.span, "cannot assign to this expression");
738 err.emit();
739 }
740
741 /// Type check assignment expression `expr` of form `lhs = rhs`.
742 /// The expected type is `()` and is passsed to the function for the purposes of diagnostics.
743 fn check_expr_assign(
744 &self,
745 expr: &'tcx hir::Expr<'tcx>,
746 expected: Expectation<'tcx>,
747 lhs: &'tcx hir::Expr<'tcx>,
748 rhs: &'tcx hir::Expr<'tcx>,
749 span: &Span,
750 ) -> Ty<'tcx> {
751 let expected_ty = expected.coercion_target_type(self, expr.span);
752 if expected_ty == self.tcx.types.bool {
753 // The expected type is `bool` but this will result in `()` so we can reasonably
754 // say that the user intended to write `lhs == rhs` instead of `lhs = rhs`.
755 // The likely cause of this is `if foo = bar { .. }`.
756 let actual_ty = self.tcx.mk_unit();
757 let mut err = self.demand_suptype_diag(expr.span, expected_ty, actual_ty).unwrap();
758 let lhs_ty = self.check_expr(&lhs);
759 let rhs_ty = self.check_expr(&rhs);
760 let (applicability, eq) = if self.can_coerce(rhs_ty, lhs_ty) {
761 (Applicability::MachineApplicable, true)
762 } else {
763 (Applicability::MaybeIncorrect, false)
764 };
765 if !lhs.is_syntactic_place_expr() {
766 // Do not suggest `if let x = y` as `==` is way more likely to be the intention.
767 if let hir::Node::Expr(hir::Expr {
768 kind:
769 ExprKind::Match(
770 _,
771 _,
772 hir::MatchSource::IfDesugar { .. } | hir::MatchSource::WhileDesugar,
773 ),
774 ..
775 }) = self.tcx.hir().get(
776 self.tcx.hir().get_parent_node(self.tcx.hir().get_parent_node(expr.hir_id)),
777 ) {
778 // Likely `if let` intended.
779 err.span_suggestion_verbose(
780 expr.span.shrink_to_lo(),
781 "you might have meant to use pattern matching",
782 "let ".to_string(),
783 applicability,
784 );
785 }
786 }
787 if eq {
788 err.span_suggestion_verbose(
789 *span,
790 "you might have meant to compare for equality",
791 "==".to_string(),
792 applicability,
793 );
794 }
795
796 if self.sess().if_let_suggestions.borrow().get(&expr.span).is_some() {
797 // We already emitted an `if let` suggestion due to an identifier not found.
798 err.delay_as_bug();
799 } else {
800 err.emit();
801 }
802 return self.tcx.ty_error();
803 }
804
805 self.check_lhs_assignable(lhs, "E0070", span);
806
807 let lhs_ty = self.check_expr_with_needs(&lhs, Needs::MutPlace);
808 let rhs_ty = self.check_expr_coercable_to_type(&rhs, lhs_ty, Some(lhs));
809
810 self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized);
811
812 if lhs_ty.references_error() || rhs_ty.references_error() {
813 self.tcx.ty_error()
814 } else {
815 self.tcx.mk_unit()
816 }
817 }
818
819 fn check_expr_loop(
820 &self,
821 body: &'tcx hir::Block<'tcx>,
822 source: hir::LoopSource,
823 expected: Expectation<'tcx>,
824 expr: &'tcx hir::Expr<'tcx>,
825 ) -> Ty<'tcx> {
826 let coerce = match source {
827 // you can only use break with a value from a normal `loop { }`
828 hir::LoopSource::Loop => {
829 let coerce_to = expected.coercion_target_type(self, body.span);
830 Some(CoerceMany::new(coerce_to))
831 }
832
833 hir::LoopSource::While | hir::LoopSource::WhileLet | hir::LoopSource::ForLoop => None,
834 };
835
836 let ctxt = BreakableCtxt {
837 coerce,
838 may_break: false, // Will get updated if/when we find a `break`.
839 };
840
841 let (ctxt, ()) = self.with_breakable_ctxt(expr.hir_id, ctxt, || {
842 self.check_block_no_value(&body);
843 });
844
845 if ctxt.may_break {
846 // No way to know whether it's diverging because
847 // of a `break` or an outer `break` or `return`.
848 self.diverges.set(Diverges::Maybe);
849 }
850
851 // If we permit break with a value, then result type is
852 // the LUB of the breaks (possibly ! if none); else, it
853 // is nil. This makes sense because infinite loops
854 // (which would have type !) are only possible iff we
855 // permit break with a value [1].
856 if ctxt.coerce.is_none() && !ctxt.may_break {
857 // [1]
858 self.tcx.sess.delay_span_bug(body.span, "no coercion, but loop may not break");
859 }
860 ctxt.coerce.map(|c| c.complete(self)).unwrap_or_else(|| self.tcx.mk_unit())
861 }
862
863 /// Checks a method call.
864 fn check_method_call(
865 &self,
866 expr: &'tcx hir::Expr<'tcx>,
867 segment: &hir::PathSegment<'_>,
868 span: Span,
869 args: &'tcx [hir::Expr<'tcx>],
870 expected: Expectation<'tcx>,
871 ) -> Ty<'tcx> {
872 let rcvr = &args[0];
873 let rcvr_t = self.check_expr(&rcvr);
874 // no need to check for bot/err -- callee does that
875 let rcvr_t = self.structurally_resolved_type(args[0].span, rcvr_t);
876
877 let method = match self.lookup_method(rcvr_t, segment, span, expr, rcvr) {
878 Ok(method) => {
879 // We could add a "consider `foo::<params>`" suggestion here, but I wasn't able to
880 // trigger this codepath causing `structuraly_resolved_type` to emit an error.
881
882 self.write_method_call(expr.hir_id, method);
883 Ok(method)
884 }
885 Err(error) => {
886 if segment.ident.name != kw::Invalid {
887 self.report_extended_method_error(segment, span, args, rcvr_t, error);
888 }
889 Err(())
890 }
891 };
892
893 // Call the generic checker.
894 self.check_method_argument_types(
895 span,
896 expr,
897 method,
898 &args[1..],
899 DontTupleArguments,
900 expected,
901 )
902 }
903
904 fn report_extended_method_error(
905 &self,
906 segment: &hir::PathSegment<'_>,
907 span: Span,
908 args: &'tcx [hir::Expr<'tcx>],
909 rcvr_t: Ty<'tcx>,
910 error: MethodError<'tcx>,
911 ) {
912 let rcvr = &args[0];
913 let try_alt_rcvr = |err: &mut DiagnosticBuilder<'_>, new_rcvr_t| {
914 if let Some(new_rcvr_t) = new_rcvr_t {
915 if let Ok(pick) = self.lookup_probe(
916 span,
917 segment.ident,
918 new_rcvr_t,
919 rcvr,
920 probe::ProbeScope::AllTraits,
921 ) {
922 debug!("try_alt_rcvr: pick candidate {:?}", pick);
923 // Make sure the method is defined for the *actual* receiver:
924 // we don't want to treat `Box<Self>` as a receiver if
925 // it only works because of an autoderef to `&self`
926 if pick.autoderefs == 0 {
927 err.span_label(
928 pick.item.ident.span,
929 &format!("the method is available for `{}` here", new_rcvr_t),
930 );
931 }
932 }
933 }
934 };
935
936 if let Some(mut err) = self.report_method_error(
937 span,
938 rcvr_t,
939 segment.ident,
940 SelfSource::MethodCall(rcvr),
941 error,
942 Some(args),
943 ) {
944 if let ty::Adt(..) = rcvr_t.kind() {
945 // Try alternative arbitrary self types that could fulfill this call.
946 // FIXME: probe for all types that *could* be arbitrary self-types, not
947 // just this list.
948 try_alt_rcvr(&mut err, self.tcx.mk_lang_item(rcvr_t, LangItem::OwnedBox));
949 try_alt_rcvr(&mut err, self.tcx.mk_lang_item(rcvr_t, LangItem::Pin));
950 try_alt_rcvr(&mut err, self.tcx.mk_diagnostic_item(rcvr_t, sym::Arc));
951 try_alt_rcvr(&mut err, self.tcx.mk_diagnostic_item(rcvr_t, sym::Rc));
952 }
953 err.emit();
954 }
955 }
956
957 fn check_expr_cast(
958 &self,
959 e: &'tcx hir::Expr<'tcx>,
960 t: &'tcx hir::Ty<'tcx>,
961 expr: &'tcx hir::Expr<'tcx>,
962 ) -> Ty<'tcx> {
963 // Find the type of `e`. Supply hints based on the type we are casting to,
964 // if appropriate.
965 let t_cast = self.to_ty_saving_user_provided_ty(t);
966 let t_cast = self.resolve_vars_if_possible(&t_cast);
967 let t_expr = self.check_expr_with_expectation(e, ExpectCastableToType(t_cast));
968 let t_cast = self.resolve_vars_if_possible(&t_cast);
969
970 // Eagerly check for some obvious errors.
971 if t_expr.references_error() || t_cast.references_error() {
972 self.tcx.ty_error()
973 } else {
974 // Defer other checks until we're done type checking.
975 let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
976 match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
977 Ok(cast_check) => {
978 deferred_cast_checks.push(cast_check);
979 t_cast
980 }
981 Err(ErrorReported) => self.tcx.ty_error(),
982 }
983 }
984 }
985
986 fn check_expr_array(
987 &self,
988 args: &'tcx [hir::Expr<'tcx>],
989 expected: Expectation<'tcx>,
990 expr: &'tcx hir::Expr<'tcx>,
991 ) -> Ty<'tcx> {
992 let element_ty = if !args.is_empty() {
993 let coerce_to = expected
994 .to_option(self)
995 .and_then(|uty| match *uty.kind() {
996 ty::Array(ty, _) | ty::Slice(ty) => Some(ty),
997 _ => None,
998 })
999 .unwrap_or_else(|| {
1000 self.next_ty_var(TypeVariableOrigin {
1001 kind: TypeVariableOriginKind::TypeInference,
1002 span: expr.span,
1003 })
1004 });
1005 let mut coerce = CoerceMany::with_coercion_sites(coerce_to, args);
1006 assert_eq!(self.diverges.get(), Diverges::Maybe);
1007 for e in args {
1008 let e_ty = self.check_expr_with_hint(e, coerce_to);
1009 let cause = self.misc(e.span);
1010 coerce.coerce(self, &cause, e, e_ty);
1011 }
1012 coerce.complete(self)
1013 } else {
1014 self.next_ty_var(TypeVariableOrigin {
1015 kind: TypeVariableOriginKind::TypeInference,
1016 span: expr.span,
1017 })
1018 };
1019 self.tcx.mk_array(element_ty, args.len() as u64)
1020 }
1021
1022 fn check_expr_repeat(
1023 &self,
1024 element: &'tcx hir::Expr<'tcx>,
1025 count: &'tcx hir::AnonConst,
1026 expected: Expectation<'tcx>,
1027 _expr: &'tcx hir::Expr<'tcx>,
1028 ) -> Ty<'tcx> {
1029 let tcx = self.tcx;
1030 let count = self.to_const(count);
1031
1032 let uty = match expected {
1033 ExpectHasType(uty) => match *uty.kind() {
1034 ty::Array(ty, _) | ty::Slice(ty) => Some(ty),
1035 _ => None,
1036 },
1037 _ => None,
1038 };
1039
1040 let (element_ty, t) = match uty {
1041 Some(uty) => {
1042 self.check_expr_coercable_to_type(&element, uty, None);
1043 (uty, uty)
1044 }
1045 None => {
1046 let ty = self.next_ty_var(TypeVariableOrigin {
1047 kind: TypeVariableOriginKind::MiscVariable,
1048 span: element.span,
1049 });
1050 let element_ty = self.check_expr_has_type_or_error(&element, ty, |_| {});
1051 (element_ty, ty)
1052 }
1053 };
1054
1055 if element_ty.references_error() {
1056 return tcx.ty_error();
1057 }
1058
1059 tcx.mk_ty(ty::Array(t, count))
1060 }
1061
1062 fn check_expr_tuple(
1063 &self,
1064 elts: &'tcx [hir::Expr<'tcx>],
1065 expected: Expectation<'tcx>,
1066 expr: &'tcx hir::Expr<'tcx>,
1067 ) -> Ty<'tcx> {
1068 let flds = expected.only_has_type(self).and_then(|ty| {
1069 let ty = self.resolve_vars_with_obligations(ty);
1070 match ty.kind() {
1071 ty::Tuple(ref flds) => Some(&flds[..]),
1072 _ => None,
1073 }
1074 });
1075
1076 let elt_ts_iter = elts.iter().enumerate().map(|(i, e)| match flds {
1077 Some(ref fs) if i < fs.len() => {
1078 let ety = fs[i].expect_ty();
1079 self.check_expr_coercable_to_type(&e, ety, None);
1080 ety
1081 }
1082 _ => self.check_expr_with_expectation(&e, NoExpectation),
1083 });
1084 let tuple = self.tcx.mk_tup(elt_ts_iter);
1085 if tuple.references_error() {
1086 self.tcx.ty_error()
1087 } else {
1088 self.require_type_is_sized(tuple, expr.span, traits::TupleInitializerSized);
1089 tuple
1090 }
1091 }
1092
1093 fn check_expr_struct(
1094 &self,
1095 expr: &hir::Expr<'_>,
1096 expected: Expectation<'tcx>,
1097 qpath: &QPath<'_>,
1098 fields: &'tcx [hir::Field<'tcx>],
1099 base_expr: &'tcx Option<&'tcx hir::Expr<'tcx>>,
1100 ) -> Ty<'tcx> {
1101 // Find the relevant variant
1102 let (variant, adt_ty) = if let Some(variant_ty) = self.check_struct_path(qpath, expr.hir_id)
1103 {
1104 variant_ty
1105 } else {
1106 self.check_struct_fields_on_error(fields, base_expr);
1107 return self.tcx.ty_error();
1108 };
1109
1110 // Prohibit struct expressions when non-exhaustive flag is set.
1111 let adt = adt_ty.ty_adt_def().expect("`check_struct_path` returned non-ADT type");
1112 if !adt.did.is_local() && variant.is_field_list_non_exhaustive() {
1113 self.tcx
1114 .sess
1115 .emit_err(StructExprNonExhaustive { span: expr.span, what: adt.variant_descr() });
1116 }
1117
1118 let error_happened = self.check_expr_struct_fields(
1119 adt_ty,
1120 expected,
1121 expr.hir_id,
1122 qpath.span(),
1123 variant,
1124 fields,
1125 base_expr.is_none(),
1126 );
1127 if let &Some(ref base_expr) = base_expr {
1128 // If check_expr_struct_fields hit an error, do not attempt to populate
1129 // the fields with the base_expr. This could cause us to hit errors later
1130 // when certain fields are assumed to exist that in fact do not.
1131 if !error_happened {
1132 self.check_expr_has_type_or_error(base_expr, adt_ty, |_| {});
1133 match adt_ty.kind() {
1134 ty::Adt(adt, substs) if adt.is_struct() => {
1135 let fru_field_types = adt
1136 .non_enum_variant()
1137 .fields
1138 .iter()
1139 .map(|f| {
1140 self.normalize_associated_types_in(
1141 expr.span,
1142 &f.ty(self.tcx, substs),
1143 )
1144 })
1145 .collect();
1146
1147 self.typeck_results
1148 .borrow_mut()
1149 .fru_field_types_mut()
1150 .insert(expr.hir_id, fru_field_types);
1151 }
1152 _ => {
1153 self.tcx
1154 .sess
1155 .emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span });
1156 }
1157 }
1158 }
1159 }
1160 self.require_type_is_sized(adt_ty, expr.span, traits::StructInitializerSized);
1161 adt_ty
1162 }
1163
1164 fn check_expr_struct_fields(
1165 &self,
1166 adt_ty: Ty<'tcx>,
1167 expected: Expectation<'tcx>,
1168 expr_id: hir::HirId,
1169 span: Span,
1170 variant: &'tcx ty::VariantDef,
1171 ast_fields: &'tcx [hir::Field<'tcx>],
1172 check_completeness: bool,
1173 ) -> bool {
1174 let tcx = self.tcx;
1175
1176 let adt_ty_hint = self
1177 .expected_inputs_for_expected_output(span, expected, adt_ty, &[adt_ty])
1178 .get(0)
1179 .cloned()
1180 .unwrap_or(adt_ty);
1181 // re-link the regions that EIfEO can erase.
1182 self.demand_eqtype(span, adt_ty_hint, adt_ty);
1183
1184 let (substs, adt_kind, kind_name) = match &adt_ty.kind() {
1185 &ty::Adt(adt, substs) => (substs, adt.adt_kind(), adt.variant_descr()),
1186 _ => span_bug!(span, "non-ADT passed to check_expr_struct_fields"),
1187 };
1188
1189 let mut remaining_fields = variant
1190 .fields
1191 .iter()
1192 .enumerate()
1193 .map(|(i, field)| (field.ident.normalize_to_macros_2_0(), (i, field)))
1194 .collect::<FxHashMap<_, _>>();
1195
1196 let mut seen_fields = FxHashMap::default();
1197
1198 let mut error_happened = false;
1199
1200 // Type-check each field.
1201 for field in ast_fields {
1202 let ident = tcx.adjust_ident(field.ident, variant.def_id);
1203 let field_type = if let Some((i, v_field)) = remaining_fields.remove(&ident) {
1204 seen_fields.insert(ident, field.span);
1205 self.write_field_index(field.hir_id, i);
1206
1207 // We don't look at stability attributes on
1208 // struct-like enums (yet...), but it's definitely not
1209 // a bug to have constructed one.
1210 if adt_kind != AdtKind::Enum {
1211 tcx.check_stability(v_field.did, Some(expr_id), field.span);
1212 }
1213
1214 self.field_ty(field.span, v_field, substs)
1215 } else {
1216 error_happened = true;
1217 if let Some(prev_span) = seen_fields.get(&ident) {
1218 tcx.sess.emit_err(FieldMultiplySpecifiedInInitializer {
1219 span: field.ident.span,
1220 prev_span: *prev_span,
1221 ident,
1222 });
1223 } else {
1224 self.report_unknown_field(adt_ty, variant, field, ast_fields, kind_name, span);
1225 }
1226
1227 tcx.ty_error()
1228 };
1229
1230 // Make sure to give a type to the field even if there's
1231 // an error, so we can continue type-checking.
1232 self.check_expr_coercable_to_type(&field.expr, field_type, None);
1233 }
1234
1235 // Make sure the programmer specified correct number of fields.
1236 if kind_name == "union" {
1237 if ast_fields.len() != 1 {
1238 tcx.sess.span_err(span, "union expressions should have exactly one field");
1239 }
1240 } else if check_completeness && !error_happened && !remaining_fields.is_empty() {
1241 let no_accessible_remaining_fields = remaining_fields
1242 .iter()
1243 .find(|(_, (_, field))| {
1244 field.vis.is_accessible_from(tcx.parent_module(expr_id).to_def_id(), tcx)
1245 })
1246 .is_none();
1247
1248 if no_accessible_remaining_fields {
1249 self.report_no_accessible_fields(adt_ty, span);
1250 } else {
1251 self.report_missing_field(adt_ty, span, remaining_fields);
1252 }
1253 }
1254
1255 error_happened
1256 }
1257
1258 fn check_struct_fields_on_error(
1259 &self,
1260 fields: &'tcx [hir::Field<'tcx>],
1261 base_expr: &'tcx Option<&'tcx hir::Expr<'tcx>>,
1262 ) {
1263 for field in fields {
1264 self.check_expr(&field.expr);
1265 }
1266 if let Some(ref base) = *base_expr {
1267 self.check_expr(&base);
1268 }
1269 }
1270
1271 /// Report an error for a struct field expression when there are fields which aren't provided.
1272 ///
1273 /// ```text
1274 /// error: missing field `you_can_use_this_field` in initializer of `foo::Foo`
1275 /// --> src/main.rs:8:5
1276 /// |
1277 /// 8 | foo::Foo {};
1278 /// | ^^^^^^^^ missing `you_can_use_this_field`
1279 ///
1280 /// error: aborting due to previous error
1281 /// ```
1282 fn report_missing_field(
1283 &self,
1284 adt_ty: Ty<'tcx>,
1285 span: Span,
1286 remaining_fields: FxHashMap<Ident, (usize, &ty::FieldDef)>,
1287 ) {
1288 let tcx = self.tcx;
1289 let len = remaining_fields.len();
1290
1291 let mut displayable_field_names =
1292 remaining_fields.keys().map(|ident| ident.as_str()).collect::<Vec<_>>();
1293
1294 displayable_field_names.sort();
1295
1296 let truncated_fields_error = if len <= 3 {
1297 String::new()
1298 } else {
1299 format!(" and {} other field{}", (len - 3), if len - 3 == 1 { "" } else { "s" })
1300 };
1301
1302 let remaining_fields_names = displayable_field_names
1303 .iter()
1304 .take(3)
1305 .map(|n| format!("`{}`", n))
1306 .collect::<Vec<_>>()
1307 .join(", ");
1308
1309 struct_span_err!(
1310 tcx.sess,
1311 span,
1312 E0063,
1313 "missing field{} {}{} in initializer of `{}`",
1314 pluralize!(remaining_fields.len()),
1315 remaining_fields_names,
1316 truncated_fields_error,
1317 adt_ty
1318 )
1319 .span_label(span, format!("missing {}{}", remaining_fields_names, truncated_fields_error))
1320 .emit();
1321 }
1322
1323 /// Report an error for a struct field expression when there are no visible fields.
1324 ///
1325 /// ```text
1326 /// error: cannot construct `Foo` with struct literal syntax due to inaccessible fields
1327 /// --> src/main.rs:8:5
1328 /// |
1329 /// 8 | foo::Foo {};
1330 /// | ^^^^^^^^
1331 ///
1332 /// error: aborting due to previous error
1333 /// ```
1334 fn report_no_accessible_fields(&self, adt_ty: Ty<'tcx>, span: Span) {
1335 self.tcx.sess.span_err(
1336 span,
1337 &format!(
1338 "cannot construct `{}` with struct literal syntax due to inaccessible fields",
1339 adt_ty,
1340 ),
1341 );
1342 }
1343
1344 fn report_unknown_field(
1345 &self,
1346 ty: Ty<'tcx>,
1347 variant: &'tcx ty::VariantDef,
1348 field: &hir::Field<'_>,
1349 skip_fields: &[hir::Field<'_>],
1350 kind_name: &str,
1351 ty_span: Span,
1352 ) {
1353 if variant.is_recovered() {
1354 self.set_tainted_by_errors();
1355 return;
1356 }
1357 let mut err = self.type_error_struct_with_diag(
1358 field.ident.span,
1359 |actual| match ty.kind() {
1360 ty::Adt(adt, ..) if adt.is_enum() => struct_span_err!(
1361 self.tcx.sess,
1362 field.ident.span,
1363 E0559,
1364 "{} `{}::{}` has no field named `{}`",
1365 kind_name,
1366 actual,
1367 variant.ident,
1368 field.ident
1369 ),
1370 _ => struct_span_err!(
1371 self.tcx.sess,
1372 field.ident.span,
1373 E0560,
1374 "{} `{}` has no field named `{}`",
1375 kind_name,
1376 actual,
1377 field.ident
1378 ),
1379 },
1380 ty,
1381 );
1382 match variant.ctor_kind {
1383 CtorKind::Fn => {
1384 err.span_label(variant.ident.span, format!("`{adt}` defined here", adt = ty));
1385 err.span_label(field.ident.span, "field does not exist");
1386 err.span_label(
1387 ty_span,
1388 format!(
1389 "`{adt}` is a tuple {kind_name}, \
1390 use the appropriate syntax: `{adt}(/* fields */)`",
1391 adt = ty,
1392 kind_name = kind_name
1393 ),
1394 );
1395 }
1396 _ => {
1397 // prevent all specified fields from being suggested
1398 let skip_fields = skip_fields.iter().map(|ref x| x.ident.name);
1399 if let Some(field_name) =
1400 Self::suggest_field_name(variant, field.ident.name, skip_fields.collect())
1401 {
1402 err.span_suggestion(
1403 field.ident.span,
1404 "a field with a similar name exists",
1405 field_name.to_string(),
1406 Applicability::MaybeIncorrect,
1407 );
1408 } else {
1409 match ty.kind() {
1410 ty::Adt(adt, ..) => {
1411 if adt.is_enum() {
1412 err.span_label(
1413 field.ident.span,
1414 format!("`{}::{}` does not have this field", ty, variant.ident),
1415 );
1416 } else {
1417 err.span_label(
1418 field.ident.span,
1419 format!("`{}` does not have this field", ty),
1420 );
1421 }
1422 let available_field_names = self.available_field_names(variant);
1423 if !available_field_names.is_empty() {
1424 err.note(&format!(
1425 "available fields are: {}",
1426 self.name_series_display(available_field_names)
1427 ));
1428 }
1429 }
1430 _ => bug!("non-ADT passed to report_unknown_field"),
1431 }
1432 };
1433 }
1434 }
1435 err.emit();
1436 }
1437
1438 // Return an hint about the closest match in field names
1439 fn suggest_field_name(
1440 variant: &'tcx ty::VariantDef,
1441 field: Symbol,
1442 skip: Vec<Symbol>,
1443 ) -> Option<Symbol> {
1444 let names = variant.fields.iter().filter_map(|field| {
1445 // ignore already set fields and private fields from non-local crates
1446 if skip.iter().any(|&x| x == field.ident.name)
1447 || (!variant.def_id.is_local() && field.vis != Visibility::Public)
1448 {
1449 None
1450 } else {
1451 Some(&field.ident.name)
1452 }
1453 });
1454
1455 find_best_match_for_name(names, field, None)
1456 }
1457
1458 fn available_field_names(&self, variant: &'tcx ty::VariantDef) -> Vec<Symbol> {
1459 variant
1460 .fields
1461 .iter()
1462 .filter(|field| {
1463 let def_scope = self
1464 .tcx
1465 .adjust_ident_and_get_scope(field.ident, variant.def_id, self.body_id)
1466 .1;
1467 field.vis.is_accessible_from(def_scope, self.tcx)
1468 })
1469 .map(|field| field.ident.name)
1470 .collect()
1471 }
1472
1473 fn name_series_display(&self, names: Vec<Symbol>) -> String {
1474 // dynamic limit, to never omit just one field
1475 let limit = if names.len() == 6 { 6 } else { 5 };
1476 let mut display =
1477 names.iter().take(limit).map(|n| format!("`{}`", n)).collect::<Vec<_>>().join(", ");
1478 if names.len() > limit {
1479 display = format!("{} ... and {} others", display, names.len() - limit);
1480 }
1481 display
1482 }
1483
1484 // Check field access expressions
1485 fn check_field(
1486 &self,
1487 expr: &'tcx hir::Expr<'tcx>,
1488 base: &'tcx hir::Expr<'tcx>,
1489 field: Ident,
1490 ) -> Ty<'tcx> {
1491 let expr_t = self.check_expr(base);
1492 let expr_t = self.structurally_resolved_type(base.span, expr_t);
1493 let mut private_candidate = None;
1494 let mut autoderef = self.autoderef(expr.span, expr_t);
1495 while let Some((base_t, _)) = autoderef.next() {
1496 match base_t.kind() {
1497 ty::Adt(base_def, substs) if !base_def.is_enum() => {
1498 debug!("struct named {:?}", base_t);
1499 let (ident, def_scope) =
1500 self.tcx.adjust_ident_and_get_scope(field, base_def.did, self.body_id);
1501 let fields = &base_def.non_enum_variant().fields;
1502 if let Some(index) =
1503 fields.iter().position(|f| f.ident.normalize_to_macros_2_0() == ident)
1504 {
1505 let field = &fields[index];
1506 let field_ty = self.field_ty(expr.span, field, substs);
1507 // Save the index of all fields regardless of their visibility in case
1508 // of error recovery.
1509 self.write_field_index(expr.hir_id, index);
1510 if field.vis.is_accessible_from(def_scope, self.tcx) {
1511 let adjustments = self.adjust_steps(&autoderef);
1512 self.apply_adjustments(base, adjustments);
1513 self.register_predicates(autoderef.into_obligations());
1514
1515 self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span);
1516 return field_ty;
1517 }
1518 private_candidate = Some((base_def.did, field_ty));
1519 }
1520 }
1521 ty::Tuple(ref tys) => {
1522 let fstr = field.as_str();
1523 if let Ok(index) = fstr.parse::<usize>() {
1524 if fstr == index.to_string() {
1525 if let Some(field_ty) = tys.get(index) {
1526 let adjustments = self.adjust_steps(&autoderef);
1527 self.apply_adjustments(base, adjustments);
1528 self.register_predicates(autoderef.into_obligations());
1529
1530 self.write_field_index(expr.hir_id, index);
1531 return field_ty.expect_ty();
1532 }
1533 }
1534 }
1535 }
1536 _ => {}
1537 }
1538 }
1539 self.structurally_resolved_type(autoderef.span(), autoderef.final_ty(false));
1540
1541 if let Some((did, field_ty)) = private_candidate {
1542 self.ban_private_field_access(expr, expr_t, field, did);
1543 return field_ty;
1544 }
1545
1546 if field.name == kw::Invalid {
1547 } else if self.method_exists(field, expr_t, expr.hir_id, true) {
1548 self.ban_take_value_of_method(expr, expr_t, field);
1549 } else if !expr_t.is_primitive_ty() {
1550 self.ban_nonexisting_field(field, base, expr, expr_t);
1551 } else {
1552 type_error_struct!(
1553 self.tcx().sess,
1554 field.span,
1555 expr_t,
1556 E0610,
1557 "`{}` is a primitive type and therefore doesn't have fields",
1558 expr_t
1559 )
1560 .emit();
1561 }
1562
1563 self.tcx().ty_error()
1564 }
1565
1566 fn suggest_await_on_field_access(
1567 &self,
1568 err: &mut DiagnosticBuilder<'_>,
1569 field_ident: Ident,
1570 base: &'tcx hir::Expr<'tcx>,
1571 ty: Ty<'tcx>,
1572 ) {
1573 let output_ty = match self.infcx.get_impl_future_output_ty(ty) {
1574 Some(output_ty) => self.resolve_vars_if_possible(&output_ty),
1575 _ => return,
1576 };
1577 let mut add_label = true;
1578 if let ty::Adt(def, _) = output_ty.kind() {
1579 // no field access on enum type
1580 if !def.is_enum() {
1581 if def.non_enum_variant().fields.iter().any(|field| field.ident == field_ident) {
1582 add_label = false;
1583 err.span_label(
1584 field_ident.span,
1585 "field not available in `impl Future`, but it is available in its `Output`",
1586 );
1587 err.span_suggestion_verbose(
1588 base.span.shrink_to_hi(),
1589 "consider `await`ing on the `Future` and access the field of its `Output`",
1590 ".await".to_string(),
1591 Applicability::MaybeIncorrect,
1592 );
1593 }
1594 }
1595 }
1596 if add_label {
1597 err.span_label(field_ident.span, &format!("field not found in `{}`", ty));
1598 }
1599 }
1600
1601 fn ban_nonexisting_field(
1602 &self,
1603 field: Ident,
1604 base: &'tcx hir::Expr<'tcx>,
1605 expr: &'tcx hir::Expr<'tcx>,
1606 expr_t: Ty<'tcx>,
1607 ) {
1608 debug!(
1609 "ban_nonexisting_field: field={:?}, base={:?}, expr={:?}, expr_ty={:?}",
1610 field, base, expr, expr_t
1611 );
1612 let mut err = self.no_such_field_err(field.span, field, expr_t);
1613
1614 match *expr_t.peel_refs().kind() {
1615 ty::Array(_, len) => {
1616 self.maybe_suggest_array_indexing(&mut err, expr, base, field, len);
1617 }
1618 ty::RawPtr(..) => {
1619 self.suggest_first_deref_field(&mut err, expr, base, field);
1620 }
1621 ty::Adt(def, _) if !def.is_enum() => {
1622 self.suggest_fields_on_recordish(&mut err, def, field);
1623 }
1624 ty::Param(param_ty) => {
1625 self.point_at_param_definition(&mut err, param_ty);
1626 }
1627 ty::Opaque(_, _) => {
1628 self.suggest_await_on_field_access(&mut err, field, base, expr_t.peel_refs());
1629 }
1630 _ => {}
1631 }
1632
1633 if field.name == kw::Await {
1634 // We know by construction that `<expr>.await` is either on Rust 2015
1635 // or results in `ExprKind::Await`. Suggest switching the edition to 2018.
1636 err.note("to `.await` a `Future`, switch to Rust 2018");
1637 err.help("set `edition = \"2018\"` in `Cargo.toml`");
1638 err.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
1639 }
1640
1641 err.emit();
1642 }
1643
1644 fn ban_private_field_access(
1645 &self,
1646 expr: &hir::Expr<'_>,
1647 expr_t: Ty<'tcx>,
1648 field: Ident,
1649 base_did: DefId,
1650 ) {
1651 let struct_path = self.tcx().def_path_str(base_did);
1652 let kind_name = self.tcx().def_kind(base_did).descr(base_did);
1653 let mut err = struct_span_err!(
1654 self.tcx().sess,
1655 field.span,
1656 E0616,
1657 "field `{}` of {} `{}` is private",
1658 field,
1659 kind_name,
1660 struct_path
1661 );
1662 err.span_label(field.span, "private field");
1663 // Also check if an accessible method exists, which is often what is meant.
1664 if self.method_exists(field, expr_t, expr.hir_id, false) && !self.expr_in_place(expr.hir_id)
1665 {
1666 self.suggest_method_call(
1667 &mut err,
1668 &format!("a method `{}` also exists, call it with parentheses", field),
1669 field,
1670 expr_t,
1671 expr,
1672 );
1673 }
1674 err.emit();
1675 }
1676
1677 fn ban_take_value_of_method(&self, expr: &hir::Expr<'_>, expr_t: Ty<'tcx>, field: Ident) {
1678 let mut err = type_error_struct!(
1679 self.tcx().sess,
1680 field.span,
1681 expr_t,
1682 E0615,
1683 "attempted to take value of method `{}` on type `{}`",
1684 field,
1685 expr_t
1686 );
1687 err.span_label(field.span, "method, not a field");
1688 if !self.expr_in_place(expr.hir_id) {
1689 self.suggest_method_call(
1690 &mut err,
1691 "use parentheses to call the method",
1692 field,
1693 expr_t,
1694 expr,
1695 );
1696 } else {
1697 err.help("methods are immutable and cannot be assigned to");
1698 }
1699
1700 err.emit();
1701 }
1702
1703 fn point_at_param_definition(&self, err: &mut DiagnosticBuilder<'_>, param: ty::ParamTy) {
1704 let generics = self.tcx.generics_of(self.body_id.owner.to_def_id());
1705 let generic_param = generics.type_param(&param, self.tcx);
1706 if let ty::GenericParamDefKind::Type { synthetic: Some(..), .. } = generic_param.kind {
1707 return;
1708 }
1709 let param_def_id = generic_param.def_id;
1710 let param_hir_id = match param_def_id.as_local() {
1711 Some(x) => self.tcx.hir().local_def_id_to_hir_id(x),
1712 None => return,
1713 };
1714 let param_span = self.tcx.hir().span(param_hir_id);
1715 let param_name = self.tcx.hir().ty_param_name(param_hir_id);
1716
1717 err.span_label(param_span, &format!("type parameter '{}' declared here", param_name));
1718 }
1719
1720 fn suggest_fields_on_recordish(
1721 &self,
1722 err: &mut DiagnosticBuilder<'_>,
1723 def: &'tcx ty::AdtDef,
1724 field: Ident,
1725 ) {
1726 if let Some(suggested_field_name) =
1727 Self::suggest_field_name(def.non_enum_variant(), field.name, vec![])
1728 {
1729 err.span_suggestion(
1730 field.span,
1731 "a field with a similar name exists",
1732 suggested_field_name.to_string(),
1733 Applicability::MaybeIncorrect,
1734 );
1735 } else {
1736 err.span_label(field.span, "unknown field");
1737 let struct_variant_def = def.non_enum_variant();
1738 let field_names = self.available_field_names(struct_variant_def);
1739 if !field_names.is_empty() {
1740 err.note(&format!(
1741 "available fields are: {}",
1742 self.name_series_display(field_names),
1743 ));
1744 }
1745 }
1746 }
1747
1748 fn maybe_suggest_array_indexing(
1749 &self,
1750 err: &mut DiagnosticBuilder<'_>,
1751 expr: &hir::Expr<'_>,
1752 base: &hir::Expr<'_>,
1753 field: Ident,
1754 len: &ty::Const<'tcx>,
1755 ) {
1756 if let (Some(len), Ok(user_index)) =
1757 (len.try_eval_usize(self.tcx, self.param_env), field.as_str().parse::<u64>())
1758 {
1759 if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
1760 let help = "instead of using tuple indexing, use array indexing";
1761 let suggestion = format!("{}[{}]", base, field);
1762 let applicability = if len < user_index {
1763 Applicability::MachineApplicable
1764 } else {
1765 Applicability::MaybeIncorrect
1766 };
1767 err.span_suggestion(expr.span, help, suggestion, applicability);
1768 }
1769 }
1770 }
1771
1772 fn suggest_first_deref_field(
1773 &self,
1774 err: &mut DiagnosticBuilder<'_>,
1775 expr: &hir::Expr<'_>,
1776 base: &hir::Expr<'_>,
1777 field: Ident,
1778 ) {
1779 if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
1780 let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
1781 let suggestion = format!("(*{}).{}", base, field);
1782 err.span_suggestion(expr.span, &msg, suggestion, Applicability::MaybeIncorrect);
1783 }
1784 }
1785
1786 fn no_such_field_err<T: Display>(
1787 &self,
1788 span: Span,
1789 field: T,
1790 expr_t: &ty::TyS<'_>,
1791 ) -> DiagnosticBuilder<'_> {
1792 type_error_struct!(
1793 self.tcx().sess,
1794 span,
1795 expr_t,
1796 E0609,
1797 "no field `{}` on type `{}`",
1798 field,
1799 expr_t
1800 )
1801 }
1802
1803 fn check_expr_index(
1804 &self,
1805 base: &'tcx hir::Expr<'tcx>,
1806 idx: &'tcx hir::Expr<'tcx>,
1807 expr: &'tcx hir::Expr<'tcx>,
1808 ) -> Ty<'tcx> {
1809 let base_t = self.check_expr(&base);
1810 let idx_t = self.check_expr(&idx);
1811
1812 if base_t.references_error() {
1813 base_t
1814 } else if idx_t.references_error() {
1815 idx_t
1816 } else {
1817 let base_t = self.structurally_resolved_type(base.span, base_t);
1818 match self.lookup_indexing(expr, base, base_t, idx_t) {
1819 Some((index_ty, element_ty)) => {
1820 // two-phase not needed because index_ty is never mutable
1821 self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
1822 element_ty
1823 }
1824 None => {
1825 let mut err = type_error_struct!(
1826 self.tcx.sess,
1827 expr.span,
1828 base_t,
1829 E0608,
1830 "cannot index into a value of type `{}`",
1831 base_t
1832 );
1833 // Try to give some advice about indexing tuples.
1834 if let ty::Tuple(..) = base_t.kind() {
1835 let mut needs_note = true;
1836 // If the index is an integer, we can show the actual
1837 // fixed expression:
1838 if let ExprKind::Lit(ref lit) = idx.kind {
1839 if let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node {
1840 let snip = self.tcx.sess.source_map().span_to_snippet(base.span);
1841 if let Ok(snip) = snip {
1842 err.span_suggestion(
1843 expr.span,
1844 "to access tuple elements, use",
1845 format!("{}.{}", snip, i),
1846 Applicability::MachineApplicable,
1847 );
1848 needs_note = false;
1849 }
1850 }
1851 }
1852 if needs_note {
1853 err.help(
1854 "to access tuple elements, use tuple indexing \
1855 syntax (e.g., `tuple.0`)",
1856 );
1857 }
1858 }
1859 err.emit();
1860 self.tcx.ty_error()
1861 }
1862 }
1863 }
1864 }
1865
1866 fn check_expr_yield(
1867 &self,
1868 value: &'tcx hir::Expr<'tcx>,
1869 expr: &'tcx hir::Expr<'tcx>,
1870 src: &'tcx hir::YieldSource,
1871 ) -> Ty<'tcx> {
1872 match self.resume_yield_tys {
1873 Some((resume_ty, yield_ty)) => {
1874 self.check_expr_coercable_to_type(&value, yield_ty, None);
1875
1876 resume_ty
1877 }
1878 // Given that this `yield` expression was generated as a result of lowering a `.await`,
1879 // we know that the yield type must be `()`; however, the context won't contain this
1880 // information. Hence, we check the source of the yield expression here and check its
1881 // value's type against `()` (this check should always hold).
1882 None if src.is_await() => {
1883 self.check_expr_coercable_to_type(&value, self.tcx.mk_unit(), None);
1884 self.tcx.mk_unit()
1885 }
1886 _ => {
1887 self.tcx.sess.emit_err(YieldExprOutsideOfGenerator { span: expr.span });
1888 self.tcx.mk_unit()
1889 }
1890 }
1891 }
1892
1893 fn check_expr_asm_operand(&self, expr: &'tcx hir::Expr<'tcx>, is_input: bool) {
1894 let needs = if is_input { Needs::None } else { Needs::MutPlace };
1895 let ty = self.check_expr_with_needs(expr, needs);
1896 self.require_type_is_sized(ty, expr.span, traits::InlineAsmSized);
1897
1898 if !is_input && !expr.is_syntactic_place_expr() {
1899 let mut err = self.tcx.sess.struct_span_err(expr.span, "invalid asm output");
1900 err.span_label(expr.span, "cannot assign to this expression");
1901 err.emit();
1902 }
1903
1904 // If this is an input value, we require its type to be fully resolved
1905 // at this point. This allows us to provide helpful coercions which help
1906 // pass the type candidate list in a later pass.
1907 //
1908 // We don't require output types to be resolved at this point, which
1909 // allows them to be inferred based on how they are used later in the
1910 // function.
1911 if is_input {
1912 let ty = self.structurally_resolved_type(expr.span, &ty);
1913 match *ty.kind() {
1914 ty::FnDef(..) => {
1915 let fnptr_ty = self.tcx.mk_fn_ptr(ty.fn_sig(self.tcx));
1916 self.demand_coerce(expr, ty, fnptr_ty, None, AllowTwoPhase::No);
1917 }
1918 ty::Ref(_, base_ty, mutbl) => {
1919 let ptr_ty = self.tcx.mk_ptr(ty::TypeAndMut { ty: base_ty, mutbl });
1920 self.demand_coerce(expr, ty, ptr_ty, None, AllowTwoPhase::No);
1921 }
1922 _ => {}
1923 }
1924 }
1925 }
1926
1927 fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>) -> Ty<'tcx> {
1928 for op in asm.operands {
1929 match op {
1930 hir::InlineAsmOperand::In { expr, .. } | hir::InlineAsmOperand::Const { expr } => {
1931 self.check_expr_asm_operand(expr, true);
1932 }
1933 hir::InlineAsmOperand::Out { expr, .. } => {
1934 if let Some(expr) = expr {
1935 self.check_expr_asm_operand(expr, false);
1936 }
1937 }
1938 hir::InlineAsmOperand::InOut { expr, .. } => {
1939 self.check_expr_asm_operand(expr, false);
1940 }
1941 hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
1942 self.check_expr_asm_operand(in_expr, true);
1943 if let Some(out_expr) = out_expr {
1944 self.check_expr_asm_operand(out_expr, false);
1945 }
1946 }
1947 hir::InlineAsmOperand::Sym { expr } => {
1948 self.check_expr(expr);
1949 }
1950 }
1951 }
1952 if asm.options.contains(ast::InlineAsmOptions::NORETURN) {
1953 self.tcx.types.never
1954 } else {
1955 self.tcx.mk_unit()
1956 }
1957 }
1958 }
1959
1960 pub(super) fn ty_kind_suggestion(ty: Ty<'_>) -> Option<&'static str> {
1961 Some(match ty.kind() {
1962 ty::Bool => "true",
1963 ty::Char => "'a'",
1964 ty::Int(_) | ty::Uint(_) => "42",
1965 ty::Float(_) => "3.14159",
1966 ty::Error(_) | ty::Never => return None,
1967 _ => "value",
1968 })
1969 }