1use rustc_abi::{FIRST_VARIANT, FieldIdx};
9use rustc_ast as ast;
10use rustc_ast::util::parser::ExprPrecedence;
11use rustc_data_structures::fx::{FxHashMap, FxHashSet};
12use rustc_data_structures::stack::ensure_sufficient_stack;
13use rustc_data_structures::unord::UnordMap;
14use rustc_errors::codes::*;
15use rustc_errors::{
16 Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey, Subdiagnostic, listify, pluralize,
17 struct_span_code_err,
18};
19use rustc_hir as hir;
20use rustc_hir::def::{CtorKind, DefKind, Res};
21use rustc_hir::def_id::DefId;
22use rustc_hir::lang_items::LangItem;
23use rustc_hir::{ExprKind, HirId, QPath, find_attr, is_range_literal};
24use rustc_hir_analysis::NoVariantNamed;
25use rustc_hir_analysis::errors::NoFieldOnType;
26use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer as _;
27use rustc_infer::infer::{self, DefineOpaqueTypes, InferOk, RegionVariableOrigin};
28use rustc_infer::traits::query::NoSolution;
29use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
30use rustc_middle::ty::error::{ExpectedFound, TypeError};
31use rustc_middle::ty::{self, AdtKind, GenericArgsRef, Ty, TypeVisitableExt, Unnormalized};
32use rustc_middle::{bug, span_bug};
33use rustc_session::errors::ExprParenthesesNeeded;
34use rustc_session::parse::feature_err;
35use rustc_span::edit_distance::find_best_match_for_name;
36use rustc_span::hygiene::DesugaringKind;
37use rustc_span::{Ident, Span, Spanned, Symbol, kw, sym};
38use rustc_trait_selection::infer::InferCtxtExt;
39use rustc_trait_selection::traits::{self, ObligationCauseCode, ObligationCtxt};
40use tracing::{debug, instrument, trace};
41
42use crate::Expectation::{self, ExpectCastableToType, ExpectHasType, NoExpectation};
43use crate::coercion::CoerceMany;
44use crate::errors::{
45 AddressOfTemporaryTaken, BaseExpressionDoubleDot, BaseExpressionDoubleDotAddExpr,
46 BaseExpressionDoubleDotRemove, CantDereference, FieldMultiplySpecifiedInInitializer,
47 FunctionalRecordUpdateOnNonStruct, HelpUseLatestEdition, NakedAsmOutsideNakedFn,
48 NoFieldOnVariant, ReturnLikeStatementKind, ReturnStmtOutsideOfFnBody, StructExprNonExhaustive,
49 TypeMismatchFruTypo, YieldExprOutsideOfCoroutine,
50};
51use crate::op::contains_let_in_chain;
52use crate::{
53 BreakableCtxt, CoroutineTypes, Diverges, FnCtxt, GatherLocalsVisitor, Needs,
54 TupleArgumentsFlag, cast, fatally_break_rust, report_unexpected_variant_res, type_error_struct,
55};
56
57impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
58 pub(crate) fn precedence(&self, expr: &hir::Expr<'_>) -> ExprPrecedence {
59 let has_attr = |id: HirId| -> bool {
60 for attr in self.tcx.hir_attrs(id) {
61 if attr.span().desugaring_kind().is_none() {
75 return true;
76 }
77 }
78 false
79 };
80
81 if is_range_literal(expr) {
85 return ExprPrecedence::Range;
86 }
87
88 expr.precedence(&has_attr)
89 }
90
91 pub(crate) fn check_expr_has_type_or_error(
95 &self,
96 expr: &'tcx hir::Expr<'tcx>,
97 expected_ty: Ty<'tcx>,
98 extend_err: impl FnOnce(&mut Diag<'_>),
99 ) -> Ty<'tcx> {
100 let mut ty = self.check_expr_with_expectation(expr, ExpectHasType(expected_ty));
101
102 if self.try_structurally_resolve_type(expr.span, ty).is_never()
105 && self.tcx.expr_guaranteed_to_constitute_read_for_never(expr)
106 {
107 if let Some(adjustments) = self.typeck_results.borrow().adjustments().get(expr.hir_id) {
108 let reported = self.dcx().span_delayed_bug(
109 expr.span,
110 "expression with never type wound up being adjusted",
111 );
112
113 return if let [Adjustment { kind: Adjust::NeverToAny, target }] = &adjustments[..] {
114 target.to_owned()
115 } else {
116 Ty::new_error(self.tcx(), reported)
117 };
118 }
119
120 let adj_ty = self.next_ty_var(expr.span);
121 self.apply_adjustments(
122 expr,
123 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[Adjustment { kind: Adjust::NeverToAny, target: adj_ty }]))vec![Adjustment { kind: Adjust::NeverToAny, target: adj_ty }],
124 );
125 ty = adj_ty;
126 }
127
128 if let Err(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
129 let _ = self.emit_type_mismatch_suggestions(
130 &mut err,
131 expr.peel_drop_temps(),
132 ty,
133 expected_ty,
134 None,
135 None,
136 );
137 extend_err(&mut err);
138 err.emit();
139 }
140 ty
141 }
142
143 pub(super) fn check_expr_coercible_to_type(
147 &self,
148 expr: &'tcx hir::Expr<'tcx>,
149 expected: Ty<'tcx>,
150 expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
151 ) -> Ty<'tcx> {
152 self.check_expr_coercible_to_type_or_error(expr, expected, expected_ty_expr, |_, _| {})
153 }
154
155 pub(crate) fn check_expr_coercible_to_type_or_error(
156 &self,
157 expr: &'tcx hir::Expr<'tcx>,
158 expected: Ty<'tcx>,
159 expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
160 extend_err: impl FnOnce(&mut Diag<'_>, Ty<'tcx>),
161 ) -> Ty<'tcx> {
162 let ty = self.check_expr_with_hint(expr, expected);
163 match self.demand_coerce_diag(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No) {
165 Ok(ty) => ty,
166 Err(mut err) => {
167 extend_err(&mut err, ty);
168 err.emit();
169 expected
173 }
174 }
175 }
176
177 pub(super) fn check_expr_with_hint(
182 &self,
183 expr: &'tcx hir::Expr<'tcx>,
184 expected: Ty<'tcx>,
185 ) -> Ty<'tcx> {
186 self.check_expr_with_expectation(expr, ExpectHasType(expected))
187 }
188
189 fn check_expr_with_expectation_and_needs(
192 &self,
193 expr: &'tcx hir::Expr<'tcx>,
194 expected: Expectation<'tcx>,
195 needs: Needs,
196 ) -> Ty<'tcx> {
197 let ty = self.check_expr_with_expectation(expr, expected);
198
199 if let Needs::MutPlace = needs {
202 self.convert_place_derefs_to_mutable(expr);
203 }
204
205 ty
206 }
207
208 pub(super) fn check_expr(&self, expr: &'tcx hir::Expr<'tcx>) -> Ty<'tcx> {
210 self.check_expr_with_expectation(expr, NoExpectation)
211 }
212
213 pub(super) fn check_expr_with_needs(
216 &self,
217 expr: &'tcx hir::Expr<'tcx>,
218 needs: Needs,
219 ) -> Ty<'tcx> {
220 self.check_expr_with_expectation_and_needs(expr, NoExpectation, needs)
221 }
222
223 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check_expr_with_expectation",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(225u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["expected"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expected)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Ty<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{ self.check_expr_with_expectation_and_args(expr, expected, None) }
}
}#[instrument(skip(self, expr), level = "debug")]
226 pub(super) fn check_expr_with_expectation(
227 &self,
228 expr: &'tcx hir::Expr<'tcx>,
229 expected: Expectation<'tcx>,
230 ) -> Ty<'tcx> {
231 self.check_expr_with_expectation_and_args(expr, expected, None)
232 }
233
234 pub(super) fn check_expr_with_expectation_and_args(
239 &self,
240 expr: &'tcx hir::Expr<'tcx>,
241 expected: Expectation<'tcx>,
242 call_expr_and_args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
243 ) -> Ty<'tcx> {
244 if self.tcx().sess.verbose_internals() {
245 if let Ok(lint_str) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
247 if !lint_str.contains('\n') {
248 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:248",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(248u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("expr text: {0}",
lint_str) as &dyn Value))])
});
} else { ; }
};debug!("expr text: {lint_str}");
249 } else {
250 let mut lines = lint_str.lines();
251 if let Some(line0) = lines.next() {
252 let remaining_lines = lines.count();
253 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:253",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(253u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("expr text: {0}",
line0) as &dyn Value))])
});
} else { ; }
};debug!("expr text: {line0}");
254 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:254",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(254u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("expr text: ...(and {0} more lines)",
remaining_lines) as &dyn Value))])
});
} else { ; }
};debug!("expr text: ...(and {remaining_lines} more lines)");
255 }
256 }
257 }
258 }
259
260 let is_try_block_generated_unit_expr = match expr.kind {
264 ExprKind::Call(_, [arg]) => {
265 expr.span.is_desugaring(DesugaringKind::TryBlock)
266 && arg.span.is_desugaring(DesugaringKind::TryBlock)
267 }
268 _ => false,
269 };
270
271 if !is_try_block_generated_unit_expr {
273 self.warn_if_unreachable(expr.hir_id, expr.span, "expression");
274 }
275
276 let old_diverges = self.diverges.replace(Diverges::Maybe);
279
280 if self.is_whole_body.replace(false) {
281 self.diverges.set(self.function_diverges_because_of_empty_arguments.get())
284 };
285
286 let ty = ensure_sufficient_stack(|| match &expr.kind {
287 hir::ExprKind::Path(
289 qpath @ (hir::QPath::Resolved(..) | hir::QPath::TypeRelative(..)),
290 ) => self.check_expr_path(qpath, expr, call_expr_and_args),
291 _ => self.check_expr_kind(expr, expected),
292 });
293 let ty = self.resolve_vars_if_possible(ty);
294
295 match expr.kind {
297 ExprKind::Block(..)
298 | ExprKind::If(..)
299 | ExprKind::Let(..)
300 | ExprKind::Loop(..)
301 | ExprKind::Match(..) => {}
302 ExprKind::Cast(_, _) => {}
305 ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::TryBlock) => {}
309 ExprKind::Call(..) if expr.span.is_desugaring(DesugaringKind::Contract) => {}
311 ExprKind::Call(callee, _) => self.warn_if_unreachable(expr.hir_id, callee.span, "call"),
312 ExprKind::MethodCall(segment, ..) => {
313 self.warn_if_unreachable(expr.hir_id, segment.ident.span, "call")
314 }
315 _ => self.warn_if_unreachable(expr.hir_id, expr.span, "expression"),
316 }
317
318 if self.try_structurally_resolve_type(expr.span, ty).is_never()
323 && self.tcx.expr_guaranteed_to_constitute_read_for_never(expr)
324 {
325 self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
326 }
327
328 self.write_ty(expr.hir_id, ty);
332
333 self.diverges.set(self.diverges.get() | old_diverges);
335
336 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:336",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(336u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("type of {0} is...",
self.tcx.hir_id_to_string(expr.hir_id)) as &dyn Value))])
});
} else { ; }
};debug!("type of {} is...", self.tcx.hir_id_to_string(expr.hir_id));
337 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:337",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(337u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("... {0:?}, expected is {1:?}",
ty, expected) as &dyn Value))])
});
} else { ; }
};debug!("... {:?}, expected is {:?}", ty, expected);
338
339 ty
340 }
341
342 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check_expr_kind",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(342u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["expected"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&expected)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Ty<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:348",
"rustc_hir_typeck::expr", ::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(348u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("expr={0:#?}",
expr) as &dyn Value))])
});
} else { ; }
};
let tcx = self.tcx;
match expr.kind {
ExprKind::Lit(ref lit) =>
self.check_expr_lit(lit, expr.hir_id, expected),
ExprKind::Binary(op, lhs, rhs) =>
self.check_expr_binop(expr, op, lhs, rhs, expected),
ExprKind::Assign(lhs, rhs, span) => {
self.check_expr_assign(expr, expected, lhs, rhs, span)
}
ExprKind::AssignOp(op, lhs, rhs) => {
self.check_expr_assign_op(expr, op, lhs, rhs, expected)
}
ExprKind::Unary(unop, oprnd) =>
self.check_expr_unop(unop, oprnd, expected, expr),
ExprKind::AddrOf(kind, mutbl, oprnd) => {
self.check_expr_addr_of(kind, mutbl, oprnd, expected, expr)
}
ExprKind::Path(ref qpath) =>
self.check_expr_path(qpath, expr, None),
ExprKind::InlineAsm(asm) => {
self.deferred_asm_checks.borrow_mut().push((asm,
expr.hir_id));
self.check_expr_asm(asm, expr.span)
}
ExprKind::OffsetOf(container, fields) => {
self.check_expr_offset_of(container, fields, expr)
}
ExprKind::Break(destination, ref expr_opt) => {
self.check_expr_break(destination, expr_opt.as_deref(),
expr)
}
ExprKind::Continue(destination) =>
self.check_expr_continue(destination, expr),
ExprKind::Ret(ref expr_opt) =>
self.check_expr_return(expr_opt.as_deref(), expr),
ExprKind::Become(call) => self.check_expr_become(call, expr),
ExprKind::Let(let_expr) =>
self.check_expr_let(let_expr, expr.hir_id),
ExprKind::Loop(body, _, source, _) => {
self.check_expr_loop(body, source, expected, expr)
}
ExprKind::Match(discrim, arms, match_src) => {
self.check_expr_match(expr, discrim, arms, expected,
match_src)
}
ExprKind::Closure(closure) =>
self.check_expr_closure(closure, expr.span, expected),
ExprKind::Block(body, _) =>
self.check_expr_block(body, expected),
ExprKind::Call(callee, args) =>
self.check_expr_call(expr, callee, args, expected),
ExprKind::Use(used_expr, _) =>
self.check_expr_use(used_expr, expected),
ExprKind::MethodCall(segment, receiver, args, _) => {
self.check_expr_method_call(expr, segment, receiver, args,
expected)
}
ExprKind::Cast(e, t) => self.check_expr_cast(e, t, expr),
ExprKind::Type(e, t) => {
let ascribed_ty = self.lower_ty_saving_user_provided_ty(t);
let ty = self.check_expr_with_hint(e, ascribed_ty);
self.demand_eqtype(e.span, ascribed_ty, ty);
ascribed_ty
}
ExprKind::If(cond, then_expr, opt_else_expr) => {
self.check_expr_if(expr.hir_id, cond, then_expr,
opt_else_expr, expr.span, expected)
}
ExprKind::DropTemps(e) =>
self.check_expr_with_expectation(e, expected),
ExprKind::Array(args) =>
self.check_expr_array(args, expected, expr),
ExprKind::ConstBlock(ref block) =>
self.check_expr_const_block(block, expected),
ExprKind::Repeat(element, ref count) => {
self.check_expr_repeat(element, count, expected, expr)
}
ExprKind::Tup(elts) =>
self.check_expr_tuple(elts, expected, expr),
ExprKind::Struct(qpath, fields, ref base_expr) => {
self.check_expr_struct(expr, expected, qpath, fields,
base_expr)
}
ExprKind::Field(base, field) =>
self.check_expr_field(expr, base, field, expected),
ExprKind::Index(base, idx, brackets_span) => {
self.check_expr_index(base, idx, expr, brackets_span)
}
ExprKind::Yield(value, _) =>
self.check_expr_yield(value, expr),
ExprKind::UnsafeBinderCast(kind, inner_expr, ty) => {
self.check_expr_unsafe_binder_cast(expr.span, kind,
inner_expr, ty, expected)
}
ExprKind::Err(guar) => Ty::new_error(tcx, guar),
}
}
}
}#[instrument(skip(self, expr), level = "debug")]
343 fn check_expr_kind(
344 &self,
345 expr: &'tcx hir::Expr<'tcx>,
346 expected: Expectation<'tcx>,
347 ) -> Ty<'tcx> {
348 trace!("expr={:#?}", expr);
349
350 let tcx = self.tcx;
351 match expr.kind {
352 ExprKind::Lit(ref lit) => self.check_expr_lit(lit, expr.hir_id, expected),
353 ExprKind::Binary(op, lhs, rhs) => self.check_expr_binop(expr, op, lhs, rhs, expected),
354 ExprKind::Assign(lhs, rhs, span) => {
355 self.check_expr_assign(expr, expected, lhs, rhs, span)
356 }
357 ExprKind::AssignOp(op, lhs, rhs) => {
358 self.check_expr_assign_op(expr, op, lhs, rhs, expected)
359 }
360 ExprKind::Unary(unop, oprnd) => self.check_expr_unop(unop, oprnd, expected, expr),
361 ExprKind::AddrOf(kind, mutbl, oprnd) => {
362 self.check_expr_addr_of(kind, mutbl, oprnd, expected, expr)
363 }
364 ExprKind::Path(ref qpath) => self.check_expr_path(qpath, expr, None),
365 ExprKind::InlineAsm(asm) => {
366 self.deferred_asm_checks.borrow_mut().push((asm, expr.hir_id));
368 self.check_expr_asm(asm, expr.span)
369 }
370 ExprKind::OffsetOf(container, fields) => {
371 self.check_expr_offset_of(container, fields, expr)
372 }
373 ExprKind::Break(destination, ref expr_opt) => {
374 self.check_expr_break(destination, expr_opt.as_deref(), expr)
375 }
376 ExprKind::Continue(destination) => self.check_expr_continue(destination, expr),
377 ExprKind::Ret(ref expr_opt) => self.check_expr_return(expr_opt.as_deref(), expr),
378 ExprKind::Become(call) => self.check_expr_become(call, expr),
379 ExprKind::Let(let_expr) => self.check_expr_let(let_expr, expr.hir_id),
380 ExprKind::Loop(body, _, source, _) => {
381 self.check_expr_loop(body, source, expected, expr)
382 }
383 ExprKind::Match(discrim, arms, match_src) => {
384 self.check_expr_match(expr, discrim, arms, expected, match_src)
385 }
386 ExprKind::Closure(closure) => self.check_expr_closure(closure, expr.span, expected),
387 ExprKind::Block(body, _) => self.check_expr_block(body, expected),
388 ExprKind::Call(callee, args) => self.check_expr_call(expr, callee, args, expected),
389 ExprKind::Use(used_expr, _) => self.check_expr_use(used_expr, expected),
390 ExprKind::MethodCall(segment, receiver, args, _) => {
391 self.check_expr_method_call(expr, segment, receiver, args, expected)
392 }
393 ExprKind::Cast(e, t) => self.check_expr_cast(e, t, expr),
394 ExprKind::Type(e, t) => {
395 let ascribed_ty = self.lower_ty_saving_user_provided_ty(t);
396 let ty = self.check_expr_with_hint(e, ascribed_ty);
397 self.demand_eqtype(e.span, ascribed_ty, ty);
398 ascribed_ty
399 }
400 ExprKind::If(cond, then_expr, opt_else_expr) => {
401 self.check_expr_if(expr.hir_id, cond, then_expr, opt_else_expr, expr.span, expected)
402 }
403 ExprKind::DropTemps(e) => self.check_expr_with_expectation(e, expected),
404 ExprKind::Array(args) => self.check_expr_array(args, expected, expr),
405 ExprKind::ConstBlock(ref block) => self.check_expr_const_block(block, expected),
406 ExprKind::Repeat(element, ref count) => {
407 self.check_expr_repeat(element, count, expected, expr)
408 }
409 ExprKind::Tup(elts) => self.check_expr_tuple(elts, expected, expr),
410 ExprKind::Struct(qpath, fields, ref base_expr) => {
411 self.check_expr_struct(expr, expected, qpath, fields, base_expr)
412 }
413 ExprKind::Field(base, field) => self.check_expr_field(expr, base, field, expected),
414 ExprKind::Index(base, idx, brackets_span) => {
415 self.check_expr_index(base, idx, expr, brackets_span)
416 }
417 ExprKind::Yield(value, _) => self.check_expr_yield(value, expr),
418 ExprKind::UnsafeBinderCast(kind, inner_expr, ty) => {
419 self.check_expr_unsafe_binder_cast(expr.span, kind, inner_expr, ty, expected)
420 }
421 ExprKind::Err(guar) => Ty::new_error(tcx, guar),
422 }
423 }
424
425 fn check_expr_unop(
426 &self,
427 unop: hir::UnOp,
428 oprnd: &'tcx hir::Expr<'tcx>,
429 expected: Expectation<'tcx>,
430 expr: &'tcx hir::Expr<'tcx>,
431 ) -> Ty<'tcx> {
432 let tcx = self.tcx;
433 let expected_inner = match unop {
434 hir::UnOp::Not | hir::UnOp::Neg => expected,
435 hir::UnOp::Deref => NoExpectation,
436 };
437 let oprnd_t = self.check_expr_with_expectation(oprnd, expected_inner);
438
439 if let Err(guar) = oprnd_t.error_reported() {
440 return Ty::new_error(tcx, guar);
441 }
442
443 let oprnd_t = self.structurally_resolve_type(expr.span, oprnd_t);
444 match unop {
445 hir::UnOp::Deref => self.lookup_derefing(expr, oprnd, oprnd_t).unwrap_or_else(|| {
446 let mut err =
447 self.dcx().create_err(CantDereference { span: expr.span, ty: oprnd_t });
448 let sp = tcx.sess.source_map().start_point(expr.span).with_parent(None);
449 if let Some(sp) = tcx.sess.psess.ambiguous_block_expr_parse.borrow().get(&sp) {
450 err.subdiagnostic(ExprParenthesesNeeded::surrounding(*sp));
451 }
452 Ty::new_error(tcx, err.emit())
453 }),
454 hir::UnOp::Not => {
455 let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner);
456 if oprnd_t.is_integral() || *oprnd_t.kind() == ty::Bool { oprnd_t } else { result }
458 }
459 hir::UnOp::Neg => {
460 let result = self.check_user_unop(expr, oprnd_t, unop, expected_inner);
461 if oprnd_t.is_numeric() { oprnd_t } else { result }
463 }
464 }
465 }
466
467 fn check_expr_addr_of(
468 &self,
469 kind: hir::BorrowKind,
470 mutbl: hir::Mutability,
471 oprnd: &'tcx hir::Expr<'tcx>,
472 expected: Expectation<'tcx>,
473 expr: &'tcx hir::Expr<'tcx>,
474 ) -> Ty<'tcx> {
475 let hint = expected.only_has_type(self).map_or(NoExpectation, |ty| {
476 match self.try_structurally_resolve_type(expr.span, ty).kind() {
477 ty::Ref(_, ty, _) | ty::RawPtr(ty, _) => {
478 if oprnd.is_syntactic_place_expr() {
479 ExpectHasType(*ty)
483 } else {
484 Expectation::rvalue_hint(self, *ty)
485 }
486 }
487 _ => NoExpectation,
488 }
489 });
490 let ty =
491 self.check_expr_with_expectation_and_needs(oprnd, hint, Needs::maybe_mut_place(mutbl));
492 if let Err(guar) = ty.error_reported() {
493 return Ty::new_error(self.tcx, guar);
494 }
495
496 match kind {
497 hir::BorrowKind::Raw => {
498 self.check_named_place_expr(oprnd);
499 Ty::new_ptr(self.tcx, ty, mutbl)
500 }
501 hir::BorrowKind::Ref | hir::BorrowKind::Pin => {
502 let region = self.next_region_var(RegionVariableOrigin::BorrowRegion(expr.span));
517 match kind {
518 hir::BorrowKind::Ref => Ty::new_ref(self.tcx, region, ty, mutbl),
519 hir::BorrowKind::Pin => Ty::new_pinned_ref(self.tcx, region, ty, mutbl),
520 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
521 }
522 }
523 }
524 }
525
526 fn check_named_place_expr(&self, oprnd: &'tcx hir::Expr<'tcx>) {
532 let is_named = oprnd.is_place_expr(|base| {
533 self.typeck_results
545 .borrow()
546 .adjustments()
547 .get(base.hir_id)
548 .is_some_and(|x| x.iter().any(|adj| #[allow(non_exhaustive_omitted_patterns)] match adj.kind {
Adjust::Deref(_) => true,
_ => false,
}matches!(adj.kind, Adjust::Deref(_))))
549 });
550 if !is_named {
551 self.dcx().emit_err(AddressOfTemporaryTaken { span: oprnd.span });
552 }
553 }
554
555 pub(crate) fn check_expr_path(
556 &self,
557 qpath: &'tcx hir::QPath<'tcx>,
558 expr: &'tcx hir::Expr<'tcx>,
559 call_expr_and_args: Option<(&'tcx hir::Expr<'tcx>, &'tcx [hir::Expr<'tcx>])>,
560 ) -> Ty<'tcx> {
561 let tcx = self.tcx;
562
563 if let Some((_, [arg])) = call_expr_and_args
564 && let QPath::Resolved(_, path) = qpath
565 && let Res::Def(_, def_id) = path.res
566 && let Some(lang_item) = tcx.lang_items().from_def_id(def_id)
567 {
568 let code = match lang_item {
569 LangItem::IntoFutureIntoFuture
570 if expr.span.is_desugaring(DesugaringKind::Await) =>
571 {
572 Some(ObligationCauseCode::AwaitableExpr(arg.hir_id))
573 }
574 LangItem::IntoIterIntoIter | LangItem::IteratorNext
575 if expr.span.is_desugaring(DesugaringKind::ForLoop) =>
576 {
577 Some(ObligationCauseCode::ForLoopIterator)
578 }
579 LangItem::TryTraitFromOutput
580 if expr.span.is_desugaring(DesugaringKind::TryBlock) =>
581 {
582 Some(ObligationCauseCode::QuestionMark)
584 }
585 LangItem::TryTraitBranch | LangItem::TryTraitFromResidual
586 if expr.span.is_desugaring(DesugaringKind::QuestionMark) =>
587 {
588 Some(ObligationCauseCode::QuestionMark)
589 }
590 _ => None,
591 };
592 if let Some(code) = code {
593 let args = self.fresh_args_for_item(expr.span, def_id);
594 self.add_required_obligations_with_code(expr.span, def_id, args, |_, _| {
595 code.clone()
596 });
597 return tcx.type_of(def_id).instantiate(tcx, args).skip_norm_wip();
598 }
599 }
600
601 let (res, opt_ty, segs) =
602 self.resolve_ty_and_res_fully_qualified_call(qpath, expr.hir_id, expr.span);
603 let ty = match res {
604 Res::Err => {
605 self.suggest_assoc_method_call(segs);
606 let e =
607 self.dcx().span_delayed_bug(qpath.span(), "`Res::Err` but no error emitted");
608 Ty::new_error(tcx, e)
609 }
610 Res::Def(DefKind::Variant, _) => {
611 let e = report_unexpected_variant_res(
612 tcx,
613 res,
614 Some(expr),
615 qpath,
616 expr.span,
617 E0533,
618 "value",
619 );
620 Ty::new_error(tcx, e)
621 }
622 _ => {
623 self.instantiate_value_path(
624 segs,
625 opt_ty,
626 res,
627 call_expr_and_args.map_or(expr.span, |(e, _)| e.span),
628 expr.span,
629 expr.hir_id,
630 )
631 .0
632 }
633 };
634
635 if let ty::FnDef(did, _) = *ty.kind() {
636 let fn_sig = ty.fn_sig(tcx);
637
638 if tcx.is_intrinsic(did, sym::transmute) {
639 let Some(from) = fn_sig.inputs().skip_binder().get(0) else {
640 ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(did),
format_args!("intrinsic fn `transmute` defined with no parameters"));span_bug!(
641 tcx.def_span(did),
642 "intrinsic fn `transmute` defined with no parameters"
643 );
644 };
645 let to = fn_sig.output().skip_binder();
646 self.deferred_transmute_checks.borrow_mut().push((*from, to, expr.hir_id));
651 }
652 if !tcx.features().unsized_fn_params() {
653 for i in 0..fn_sig.inputs().skip_binder().len() {
663 let span = call_expr_and_args
667 .and_then(|(_, args)| args.get(i))
668 .map_or(expr.span, |arg| arg.span);
669 let input = self.instantiate_binder_with_fresh_vars(
670 span,
671 infer::BoundRegionConversionTime::FnCall,
672 fn_sig.input(i),
673 );
674 self.require_type_is_sized_deferred(
675 input,
676 span,
677 ObligationCauseCode::SizedArgumentType(None),
678 );
679 }
680 }
681 let output = self.instantiate_binder_with_fresh_vars(
687 expr.span,
688 infer::BoundRegionConversionTime::FnCall,
689 fn_sig.output(),
690 );
691 self.require_type_is_sized_deferred(
692 output,
693 call_expr_and_args.map_or(expr.span, |(e, _)| e.span),
694 ObligationCauseCode::SizedCallReturnType,
695 );
696 }
697
698 let args = self.typeck_results.borrow().node_args(expr.hir_id);
701 self.add_wf_bounds(args, expr.span);
702
703 ty
704 }
705
706 fn check_expr_break(
707 &self,
708 destination: hir::Destination,
709 expr_opt: Option<&'tcx hir::Expr<'tcx>>,
710 expr: &'tcx hir::Expr<'tcx>,
711 ) -> Ty<'tcx> {
712 let tcx = self.tcx;
713 if let Ok(target_id) = destination.target_id {
714 let (e_ty, cause);
715 if let Some(e) = expr_opt {
716 let opt_coerce_to = {
719 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
723 match enclosing_breakables.opt_find_breakable(target_id) {
724 Some(ctxt) => ctxt.coerce.as_ref().map(|coerce| coerce.expected_ty()),
725 None => {
726 return Ty::new_error_with_message(
728 tcx,
729 expr.span,
730 "break was outside loop, but no error was emitted",
731 );
732 }
733 }
734 };
735
736 let coerce_to = opt_coerce_to.unwrap_or_else(|| {
741 let guar = self.dcx().span_delayed_bug(
742 expr.span,
743 "illegal break with value found but no error reported",
744 );
745 self.set_tainted_by_errors(guar);
746 Ty::new_error(tcx, guar)
747 });
748
749 e_ty = self.check_expr_with_hint(e, coerce_to);
751 cause = self.misc(e.span);
752 } else {
753 e_ty = tcx.types.unit;
756 cause = self.misc(expr.span);
757 }
758
759 let mut enclosing_breakables = self.enclosing_breakables.borrow_mut();
763 let Some(ctxt) = enclosing_breakables.opt_find_breakable(target_id) else {
764 return Ty::new_error_with_message(
766 tcx,
767 expr.span,
768 "break was outside loop, but no error was emitted",
769 );
770 };
771
772 if let Some(ref mut coerce) = ctxt.coerce {
773 if let Some(e) = expr_opt {
774 coerce.coerce(self, &cause, e, e_ty);
775 } else {
776 if !e_ty.is_unit() {
::core::panicking::panic("assertion failed: e_ty.is_unit()")
};assert!(e_ty.is_unit());
777 let ty = coerce.expected_ty();
778 coerce.coerce_forced_unit(
779 self,
780 &cause,
781 |mut err| {
782 self.suggest_missing_semicolon(&mut err, expr, e_ty, false, false);
783 self.suggest_mismatched_types_on_tail(
784 &mut err, expr, ty, e_ty, target_id,
785 );
786 let error =
787 Some(TypeError::Sorts(ExpectedFound { expected: ty, found: e_ty }));
788 self.annotate_loop_expected_due_to_inference(err, expr, error);
789 if let Some(val) =
790 self.err_ctxt().ty_kind_suggestion(self.param_env, ty)
791 {
792 err.span_suggestion_verbose(
793 expr.span.shrink_to_hi(),
794 "give the `break` a value of the expected type",
795 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {0}", val))
})format!(" {val}"),
796 Applicability::HasPlaceholders,
797 );
798 }
799 },
800 false,
801 );
802 }
803 } else {
804 if !(expr_opt.is_none() || self.tainted_by_errors().is_some()) {
::core::panicking::panic("assertion failed: expr_opt.is_none() || self.tainted_by_errors().is_some()")
};assert!(expr_opt.is_none() || self.tainted_by_errors().is_some());
812 }
813
814 ctxt.may_break |= !self.diverges.get().is_always();
818
819 tcx.types.never
821 } else {
822 let err = Ty::new_error_with_message(
827 self.tcx,
828 expr.span,
829 "break was outside loop, but no error was emitted",
830 );
831
832 if let Some(e) = expr_opt {
835 self.check_expr_with_hint(e, err);
836
837 if let ExprKind::Path(QPath::Resolved(_, path)) = e.kind {
840 if let [segment] = path.segments
841 && segment.ident.name == sym::rust
842 {
843 fatally_break_rust(self.tcx, expr.span);
844 }
845 }
846 }
847
848 err
850 }
851 }
852
853 fn check_expr_continue(
854 &self,
855 destination: hir::Destination,
856 expr: &'tcx hir::Expr<'tcx>,
857 ) -> Ty<'tcx> {
858 if let Ok(target_id) = destination.target_id {
859 if let hir::Node::Expr(hir::Expr { kind: ExprKind::Loop(..), .. }) =
860 self.tcx.hir_node(target_id)
861 {
862 self.tcx.types.never
863 } else {
864 let guar = self.dcx().span_delayed_bug(
867 expr.span,
868 "found `continue` not pointing to loop, but no error reported",
869 );
870 Ty::new_error(self.tcx, guar)
871 }
872 } else {
873 Ty::new_misc_error(self.tcx)
875 }
876 }
877
878 fn check_expr_return(
879 &self,
880 expr_opt: Option<&'tcx hir::Expr<'tcx>>,
881 expr: &'tcx hir::Expr<'tcx>,
882 ) -> Ty<'tcx> {
883 if self.ret_coercion.is_none() {
884 self.emit_return_outside_of_fn_body(expr, ReturnLikeStatementKind::Return);
885
886 if let Some(e) = expr_opt {
887 self.check_expr(e);
890 }
891 } else if let Some(e) = expr_opt {
892 if self.ret_coercion_span.get().is_none() {
893 self.ret_coercion_span.set(Some(e.span));
894 }
895 self.check_return_or_body_tail(e, true);
896 } else {
897 let mut coercion = self.ret_coercion.as_ref().unwrap().borrow_mut();
898 if self.ret_coercion_span.get().is_none() {
899 self.ret_coercion_span.set(Some(expr.span));
900 }
901 let cause = self.cause(expr.span, ObligationCauseCode::ReturnNoExpression);
902 if let Some((_, fn_decl)) = self.get_fn_decl(expr.hir_id) {
903 coercion.coerce_forced_unit(
904 self,
905 &cause,
906 |db| {
907 let span = fn_decl.output.span();
908 if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
909 db.span_label(
910 span,
911 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `{0}` because of this return type",
snippet))
})format!("expected `{snippet}` because of this return type"),
912 );
913 }
914 },
915 true,
916 );
917 } else {
918 coercion.coerce_forced_unit(self, &cause, |_| (), true);
919 }
920 }
921 self.tcx.types.never
922 }
923
924 fn check_expr_become(
925 &self,
926 call: &'tcx hir::Expr<'tcx>,
927 expr: &'tcx hir::Expr<'tcx>,
928 ) -> Ty<'tcx> {
929 match &self.ret_coercion {
930 Some(ret_coercion) => {
931 let ret_ty = ret_coercion.borrow().expected_ty();
932 let call_expr_ty = self.check_expr_with_hint(call, ret_ty);
933
934 self.demand_suptype(expr.span, ret_ty, call_expr_ty);
937 }
938 None => {
939 self.emit_return_outside_of_fn_body(expr, ReturnLikeStatementKind::Become);
940
941 self.check_expr(call);
944 }
945 }
946
947 self.tcx.types.never
948 }
949
950 pub(super) fn check_return_or_body_tail(
959 &self,
960 return_expr: &'tcx hir::Expr<'tcx>,
961 explicit_return: bool,
962 ) {
963 let ret_coercion = self.ret_coercion.as_ref().unwrap_or_else(|| {
964 ::rustc_middle::util::bug::span_bug_fmt(return_expr.span,
format_args!("check_return_expr called outside fn body"))span_bug!(return_expr.span, "check_return_expr called outside fn body")
965 });
966
967 let ret_ty = ret_coercion.borrow().expected_ty();
968 let return_expr_ty = self.check_expr_with_hint(return_expr, ret_ty);
969 let mut span = return_expr.span;
970 let mut hir_id = return_expr.hir_id;
971 if !explicit_return
974 && let ExprKind::Block(body, _) = return_expr.kind
975 && let Some(last_expr) = body.expr
976 {
977 span = last_expr.span;
978 hir_id = last_expr.hir_id;
979 }
980 ret_coercion.borrow_mut().coerce(
981 self,
982 &self.cause(span, ObligationCauseCode::ReturnValue(return_expr.hir_id)),
983 return_expr,
984 return_expr_ty,
985 );
986
987 if let Some(fn_sig) = self.body_fn_sig()
988 && fn_sig.output().has_opaque_types()
989 {
990 self.select_obligations_where_possible(|errors| {
993 self.point_at_return_for_opaque_ty_error(
994 errors,
995 hir_id,
996 span,
997 return_expr_ty,
998 return_expr.span,
999 );
1000 });
1001 }
1002 }
1003
1004 fn emit_return_outside_of_fn_body(&self, expr: &hir::Expr<'_>, kind: ReturnLikeStatementKind) {
1009 let mut err = ReturnStmtOutsideOfFnBody {
1010 span: expr.span,
1011 encl_body_span: None,
1012 encl_fn_span: None,
1013 statement_kind: kind,
1014 };
1015
1016 let encl_item_id = self.tcx.hir_get_parent_item(expr.hir_id);
1017
1018 if let hir::Node::Item(hir::Item {
1019 kind: hir::ItemKind::Fn { .. },
1020 span: encl_fn_span,
1021 ..
1022 })
1023 | hir::Node::TraitItem(hir::TraitItem {
1024 kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)),
1025 span: encl_fn_span,
1026 ..
1027 })
1028 | hir::Node::ImplItem(hir::ImplItem {
1029 kind: hir::ImplItemKind::Fn(..),
1030 span: encl_fn_span,
1031 ..
1032 }) = self.tcx.hir_node_by_def_id(encl_item_id.def_id)
1033 {
1034 let encl_body_owner_id = self.tcx.hir_enclosing_body_owner(expr.hir_id);
1038
1039 match (&encl_item_id.def_id, &encl_body_owner_id) {
(left_val, right_val) => {
if *left_val == *right_val {
let kind = ::core::panicking::AssertKind::Ne;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::None);
}
}
};assert_ne!(encl_item_id.def_id, encl_body_owner_id);
1042
1043 let encl_body = self.tcx.hir_body_owned_by(encl_body_owner_id);
1044
1045 err.encl_body_span = Some(encl_body.value.span);
1046 err.encl_fn_span = Some(*encl_fn_span);
1047 }
1048
1049 self.dcx().emit_err(err);
1050 }
1051
1052 fn point_at_return_for_opaque_ty_error(
1053 &self,
1054 errors: &mut Vec<traits::FulfillmentError<'tcx>>,
1055 hir_id: HirId,
1056 span: Span,
1057 return_expr_ty: Ty<'tcx>,
1058 return_span: Span,
1059 ) {
1060 if span == return_span {
1062 return;
1063 }
1064 for err in errors {
1065 let cause = &mut err.obligation.cause;
1066 if let ObligationCauseCode::OpaqueReturnType(None) = cause.code() {
1067 let new_cause = self.cause(
1068 cause.span,
1069 ObligationCauseCode::OpaqueReturnType(Some((return_expr_ty, hir_id))),
1070 );
1071 *cause = new_cause;
1072 }
1073 }
1074 }
1075
1076 pub(crate) fn check_lhs_assignable(
1077 &self,
1078 lhs: &'tcx hir::Expr<'tcx>,
1079 code: ErrCode,
1080 op_span: Span,
1081 adjust_err: impl FnOnce(&mut Diag<'_>),
1082 ) {
1083 if lhs.is_syntactic_place_expr() {
1084 return;
1085 }
1086
1087 if contains_let_in_chain(lhs) {
1090 return;
1091 }
1092
1093 let mut err = self.dcx().struct_span_err(op_span, "invalid left-hand side of assignment");
1094 err.code(code);
1095 err.span_label(lhs.span, "cannot assign to this expression");
1096
1097 self.comes_from_while_condition(lhs.hir_id, |expr| {
1098 err.span_suggestion_verbose(
1099 expr.span.shrink_to_lo(),
1100 "you might have meant to use pattern destructuring",
1101 "let ",
1102 Applicability::MachineApplicable,
1103 );
1104 });
1105 self.check_for_missing_semi(lhs, &mut err);
1106
1107 adjust_err(&mut err);
1108
1109 err.emit();
1110 }
1111
1112 pub(crate) fn check_for_missing_semi(
1114 &self,
1115 expr: &'tcx hir::Expr<'tcx>,
1116 err: &mut Diag<'_>,
1117 ) -> bool {
1118 if let hir::ExprKind::Binary(binop, lhs, rhs) = expr.kind
1119 && let hir::BinOpKind::Mul = binop.node
1120 && self.tcx.sess.source_map().is_multiline(lhs.span.between(rhs.span))
1121 && rhs.is_syntactic_place_expr()
1122 {
1123 err.span_suggestion_verbose(
1128 lhs.span.shrink_to_hi(),
1129 "you might have meant to write a semicolon here",
1130 ";",
1131 Applicability::MachineApplicable,
1132 );
1133 return true;
1134 }
1135 false
1136 }
1137
1138 pub(super) fn comes_from_while_condition(
1142 &self,
1143 original_expr_id: HirId,
1144 then: impl FnOnce(&hir::Expr<'_>),
1145 ) {
1146 let mut parent = self.tcx.parent_hir_id(original_expr_id);
1147 loop {
1148 let node = self.tcx.hir_node(parent);
1149 match node {
1150 hir::Node::Expr(hir::Expr {
1151 kind:
1152 hir::ExprKind::Loop(
1153 hir::Block {
1154 expr:
1155 Some(hir::Expr {
1156 kind:
1157 hir::ExprKind::Match(expr, ..) | hir::ExprKind::If(expr, ..),
1158 ..
1159 }),
1160 ..
1161 },
1162 _,
1163 hir::LoopSource::While,
1164 _,
1165 ),
1166 ..
1167 }) => {
1168 if self.tcx.hir_parent_id_iter(original_expr_id).any(|id| id == expr.hir_id) {
1172 then(expr);
1173 }
1174 break;
1175 }
1176 hir::Node::Item(_)
1177 | hir::Node::ImplItem(_)
1178 | hir::Node::TraitItem(_)
1179 | hir::Node::Crate(_) => break,
1180 _ => {
1181 parent = self.tcx.parent_hir_id(parent);
1182 }
1183 }
1184 }
1185 }
1186
1187 fn check_expr_if(
1190 &self,
1191 expr_id: HirId,
1192 cond_expr: &'tcx hir::Expr<'tcx>,
1193 then_expr: &'tcx hir::Expr<'tcx>,
1194 opt_else_expr: Option<&'tcx hir::Expr<'tcx>>,
1195 sp: Span,
1196 orig_expected: Expectation<'tcx>,
1197 ) -> Ty<'tcx> {
1198 let cond_ty = self.check_expr_has_type_or_error(cond_expr, self.tcx.types.bool, |_| {});
1199
1200 self.warn_if_unreachable(
1201 cond_expr.hir_id,
1202 then_expr.span,
1203 "block in `if` or `while` expression",
1204 );
1205
1206 let cond_diverges = self.diverges.get();
1207 self.diverges.set(Diverges::Maybe);
1208
1209 let expected = orig_expected.try_structurally_resolve_and_adjust_for_branches(self, sp);
1210 let then_ty = self.check_expr_with_expectation(then_expr, expected);
1211 let then_diverges = self.diverges.get();
1212 self.diverges.set(Diverges::Maybe);
1213
1214 let coerce_to_ty = expected.coercion_target_type(self, sp);
1221 let mut coerce = CoerceMany::with_capacity(coerce_to_ty, 2);
1222
1223 coerce.coerce(self, &self.misc(sp), then_expr, then_ty);
1224
1225 if let Some(else_expr) = opt_else_expr {
1226 let else_ty = self.check_expr_with_expectation(else_expr, expected);
1227 let else_diverges = self.diverges.get();
1228
1229 let tail_defines_return_position_impl_trait =
1230 self.return_position_impl_trait_from_match_expectation(orig_expected);
1231 let if_cause =
1232 self.if_cause(expr_id, else_expr, tail_defines_return_position_impl_trait);
1233
1234 coerce.coerce(self, &if_cause, else_expr, else_ty);
1235
1236 self.diverges.set(cond_diverges | then_diverges & else_diverges);
1238 } else {
1239 self.if_fallback_coercion(sp, cond_expr, then_expr, &mut coerce);
1240
1241 self.diverges.set(cond_diverges);
1243 }
1244
1245 let result_ty = coerce.complete(self);
1246 if let Err(guar) = cond_ty.error_reported() {
1247 Ty::new_error(self.tcx, guar)
1248 } else {
1249 result_ty
1250 }
1251 }
1252
1253 fn check_expr_assign(
1256 &self,
1257 expr: &'tcx hir::Expr<'tcx>,
1258 expected: Expectation<'tcx>,
1259 lhs: &'tcx hir::Expr<'tcx>,
1260 rhs: &'tcx hir::Expr<'tcx>,
1261 span: Span,
1262 ) -> Ty<'tcx> {
1263 let expected_ty = expected.only_has_type(self);
1264 if expected_ty == Some(self.tcx.types.bool) {
1265 let guar = self.expr_assign_expected_bool_error(expr, lhs, rhs, span);
1266 return Ty::new_error(self.tcx, guar);
1267 }
1268
1269 let lhs_ty = self.check_expr_with_needs(lhs, Needs::MutPlace);
1270
1271 let suggest_deref_binop = |err: &mut Diag<'_>, rhs_ty: Ty<'tcx>| {
1272 if let Some(lhs_deref_ty) = self.deref_once_mutably_for_diagnostic(lhs_ty) {
1273 let lhs_deref_ty_is_sized = self
1276 .infcx
1277 .type_implements_trait(
1278 self.tcx.require_lang_item(LangItem::Sized, span),
1279 [lhs_deref_ty],
1280 self.param_env,
1281 )
1282 .may_apply();
1283 if lhs_deref_ty_is_sized && self.may_coerce(rhs_ty, lhs_deref_ty) {
1284 err.span_suggestion_verbose(
1285 lhs.span.shrink_to_lo(),
1286 "consider dereferencing here to assign to the mutably borrowed value",
1287 "*",
1288 Applicability::MachineApplicable,
1289 );
1290 }
1291 }
1292 };
1293
1294 let rhs_ty = self.check_expr_with_hint(rhs, lhs_ty);
1297 if let Err(mut diag) =
1298 self.demand_coerce_diag(rhs, rhs_ty, lhs_ty, Some(lhs), AllowTwoPhase::No)
1299 {
1300 suggest_deref_binop(&mut diag, rhs_ty);
1301 diag.emit();
1302 }
1303
1304 self.check_lhs_assignable(lhs, E0070, span, |err| {
1305 if let Some(rhs_ty) = self.typeck_results.borrow().expr_ty_opt(rhs) {
1306 suggest_deref_binop(err, rhs_ty);
1307 }
1308 });
1309
1310 self.require_type_is_sized(lhs_ty, lhs.span, ObligationCauseCode::AssignmentLhsSized);
1311
1312 if let Err(guar) = (lhs_ty, rhs_ty).error_reported() {
1313 Ty::new_error(self.tcx, guar)
1314 } else {
1315 self.tcx.types.unit
1316 }
1317 }
1318
1319 fn expr_assign_expected_bool_error(
1323 &self,
1324 expr: &'tcx hir::Expr<'tcx>,
1325 lhs: &'tcx hir::Expr<'tcx>,
1326 rhs: &'tcx hir::Expr<'tcx>,
1327 span: Span,
1328 ) -> ErrorGuaranteed {
1329 let actual_ty = self.tcx.types.unit;
1330 let expected_ty = self.tcx.types.bool;
1331 let mut err = self.demand_suptype_diag(expr.span, expected_ty, actual_ty).unwrap_err();
1332 let lhs_ty = self.check_expr(lhs);
1333 let rhs_ty = self.check_expr(rhs);
1334 let refs_can_coerce = |lhs: Ty<'tcx>, rhs: Ty<'tcx>| {
1335 let lhs = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, lhs.peel_refs());
1336 let rhs = Ty::new_imm_ref(self.tcx, self.tcx.lifetimes.re_erased, rhs.peel_refs());
1337 self.may_coerce(rhs, lhs)
1338 };
1339 let (applicability, eq) = if self.may_coerce(rhs_ty, lhs_ty) {
1340 (Applicability::MachineApplicable, true)
1341 } else if refs_can_coerce(rhs_ty, lhs_ty) {
1342 (Applicability::MaybeIncorrect, true)
1345 } else if let ExprKind::Binary(
1346 Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
1347 _,
1348 rhs_expr,
1349 ) = lhs.kind
1350 {
1351 let actual_lhs = self.check_expr(rhs_expr);
1354 let may_eq = self.may_coerce(rhs_ty, actual_lhs) || refs_can_coerce(rhs_ty, actual_lhs);
1355 (Applicability::MaybeIncorrect, may_eq)
1356 } else if let ExprKind::Binary(
1357 Spanned { node: hir::BinOpKind::And | hir::BinOpKind::Or, .. },
1358 lhs_expr,
1359 _,
1360 ) = rhs.kind
1361 {
1362 let actual_rhs = self.check_expr(lhs_expr);
1365 let may_eq = self.may_coerce(actual_rhs, lhs_ty) || refs_can_coerce(actual_rhs, lhs_ty);
1366 (Applicability::MaybeIncorrect, may_eq)
1367 } else {
1368 (Applicability::MaybeIncorrect, false)
1369 };
1370
1371 if !lhs.is_syntactic_place_expr()
1372 && lhs.is_approximately_pattern()
1373 && !#[allow(non_exhaustive_omitted_patterns)] match lhs.kind {
hir::ExprKind::Lit(_) => true,
_ => false,
}matches!(lhs.kind, hir::ExprKind::Lit(_))
1374 {
1375 if let hir::Node::Expr(hir::Expr { kind: ExprKind::If { .. }, .. }) =
1377 self.tcx.parent_hir_node(expr.hir_id)
1378 {
1379 err.span_suggestion_verbose(
1380 expr.span.shrink_to_lo(),
1381 "you might have meant to use pattern matching",
1382 "let ",
1383 applicability,
1384 );
1385 };
1386 }
1387 if eq {
1388 err.span_suggestion_verbose(
1389 span.shrink_to_hi(),
1390 "you might have meant to compare for equality",
1391 '=',
1392 applicability,
1393 );
1394 }
1395
1396 err.emit_unless_delay(lhs_ty.references_error() || rhs_ty.references_error())
1399 }
1400
1401 pub(super) fn check_expr_let(
1402 &self,
1403 let_expr: &'tcx hir::LetExpr<'tcx>,
1404 hir_id: HirId,
1405 ) -> Ty<'tcx> {
1406 GatherLocalsVisitor::gather_from_let_expr(self, let_expr, hir_id);
1407
1408 let init = let_expr.init;
1410 self.warn_if_unreachable(init.hir_id, init.span, "block in `let` expression");
1411
1412 self.check_decl((let_expr, hir_id).into());
1414
1415 if let ast::Recovered::Yes(error_guaranteed) = let_expr.recovered {
1417 self.set_tainted_by_errors(error_guaranteed);
1418 Ty::new_error(self.tcx, error_guaranteed)
1419 } else {
1420 self.tcx.types.bool
1421 }
1422 }
1423
1424 fn check_expr_loop(
1425 &self,
1426 body: &'tcx hir::Block<'tcx>,
1427 source: hir::LoopSource,
1428 expected: Expectation<'tcx>,
1429 expr: &'tcx hir::Expr<'tcx>,
1430 ) -> Ty<'tcx> {
1431 let coerce = match source {
1432 hir::LoopSource::Loop => {
1434 let coerce_to = expected.coercion_target_type(self, body.span);
1435 Some(CoerceMany::new(coerce_to))
1436 }
1437
1438 hir::LoopSource::While | hir::LoopSource::ForLoop => None,
1439 };
1440
1441 let ctxt = BreakableCtxt {
1442 coerce,
1443 may_break: false, };
1445
1446 let (ctxt, ()) = self.with_breakable_ctxt(expr.hir_id, ctxt, || {
1447 self.check_block_no_value(body);
1448 });
1449
1450 if ctxt.may_break {
1451 self.diverges.set(Diverges::Maybe);
1454 } else {
1455 self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
1456 }
1457
1458 if ctxt.coerce.is_none() && !ctxt.may_break {
1464 self.dcx().span_bug(body.span, "no coercion, but loop may not break");
1465 }
1466 ctxt.coerce.map(|c| c.complete(self)).unwrap_or_else(|| self.tcx.types.unit)
1467 }
1468
1469 fn check_expr_method_call(
1471 &self,
1472 expr: &'tcx hir::Expr<'tcx>,
1473 segment: &'tcx hir::PathSegment<'tcx>,
1474 rcvr: &'tcx hir::Expr<'tcx>,
1475 args: &'tcx [hir::Expr<'tcx>],
1476 expected: Expectation<'tcx>,
1477 ) -> Ty<'tcx> {
1478 let rcvr_t = self.check_expr(rcvr);
1479 let rcvr_t = self.try_structurally_resolve_type(rcvr.span, rcvr_t);
1480
1481 match self.lookup_method(rcvr_t, segment, segment.ident.span, expr, rcvr, args) {
1482 Ok(method) => {
1483 self.write_method_call_and_enforce_effects(expr.hir_id, expr.span, method);
1484
1485 self.check_argument_types(
1486 segment.ident.span,
1487 expr,
1488 &method.sig.inputs()[1..],
1489 method.sig.output(),
1490 expected,
1491 args,
1492 method.sig.c_variadic(),
1493 TupleArgumentsFlag::DontTupleArguments,
1494 Some(method.def_id),
1495 );
1496
1497 self.check_call_abi(method.sig.abi(), expr.span);
1498
1499 method.sig.output()
1500 }
1501 Err(error) => {
1502 let guar = self.report_method_error(expr.hir_id, rcvr_t, error, expected, false);
1503
1504 let err_inputs = self.err_args(args.len(), guar);
1505 let err_output = Ty::new_error(self.tcx, guar);
1506
1507 self.check_argument_types(
1508 segment.ident.span,
1509 expr,
1510 &err_inputs,
1511 err_output,
1512 NoExpectation,
1513 args,
1514 false,
1515 TupleArgumentsFlag::DontTupleArguments,
1516 None,
1517 );
1518
1519 err_output
1520 }
1521 }
1522 }
1523
1524 fn check_expr_use(
1526 &self,
1527 used_expr: &'tcx hir::Expr<'tcx>,
1528 expected: Expectation<'tcx>,
1529 ) -> Ty<'tcx> {
1530 self.check_expr_with_expectation(used_expr, expected)
1531 }
1532
1533 fn check_expr_cast(
1534 &self,
1535 e: &'tcx hir::Expr<'tcx>,
1536 t: &'tcx hir::Ty<'tcx>,
1537 expr: &'tcx hir::Expr<'tcx>,
1538 ) -> Ty<'tcx> {
1539 let t_cast = self.lower_ty_saving_user_provided_ty(t);
1542 let t_cast = self.resolve_vars_if_possible(t_cast);
1543 let t_expr = self.check_expr_with_expectation(e, ExpectCastableToType(t_cast));
1544 let t_expr = self.resolve_vars_if_possible(t_expr);
1545
1546 if let Err(guar) = (t_expr, t_cast).error_reported() {
1548 Ty::new_error(self.tcx, guar)
1549 } else {
1550 let mut deferred_cast_checks = self.deferred_cast_checks.borrow_mut();
1552 match cast::CastCheck::new(self, e, t_expr, t_cast, t.span, expr.span) {
1553 Ok(cast_check) => {
1554 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:1554",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(1554u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("check_expr_cast: deferring cast from {0:?} to {1:?}: {2:?}",
t_cast, t_expr, cast_check) as &dyn Value))])
});
} else { ; }
};debug!(
1555 "check_expr_cast: deferring cast from {:?} to {:?}: {:?}",
1556 t_cast, t_expr, cast_check,
1557 );
1558 deferred_cast_checks.push(cast_check);
1559 t_cast
1560 }
1561 Err(guar) => Ty::new_error(self.tcx, guar),
1562 }
1563 }
1564 }
1565
1566 fn check_expr_unsafe_binder_cast(
1567 &self,
1568 span: Span,
1569 kind: ast::UnsafeBinderCastKind,
1570 inner_expr: &'tcx hir::Expr<'tcx>,
1571 hir_ty: Option<&'tcx hir::Ty<'tcx>>,
1572 expected: Expectation<'tcx>,
1573 ) -> Ty<'tcx> {
1574 match kind {
1575 ast::UnsafeBinderCastKind::Wrap => {
1576 let ascribed_ty =
1577 hir_ty.map(|hir_ty| self.lower_ty_saving_user_provided_ty(hir_ty));
1578 let expected_ty = expected.only_has_type(self);
1579 let binder_ty = match (ascribed_ty, expected_ty) {
1580 (Some(ascribed_ty), Some(expected_ty)) => {
1581 self.demand_eqtype(inner_expr.span, expected_ty, ascribed_ty);
1582 expected_ty
1583 }
1584 (Some(ty), None) | (None, Some(ty)) => ty,
1585 (None, None) => self.next_ty_var(inner_expr.span),
1589 };
1590
1591 let binder_ty = self.structurally_resolve_type(inner_expr.span, binder_ty);
1592 let hint_ty = match *binder_ty.kind() {
1593 ty::UnsafeBinder(binder) => self.instantiate_binder_with_fresh_vars(
1594 inner_expr.span,
1595 infer::BoundRegionConversionTime::HigherRankedType,
1596 binder.into(),
1597 ),
1598 ty::Error(e) => Ty::new_error(self.tcx, e),
1599 _ => {
1600 let guar = self
1601 .dcx()
1602 .struct_span_err(
1603 hir_ty.map_or(span, |hir_ty| hir_ty.span),
1604 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`wrap_binder!()` can only wrap into unsafe binder, not {0}",
binder_ty.sort_string(self.tcx)))
})format!(
1605 "`wrap_binder!()` can only wrap into unsafe binder, not {}",
1606 binder_ty.sort_string(self.tcx)
1607 ),
1608 )
1609 .with_note("unsafe binders are the only valid output of wrap")
1610 .emit();
1611 Ty::new_error(self.tcx, guar)
1612 }
1613 };
1614
1615 self.check_expr_has_type_or_error(inner_expr, hint_ty, |_| {});
1616
1617 binder_ty
1618 }
1619 ast::UnsafeBinderCastKind::Unwrap => {
1620 let ascribed_ty =
1621 hir_ty.map(|hir_ty| self.lower_ty_saving_user_provided_ty(hir_ty));
1622 let hint_ty = ascribed_ty.unwrap_or_else(|| self.next_ty_var(inner_expr.span));
1623 let binder_ty = self.check_expr_has_type_or_error(inner_expr, hint_ty, |_| {});
1625
1626 let binder_ty = self.structurally_resolve_type(inner_expr.span, binder_ty);
1629 match *binder_ty.kind() {
1630 ty::UnsafeBinder(binder) => self.instantiate_binder_with_fresh_vars(
1631 inner_expr.span,
1632 infer::BoundRegionConversionTime::HigherRankedType,
1633 binder.into(),
1634 ),
1635 ty::Error(e) => Ty::new_error(self.tcx, e),
1636 _ => {
1637 let guar = self
1638 .dcx()
1639 .struct_span_err(
1640 hir_ty.map_or(inner_expr.span, |hir_ty| hir_ty.span),
1641 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected unsafe binder, found {0} as input of `unwrap_binder!()`",
binder_ty.sort_string(self.tcx)))
})format!(
1642 "expected unsafe binder, found {} as input of \
1643 `unwrap_binder!()`",
1644 binder_ty.sort_string(self.tcx)
1645 ),
1646 )
1647 .with_note("only an unsafe binder type can be unwrapped")
1648 .emit();
1649 Ty::new_error(self.tcx, guar)
1650 }
1651 }
1652 }
1653 }
1654 }
1655
1656 fn check_expr_array(
1657 &self,
1658 args: &'tcx [hir::Expr<'tcx>],
1659 expected: Expectation<'tcx>,
1660 expr: &'tcx hir::Expr<'tcx>,
1661 ) -> Ty<'tcx> {
1662 let element_ty = if !args.is_empty() {
1663 let coerce_to = expected
1664 .to_option(self)
1665 .and_then(|uty| {
1666 self.try_structurally_resolve_type(expr.span, uty)
1667 .builtin_index()
1668 .filter(|t| !self.try_structurally_resolve_type(expr.span, *t).is_ty_var())
1671 })
1672 .unwrap_or_else(|| self.next_ty_var(expr.span));
1673 let mut coerce = CoerceMany::with_capacity(coerce_to, args.len());
1674
1675 for e in args {
1676 let e_ty = self.check_expr_with_hint(e, coerce_to);
1682 let cause = self.misc(e.span);
1683 coerce.coerce(self, &cause, e, e_ty);
1684 }
1685 coerce.complete(self)
1686 } else {
1687 self.next_ty_var(expr.span)
1688 };
1689 let array_len = args.len() as u64;
1690 self.suggest_array_len(expr, array_len);
1691 Ty::new_array(self.tcx, element_ty, array_len)
1692 }
1693
1694 fn suggest_array_len(&self, expr: &'tcx hir::Expr<'tcx>, array_len: u64) {
1695 let parent_node = self.tcx.hir_parent_iter(expr.hir_id).find(|(_, node)| {
1696 !#[allow(non_exhaustive_omitted_patterns)] match node {
hir::Node::Expr(hir::Expr { kind: hir::ExprKind::AddrOf(..), .. }) =>
true,
_ => false,
}matches!(node, hir::Node::Expr(hir::Expr { kind: hir::ExprKind::AddrOf(..), .. }))
1697 });
1698 let Some((_, hir::Node::LetStmt(hir::LetStmt { ty: Some(ty), .. }))) = parent_node else {
1699 return;
1700 };
1701 if let hir::TyKind::Array(_, ct) = ty.peel_refs().kind {
1702 let span = ct.span;
1703 self.dcx().try_steal_modify_and_emit_err(
1704 span,
1705 StashKey::UnderscoreForArrayLengths,
1706 |err| {
1707 err.span_suggestion(
1708 span,
1709 "consider specifying the array length",
1710 array_len,
1711 Applicability::MaybeIncorrect,
1712 );
1713 },
1714 );
1715 }
1716 }
1717
1718 pub(super) fn check_expr_const_block(
1719 &self,
1720 block: &'tcx hir::ConstBlock,
1721 expected: Expectation<'tcx>,
1722 ) -> Ty<'tcx> {
1723 let body = self.tcx.hir_body(block.body);
1724
1725 let def_id = block.def_id;
1727 let fcx = FnCtxt::new(self, self.param_env, def_id);
1728
1729 let ty = fcx.check_expr_with_expectation(body.value, expected);
1730 fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::SizedConstOrStatic);
1731 fcx.write_ty(block.hir_id, ty);
1732 ty
1733 }
1734
1735 fn check_expr_repeat(
1736 &self,
1737 element: &'tcx hir::Expr<'tcx>,
1738 count: &'tcx hir::ConstArg<'tcx>,
1739 expected: Expectation<'tcx>,
1740 expr: &'tcx hir::Expr<'tcx>,
1741 ) -> Ty<'tcx> {
1742 let tcx = self.tcx;
1743 let count_span = count.span;
1744 let count = self.try_structurally_resolve_const(
1745 count_span,
1746 self.normalize(
1747 count_span,
1748 Unnormalized::new_wip(self.lower_const_arg(count, tcx.types.usize)),
1749 ),
1750 );
1751
1752 if let Some(count) = count.try_to_target_usize(tcx) {
1753 self.suggest_array_len(expr, count);
1754 }
1755
1756 let uty = match expected {
1757 ExpectHasType(uty) => uty.builtin_index(),
1758 _ => None,
1759 };
1760
1761 let (element_ty, t) = match uty {
1762 Some(uty) => {
1763 self.check_expr_coercible_to_type(element, uty, None);
1764 (uty, uty)
1765 }
1766 None => {
1767 let ty = self.next_ty_var(element.span);
1768 let element_ty = self.check_expr_has_type_or_error(element, ty, |_| {});
1769 (element_ty, ty)
1770 }
1771 };
1772
1773 if let Err(guar) = element_ty.error_reported() {
1774 return Ty::new_error(tcx, guar);
1775 }
1776
1777 self.deferred_repeat_expr_checks.borrow_mut().push((element, element_ty, count));
1781
1782 let ty = Ty::new_array_with_const_len(tcx, t, count);
1783 self.register_wf_obligation(ty.into(), expr.span, ObligationCauseCode::WellFormed(None));
1784 ty
1785 }
1786
1787 fn check_expr_tuple(
1788 &self,
1789 elements: &'tcx [hir::Expr<'tcx>],
1790 expected: Expectation<'tcx>,
1791 expr: &'tcx hir::Expr<'tcx>,
1792 ) -> Ty<'tcx> {
1793 let mut expectations = expected
1794 .only_has_type(self)
1795 .and_then(|ty| self.try_structurally_resolve_type(expr.span, ty).opt_tuple_fields())
1796 .unwrap_or_default()
1797 .iter();
1798
1799 let elements = elements.iter().map(|e| {
1800 let ty = expectations.next().unwrap_or_else(|| self.next_ty_var(e.span));
1801 self.check_expr_coercible_to_type(e, ty, None);
1802 ty
1803 });
1804
1805 let tuple = Ty::new_tup_from_iter(self.tcx, elements);
1806
1807 if let Err(guar) = tuple.error_reported() {
1808 Ty::new_error(self.tcx, guar)
1809 } else {
1810 self.require_type_is_sized(
1811 tuple,
1812 expr.span,
1813 ObligationCauseCode::TupleInitializerSized,
1814 );
1815 tuple
1816 }
1817 }
1818
1819 fn check_expr_struct(
1820 &self,
1821 expr: &hir::Expr<'tcx>,
1822 expected: Expectation<'tcx>,
1823 qpath: &'tcx QPath<'tcx>,
1824 fields: &'tcx [hir::ExprField<'tcx>],
1825 base_expr: &'tcx hir::StructTailExpr<'tcx>,
1826 ) -> Ty<'tcx> {
1827 let (variant, adt_ty) = match self.check_struct_path(qpath, expr.hir_id) {
1829 Ok(data) => data,
1830 Err(guar) => {
1831 self.check_struct_fields_on_error(fields, base_expr);
1832 return Ty::new_error(self.tcx, guar);
1833 }
1834 };
1835
1836 let adt = adt_ty.ty_adt_def().expect("`check_struct_path` returned non-ADT type");
1838 if variant.field_list_has_applicable_non_exhaustive() {
1839 self.dcx()
1840 .emit_err(StructExprNonExhaustive { span: expr.span, what: adt.variant_descr() });
1841 }
1842
1843 self.check_expr_struct_fields(
1844 adt_ty,
1845 expected,
1846 expr,
1847 qpath.span(),
1848 variant,
1849 fields,
1850 base_expr,
1851 );
1852
1853 self.require_type_is_sized(adt_ty, expr.span, ObligationCauseCode::StructInitializerSized);
1854 adt_ty
1855 }
1856
1857 fn check_expr_struct_fields(
1858 &self,
1859 adt_ty: Ty<'tcx>,
1860 expected: Expectation<'tcx>,
1861 expr: &hir::Expr<'_>,
1862 path_span: Span,
1863 variant: &'tcx ty::VariantDef,
1864 hir_fields: &'tcx [hir::ExprField<'tcx>],
1865 base_expr: &'tcx hir::StructTailExpr<'tcx>,
1866 ) {
1867 let tcx = self.tcx;
1868
1869 let adt_ty = self.try_structurally_resolve_type(path_span, adt_ty);
1870 let adt_ty_hint = expected.only_has_type(self).and_then(|expected| {
1871 self.fudge_inference_if_ok(|| {
1872 let ocx = ObligationCtxt::new(self);
1873 ocx.sup(&self.misc(path_span), self.param_env, expected, adt_ty)?;
1874 if !ocx.try_evaluate_obligations().is_empty() {
1875 return Err(TypeError::Mismatch);
1876 }
1877 Ok(self.resolve_vars_if_possible(adt_ty))
1878 })
1879 .ok()
1880 });
1881 if let Some(adt_ty_hint) = adt_ty_hint {
1882 self.demand_eqtype(path_span, adt_ty_hint, adt_ty);
1884 }
1885
1886 let ty::Adt(adt, args) = adt_ty.kind() else {
1887 ::rustc_middle::util::bug::span_bug_fmt(path_span,
format_args!("non-ADT passed to check_expr_struct_fields"));span_bug!(path_span, "non-ADT passed to check_expr_struct_fields");
1888 };
1889 let adt_kind = adt.adt_kind();
1890
1891 let mut remaining_fields = variant
1892 .fields
1893 .iter_enumerated()
1894 .map(|(i, field)| (field.ident(tcx).normalize_to_macros_2_0(), (i, field)))
1895 .collect::<UnordMap<_, _>>();
1896
1897 let mut seen_fields = FxHashMap::default();
1898
1899 let mut error_happened = false;
1900
1901 if variant.fields.len() != remaining_fields.len() {
1902 let guar =
1905 self.dcx().span_delayed_bug(expr.span, "struct fields have non-unique names");
1906 self.set_tainted_by_errors(guar);
1907 error_happened = true;
1908 }
1909
1910 for (idx, field) in hir_fields.iter().enumerate() {
1912 let ident = tcx.adjust_ident(field.ident, variant.def_id);
1913 let field_type = if let Some((i, v_field)) = remaining_fields.remove(&ident) {
1914 seen_fields.insert(ident, field.span);
1915 self.write_field_index(field.hir_id, i);
1916
1917 if adt_kind != AdtKind::Enum {
1921 tcx.check_stability(v_field.did, Some(field.hir_id), field.span, None);
1922 }
1923
1924 self.field_ty(field.span, v_field, args)
1925 } else {
1926 error_happened = true;
1927 let guar = if let Some(prev_span) = seen_fields.get(&ident) {
1928 self.dcx().emit_err(FieldMultiplySpecifiedInInitializer {
1929 span: field.ident.span,
1930 prev_span: *prev_span,
1931 ident,
1932 })
1933 } else {
1934 self.report_unknown_field(
1935 adt_ty,
1936 variant,
1937 expr,
1938 field,
1939 hir_fields,
1940 adt.variant_descr(),
1941 )
1942 };
1943
1944 Ty::new_error(tcx, guar)
1945 };
1946
1947 self.register_wf_obligation(
1951 field_type.into(),
1952 field.expr.span,
1953 ObligationCauseCode::WellFormed(None),
1954 );
1955
1956 let ty = self.check_expr_with_hint(field.expr, field_type);
1959 let diag = self.demand_coerce_diag(field.expr, ty, field_type, None, AllowTwoPhase::No);
1960
1961 if let Err(diag) = diag {
1962 if idx == hir_fields.len() - 1 {
1963 if remaining_fields.is_empty() {
1964 self.suggest_fru_from_range_and_emit(field, variant, args, diag);
1965 } else {
1966 diag.stash(field.span, StashKey::MaybeFruTypo);
1967 }
1968 } else {
1969 diag.emit();
1970 }
1971 }
1972 }
1973
1974 if adt_kind == AdtKind::Union && hir_fields.len() != 1 {
1976 {
self.dcx().struct_span_err(path_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("union expressions should have exactly one field"))
})).with_code(E0784)
}struct_span_code_err!(
1977 self.dcx(),
1978 path_span,
1979 E0784,
1980 "union expressions should have exactly one field",
1981 )
1982 .emit();
1983 }
1984
1985 if error_happened {
1989 if let hir::StructTailExpr::Base(base_expr) = base_expr {
1990 self.check_expr(base_expr);
1991 }
1992 return;
1993 }
1994
1995 match *base_expr {
1996 hir::StructTailExpr::DefaultFields(span) => {
1997 let mut missing_mandatory_fields = Vec::new();
1998 let mut missing_optional_fields = Vec::new();
1999 for f in &variant.fields {
2000 let ident = self.tcx.adjust_ident(f.ident(self.tcx), variant.def_id);
2001 if let Some(_) = remaining_fields.remove(&ident) {
2002 if f.value.is_none() {
2003 missing_mandatory_fields.push(ident);
2004 } else {
2005 missing_optional_fields.push(ident);
2006 }
2007 }
2008 }
2009 if !self.tcx.features().default_field_values() {
2010 let sugg = self.tcx.crate_level_attribute_injection_span();
2011 self.dcx().emit_err(BaseExpressionDoubleDot {
2012 span: span.shrink_to_hi(),
2013 default_field_values_suggestion: if self.tcx.sess.is_nightly_build()
2016 && missing_mandatory_fields.is_empty()
2017 && !missing_optional_fields.is_empty()
2018 {
2019 Some(sugg)
2020 } else {
2021 None
2022 },
2023 add_expr: if !missing_mandatory_fields.is_empty()
2024 || !missing_optional_fields.is_empty()
2025 {
2026 Some(BaseExpressionDoubleDotAddExpr { span: span.shrink_to_hi() })
2027 } else {
2028 None
2029 },
2030 remove_dots: if missing_mandatory_fields.is_empty()
2031 && missing_optional_fields.is_empty()
2032 {
2033 Some(BaseExpressionDoubleDotRemove { span })
2034 } else {
2035 None
2036 },
2037 });
2038 return;
2039 }
2040 if variant.fields.is_empty() {
2041 let mut err = self.dcx().struct_span_err(
2042 span,
2043 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` has no fields, `..` needs at least one default field in the struct definition",
adt_ty))
})format!(
2044 "`{adt_ty}` has no fields, `..` needs at least one default field in \
2045 the struct definition",
2046 ),
2047 );
2048 err.span_label(path_span, "this type has no fields");
2049 err.emit();
2050 }
2051 if !missing_mandatory_fields.is_empty() {
2052 let s = if missing_mandatory_fields.len() == 1 { "" } else { "s" }pluralize!(missing_mandatory_fields.len());
2053 let fields = listify(&missing_mandatory_fields, |f| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", f))
})format!("`{f}`")).unwrap();
2054 self.dcx()
2055 .struct_span_err(
2056 span.shrink_to_lo(),
2057 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("missing field{0} {1} in initializer",
s, fields))
})format!("missing field{s} {fields} in initializer"),
2058 )
2059 .with_span_label(
2060 span.shrink_to_lo(),
2061 "fields that do not have a defaulted value must be provided explicitly",
2062 )
2063 .emit();
2064 return;
2065 }
2066 let fru_tys = match adt_ty.kind() {
2067 ty::Adt(adt, args) if adt.is_struct() => variant
2068 .fields
2069 .iter()
2070 .map(|f| self.normalize(span, Unnormalized::new_wip(f.ty(self.tcx, args))))
2071 .collect(),
2072 ty::Adt(adt, args) if adt.is_enum() => variant
2073 .fields
2074 .iter()
2075 .map(|f| self.normalize(span, Unnormalized::new_wip(f.ty(self.tcx, args))))
2076 .collect(),
2077 _ => {
2078 self.dcx().emit_err(FunctionalRecordUpdateOnNonStruct { span });
2079 return;
2080 }
2081 };
2082 self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
2083 }
2084 hir::StructTailExpr::Base(base_expr) => {
2085 let fru_tys = if self.tcx.features().type_changing_struct_update() {
2088 if adt.is_struct() {
2089 let fresh_args = self.fresh_args_for_item(base_expr.span, adt.did());
2091 let fru_tys = variant
2096 .fields
2097 .iter()
2098 .map(|f| {
2099 let fru_ty = self.normalize(
2100 expr.span,
2101 Unnormalized::new_wip(self.field_ty(
2102 base_expr.span,
2103 f,
2104 fresh_args,
2105 )),
2106 );
2107 let ident =
2108 self.tcx.adjust_ident(f.ident(self.tcx), variant.def_id);
2109 if let Some(_) = remaining_fields.remove(&ident) {
2110 let target_ty = self.field_ty(base_expr.span, f, args);
2111 let cause = self.misc(base_expr.span);
2112 match self.at(&cause, self.param_env).sup(
2113 DefineOpaqueTypes::Yes,
2118 target_ty,
2119 fru_ty,
2120 ) {
2121 Ok(InferOk { obligations, value: () }) => {
2122 self.register_predicates(obligations)
2123 }
2124 Err(_) => {
2125 ::rustc_middle::util::bug::span_bug_fmt(cause.span,
format_args!("subtyping remaining fields of type changing FRU failed: {2} != {3}: {0}::{1}",
variant.name, ident.name, target_ty, fru_ty));span_bug!(
2126 cause.span,
2127 "subtyping remaining fields of type changing FRU \
2128 failed: {target_ty} != {fru_ty}: {}::{}",
2129 variant.name,
2130 ident.name,
2131 );
2132 }
2133 }
2134 }
2135 self.resolve_vars_if_possible(fru_ty)
2136 })
2137 .collect();
2138 let fresh_base_ty = Ty::new_adt(self.tcx, *adt, fresh_args);
2157 self.check_expr_has_type_or_error(
2158 base_expr,
2159 self.resolve_vars_if_possible(fresh_base_ty),
2160 |_| {},
2161 );
2162 fru_tys
2163 } else {
2164 self.check_expr(base_expr);
2167 self.dcx()
2168 .emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span });
2169 return;
2170 }
2171 } else {
2172 self.check_expr_has_type_or_error(base_expr, adt_ty, |_| {
2173 let base_ty = self.typeck_results.borrow().expr_ty(base_expr);
2174 let same_adt = #[allow(non_exhaustive_omitted_patterns)] match (adt_ty.kind(),
base_ty.kind()) {
(ty::Adt(adt, _), ty::Adt(base_adt, _)) if adt == base_adt => true,
_ => false,
}matches!((adt_ty.kind(), base_ty.kind()),
2175 (ty::Adt(adt, _), ty::Adt(base_adt, _)) if adt == base_adt);
2176 if self.tcx.sess.is_nightly_build() && same_adt {
2177 feature_err(
2178 &self.tcx.sess,
2179 sym::type_changing_struct_update,
2180 base_expr.span,
2181 "type changing struct updating is experimental",
2182 )
2183 .emit();
2184 }
2185 });
2186 match adt_ty.kind() {
2187 ty::Adt(adt, args) if adt.is_struct() => variant
2188 .fields
2189 .iter()
2190 .map(|f| {
2191 self.normalize(
2192 expr.span,
2193 Unnormalized::new_wip(f.ty(self.tcx, args)),
2194 )
2195 })
2196 .collect(),
2197 _ => {
2198 self.dcx().emit_err(FunctionalRecordUpdateOnNonStruct {
2199 span: base_expr.span,
2200 });
2201 return;
2202 }
2203 }
2204 };
2205 self.typeck_results.borrow_mut().fru_field_types_mut().insert(expr.hir_id, fru_tys);
2206 }
2207 rustc_hir::StructTailExpr::NoneWithError(guaranteed) => {
2208 self.infcx.set_tainted_by_errors(guaranteed);
2219 }
2220 rustc_hir::StructTailExpr::None => {
2221 if adt_kind != AdtKind::Union
2222 && !remaining_fields.is_empty()
2223 && !variant.field_list_has_applicable_non_exhaustive()
2225 {
2226 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:2226",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(2226u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["remaining_fields"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&remaining_fields)
as &dyn Value))])
});
} else { ; }
};debug!(?remaining_fields);
2227
2228 let private_fields: Vec<&ty::FieldDef> = variant
2231 .fields
2232 .iter()
2233 .filter(|field| {
2234 !field.vis.is_accessible_from(tcx.parent_module(expr.hir_id), tcx)
2235 })
2236 .collect();
2237
2238 if !private_fields.is_empty() {
2239 self.report_private_fields(
2240 adt_ty,
2241 path_span,
2242 expr.span,
2243 private_fields,
2244 hir_fields,
2245 );
2246 } else {
2247 self.report_missing_fields(
2248 adt_ty,
2249 path_span,
2250 expr.span,
2251 remaining_fields,
2252 variant,
2253 hir_fields,
2254 args,
2255 );
2256 }
2257 }
2258 }
2259 }
2260 }
2261
2262 fn check_struct_fields_on_error(
2263 &self,
2264 fields: &'tcx [hir::ExprField<'tcx>],
2265 base_expr: &'tcx hir::StructTailExpr<'tcx>,
2266 ) {
2267 for field in fields {
2268 self.check_expr(field.expr);
2269 }
2270 if let hir::StructTailExpr::Base(base) = *base_expr {
2271 self.check_expr(base);
2272 }
2273 }
2274
2275 fn report_missing_fields(
2287 &self,
2288 adt_ty: Ty<'tcx>,
2289 span: Span,
2290 full_span: Span,
2291 remaining_fields: UnordMap<Ident, (FieldIdx, &ty::FieldDef)>,
2292 variant: &'tcx ty::VariantDef,
2293 hir_fields: &'tcx [hir::ExprField<'tcx>],
2294 args: GenericArgsRef<'tcx>,
2295 ) {
2296 let len = remaining_fields.len();
2297
2298 let displayable_field_names: Vec<&str> =
2299 remaining_fields.items().map(|(ident, _)| ident.as_str()).into_sorted_stable_ord();
2300
2301 let mut truncated_fields_error = String::new();
2302 let remaining_fields_names = match &displayable_field_names[..] {
2303 [field1] => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", field1))
})format!("`{field1}`"),
2304 [field1, field2] => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` and `{1}`", field1, field2))
})format!("`{field1}` and `{field2}`"),
2305 [field1, field2, field3] => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`, `{1}` and `{2}`", field1,
field2, field3))
})format!("`{field1}`, `{field2}` and `{field3}`"),
2306 _ => {
2307 truncated_fields_error =
2308 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" and {0} other field{1}", len - 3,
if len - 3 == 1 { "" } else { "s" }))
})format!(" and {} other field{}", len - 3, pluralize!(len - 3));
2309 displayable_field_names
2310 .iter()
2311 .take(3)
2312 .map(|n| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", n))
})format!("`{n}`"))
2313 .collect::<Vec<_>>()
2314 .join(", ")
2315 }
2316 };
2317
2318 let mut err = {
self.dcx().struct_span_err(span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("missing field{0} {1}{2} in initializer of `{3}`",
if len == 1 { "" } else { "s" }, remaining_fields_names,
truncated_fields_error, adt_ty))
})).with_code(E0063)
}struct_span_code_err!(
2319 self.dcx(),
2320 span,
2321 E0063,
2322 "missing field{} {}{} in initializer of `{}`",
2323 pluralize!(len),
2324 remaining_fields_names,
2325 truncated_fields_error,
2326 adt_ty
2327 );
2328 err.span_label(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("missing {0}{1}",
remaining_fields_names, truncated_fields_error))
})format!("missing {remaining_fields_names}{truncated_fields_error}"));
2329
2330 if remaining_fields.items().all(|(_, (_, field))| field.value.is_some())
2331 && self.tcx.sess.is_nightly_build()
2332 {
2333 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("all remaining fields have default values, {0} use those values with `..`",
if self.tcx.features().default_field_values() {
"you can"
} else {
"if you added `#![feature(default_field_values)]` to your crate you could"
}))
})format!(
2334 "all remaining fields have default values, {you_can} use those values with `..`",
2335 you_can = if self.tcx.features().default_field_values() {
2336 "you can"
2337 } else {
2338 "if you added `#![feature(default_field_values)]` to your crate you could"
2339 },
2340 );
2341 if let Some(hir_field) = hir_fields.last() {
2342 err.span_suggestion_verbose(
2343 hir_field.span.shrink_to_hi(),
2344 msg,
2345 ", ..".to_string(),
2346 Applicability::MachineApplicable,
2347 );
2348 } else if hir_fields.is_empty() {
2349 err.span_suggestion_verbose(
2350 span.shrink_to_hi().with_hi(full_span.hi()),
2351 msg,
2352 " { .. }".to_string(),
2353 Applicability::MachineApplicable,
2354 );
2355 }
2356 }
2357
2358 if let Some(hir_field) = hir_fields.last() {
2359 self.suggest_fru_from_range_and_emit(hir_field, variant, args, err);
2360 } else {
2361 err.emit();
2362 }
2363 }
2364
2365 fn suggest_fru_from_range_and_emit(
2368 &self,
2369 last_expr_field: &hir::ExprField<'tcx>,
2370 variant: &ty::VariantDef,
2371 args: GenericArgsRef<'tcx>,
2372 mut err: Diag<'_>,
2373 ) {
2374 if is_range_literal(last_expr_field.expr)
2375 && let ExprKind::Struct(&qpath, [range_start, range_end], _) = last_expr_field.expr.kind
2376 && self.tcx.qpath_is_lang_item(qpath, LangItem::Range)
2377 && let variant_field =
2378 variant.fields.iter().find(|field| field.ident(self.tcx) == last_expr_field.ident)
2379 && let range_def_id = self.tcx.lang_items().range_struct()
2380 && variant_field
2381 .and_then(|field| field.ty(self.tcx, args).ty_adt_def())
2382 .map(|adt| adt.did())
2383 != range_def_id
2384 {
2385 let expr = self
2389 .tcx
2390 .sess
2391 .source_map()
2392 .span_to_snippet(range_end.expr.span)
2393 .ok()
2394 .filter(|s| s.len() < 25 && !s.contains(|c: char| c.is_control()));
2395
2396 let fru_span = self
2397 .tcx
2398 .sess
2399 .source_map()
2400 .span_extend_while_whitespace(range_start.expr.span)
2401 .shrink_to_hi()
2402 .to(range_end.expr.span);
2403
2404 err.subdiagnostic(TypeMismatchFruTypo {
2405 expr_span: range_start.expr.span,
2406 fru_span,
2407 expr,
2408 });
2409
2410 self.dcx().try_steal_replace_and_emit_err(
2412 last_expr_field.span,
2413 StashKey::MaybeFruTypo,
2414 err,
2415 );
2416 } else {
2417 err.emit();
2418 }
2419 }
2420
2421 fn report_private_fields(
2433 &self,
2434 adt_ty: Ty<'tcx>,
2435 span: Span,
2436 expr_span: Span,
2437 private_fields: Vec<&ty::FieldDef>,
2438 used_fields: &'tcx [hir::ExprField<'tcx>],
2439 ) {
2440 let mut err =
2441 self.dcx().struct_span_err(
2442 span,
2443 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot construct `{0}` with struct literal syntax due to private fields",
adt_ty))
})format!(
2444 "cannot construct `{adt_ty}` with struct literal syntax due to private fields",
2445 ),
2446 );
2447 let (used_private_fields, remaining_private_fields): (
2448 Vec<(Symbol, Span, bool)>,
2449 Vec<(Symbol, Span, bool)>,
2450 ) = private_fields
2451 .iter()
2452 .map(|field| {
2453 match used_fields.iter().find(|used_field| field.name == used_field.ident.name) {
2454 Some(used_field) => (field.name, used_field.span, true),
2455 None => (field.name, self.tcx.def_span(field.did), false),
2456 }
2457 })
2458 .partition(|field| field.2);
2459 err.span_labels(used_private_fields.iter().map(|(_, span, _)| *span), "private field");
2460
2461 if let ty::Adt(def, _) = adt_ty.kind() {
2462 if (def.did().is_local() || !used_fields.is_empty())
2463 && !remaining_private_fields.is_empty()
2464 {
2465 let names = if remaining_private_fields.len() > 6 {
2466 String::new()
2467 } else {
2468 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} ",
listify(&remaining_private_fields,
|(name, _, _)|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", name))
})).expect("expected at least one private field to report")))
})format!(
2469 "{} ",
2470 listify(&remaining_private_fields, |(name, _, _)| format!("`{name}`"))
2471 .expect("expected at least one private field to report")
2472 )
2473 };
2474 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}private field{1} {3}that {2} not provided",
if used_fields.is_empty() { "" } else { "...and other " },
if remaining_private_fields.len() == 1 { "" } else { "s" },
if remaining_private_fields.len() == 1 {
"was"
} else { "were" }, names))
})format!(
2475 "{}private field{s} {names}that {were} not provided",
2476 if used_fields.is_empty() { "" } else { "...and other " },
2477 s = pluralize!(remaining_private_fields.len()),
2478 were = pluralize!("was", remaining_private_fields.len()),
2479 ));
2480 }
2481
2482 let def_id = def.did();
2483 let mut items = self
2484 .tcx
2485 .inherent_impls(def_id)
2486 .into_iter()
2487 .flat_map(|&i| self.tcx.associated_items(i).in_definition_order())
2488 .filter(|item| item.is_fn() && !item.is_method())
2490 .filter_map(|item| {
2491 let fn_sig = self
2493 .tcx
2494 .fn_sig(item.def_id)
2495 .instantiate(self.tcx, self.fresh_args_for_item(span, item.def_id))
2496 .skip_norm_wip();
2497 let ret_ty = self.tcx.instantiate_bound_regions_with_erased(fn_sig.output());
2498 if !self.can_eq(self.param_env, ret_ty, adt_ty) {
2499 return None;
2500 }
2501 let input_len = fn_sig.inputs().skip_binder().len();
2502 let name = item.name();
2503 let order = !name.as_str().starts_with("new");
2504 Some((order, name, input_len))
2505 })
2506 .collect::<Vec<_>>();
2507 items.sort_by_key(|(order, _, _)| *order);
2508 let suggestion = |name, args| {
2509 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("::{1}({0})",
std::iter::repeat_n("_", args).collect::<Vec<_>>().join(", "),
name))
})format!(
2510 "::{name}({})",
2511 std::iter::repeat_n("_", args).collect::<Vec<_>>().join(", ")
2512 )
2513 };
2514 match &items[..] {
2515 [] => {}
2516 [(_, name, args)] => {
2517 err.span_suggestion_verbose(
2518 span.shrink_to_hi().with_hi(expr_span.hi()),
2519 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("you might have meant to use the `{0}` associated function",
name))
})format!("you might have meant to use the `{name}` associated function"),
2520 suggestion(name, *args),
2521 Applicability::MaybeIncorrect,
2522 );
2523 }
2524 _ => {
2525 err.span_suggestions(
2526 span.shrink_to_hi().with_hi(expr_span.hi()),
2527 "you might have meant to use an associated function to build this type",
2528 items.iter().map(|(_, name, args)| suggestion(name, *args)),
2529 Applicability::MaybeIncorrect,
2530 );
2531 }
2532 }
2533 if let Some(default_trait) = self.tcx.get_diagnostic_item(sym::Default)
2534 && self
2535 .infcx
2536 .type_implements_trait(default_trait, [adt_ty], self.param_env)
2537 .may_apply()
2538 {
2539 err.multipart_suggestion(
2540 "consider using the `Default` trait",
2541 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(span.shrink_to_lo(), "<".to_string()),
(span.shrink_to_hi().with_hi(expr_span.hi()),
" as std::default::Default>::default()".to_string())]))vec![
2542 (span.shrink_to_lo(), "<".to_string()),
2543 (
2544 span.shrink_to_hi().with_hi(expr_span.hi()),
2545 " as std::default::Default>::default()".to_string(),
2546 ),
2547 ],
2548 Applicability::MaybeIncorrect,
2549 );
2550 }
2551 }
2552
2553 err.emit();
2554 }
2555
2556 fn report_unknown_field(
2557 &self,
2558 ty: Ty<'tcx>,
2559 variant: &'tcx ty::VariantDef,
2560 expr: &hir::Expr<'_>,
2561 field: &hir::ExprField<'_>,
2562 skip_fields: &[hir::ExprField<'_>],
2563 kind_name: &str,
2564 ) -> ErrorGuaranteed {
2565 if let Err(guar) = variant.has_errors() {
2567 return guar;
2568 }
2569 let mut err = self.err_ctxt().type_error_struct_with_diag(
2570 field.ident.span,
2571 |actual| match ty.kind() {
2572 ty::Adt(adt, ..) if adt.is_enum() => {
self.dcx().struct_span_err(field.ident.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} `{1}::{2}` has no field named `{3}`",
kind_name, actual, variant.name, field.ident))
})).with_code(E0559)
}struct_span_code_err!(
2573 self.dcx(),
2574 field.ident.span,
2575 E0559,
2576 "{} `{}::{}` has no field named `{}`",
2577 kind_name,
2578 actual,
2579 variant.name,
2580 field.ident
2581 ),
2582 _ => {
self.dcx().struct_span_err(field.ident.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} `{1}` has no field named `{2}`",
kind_name, actual, field.ident))
})).with_code(E0560)
}struct_span_code_err!(
2583 self.dcx(),
2584 field.ident.span,
2585 E0560,
2586 "{} `{}` has no field named `{}`",
2587 kind_name,
2588 actual,
2589 field.ident
2590 ),
2591 },
2592 ty,
2593 );
2594
2595 let variant_ident_span = self.tcx.def_ident_span(variant.def_id).unwrap();
2596 match variant.ctor {
2597 Some((CtorKind::Fn, def_id)) => match ty.kind() {
2598 ty::Adt(adt, ..) if adt.is_enum() => {
2599 err.span_label(
2600 variant_ident_span,
2601 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}::{1}` defined here", ty,
variant.name))
})format!(
2602 "`{adt}::{variant}` defined here",
2603 adt = ty,
2604 variant = variant.name,
2605 ),
2606 );
2607 err.span_label(field.ident.span, "field does not exist");
2608 let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip();
2609 let inputs = fn_sig.inputs().skip_binder();
2610 let fields = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0})",
inputs.iter().map(|i|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/* {0} */", i))
})).collect::<Vec<_>>().join(", ")))
})format!(
2611 "({})",
2612 inputs.iter().map(|i| format!("/* {i} */")).collect::<Vec<_>>().join(", ")
2613 );
2614 let (replace_span, sugg) = match expr.kind {
2615 hir::ExprKind::Struct(qpath, ..) => {
2616 (qpath.span().shrink_to_hi().with_hi(expr.span.hi()), fields)
2617 }
2618 _ => {
2619 (expr.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}::{0}{2}", variant.name, ty,
fields))
})format!("{ty}::{variant}{fields}", variant = variant.name))
2620 }
2621 };
2622 err.span_suggestion_verbose(
2623 replace_span,
2624 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}::{1}` is a tuple {2}, use the appropriate syntax",
ty, variant.name, kind_name))
})format!(
2625 "`{adt}::{variant}` is a tuple {kind_name}, use the appropriate syntax",
2626 adt = ty,
2627 variant = variant.name,
2628 ),
2629 sugg,
2630 Applicability::HasPlaceholders,
2631 );
2632 }
2633 _ => {
2634 err.span_label(variant_ident_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` defined here", ty))
})format!("`{ty}` defined here"));
2635 err.span_label(field.ident.span, "field does not exist");
2636 let fn_sig = self.tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip();
2637 let inputs = fn_sig.inputs().skip_binder();
2638 let fields = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0})",
inputs.iter().map(|i|
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("/* {0} */", i))
})).collect::<Vec<_>>().join(", ")))
})format!(
2639 "({})",
2640 inputs.iter().map(|i| format!("/* {i} */")).collect::<Vec<_>>().join(", ")
2641 );
2642 err.span_suggestion_verbose(
2643 expr.span,
2644 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is a tuple {1}, use the appropriate syntax",
ty, kind_name))
})format!("`{ty}` is a tuple {kind_name}, use the appropriate syntax",),
2645 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", ty, fields))
})format!("{ty}{fields}"),
2646 Applicability::HasPlaceholders,
2647 );
2648 }
2649 },
2650 _ => {
2651 let available_field_names = self.available_field_names(variant, expr, skip_fields);
2653 if let Some(field_name) =
2654 find_best_match_for_name(&available_field_names, field.ident.name, None)
2655 && !(field.ident.name.as_str().parse::<usize>().is_ok()
2656 && field_name.as_str().parse::<usize>().is_ok())
2657 {
2658 err.span_label(field.ident.span, "unknown field");
2659 err.span_suggestion_verbose(
2660 field.ident.span,
2661 "a field with a similar name exists",
2662 field_name,
2663 Applicability::MaybeIncorrect,
2664 );
2665 } else {
2666 match ty.kind() {
2667 ty::Adt(adt, ..) => {
2668 if adt.is_enum() {
2669 err.span_label(
2670 field.ident.span,
2671 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}::{1}` does not have this field",
ty, variant.name))
})format!("`{}::{}` does not have this field", ty, variant.name),
2672 );
2673 } else {
2674 err.span_label(
2675 field.ident.span,
2676 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` does not have this field",
ty))
})format!("`{ty}` does not have this field"),
2677 );
2678 }
2679 if available_field_names.is_empty() {
2680 err.note("all struct fields are already assigned");
2681 } else {
2682 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("available fields are: {0}",
self.name_series_display(available_field_names)))
})format!(
2683 "available fields are: {}",
2684 self.name_series_display(available_field_names)
2685 ));
2686 }
2687 }
2688 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("non-ADT passed to report_unknown_field"))bug!("non-ADT passed to report_unknown_field"),
2689 }
2690 };
2691 }
2692 }
2693 err.emit()
2694 }
2695
2696 fn available_field_names(
2697 &self,
2698 variant: &'tcx ty::VariantDef,
2699 expr: &hir::Expr<'_>,
2700 skip_fields: &[hir::ExprField<'_>],
2701 ) -> Vec<Symbol> {
2702 variant
2703 .fields
2704 .iter()
2705 .filter(|field| {
2706 skip_fields.iter().all(|&skip| skip.ident.name != field.name)
2707 && self.is_field_suggestable(field, expr.hir_id, expr.span)
2708 })
2709 .map(|field| field.name)
2710 .collect()
2711 }
2712
2713 fn name_series_display(&self, names: Vec<Symbol>) -> String {
2714 let limit = if names.len() == 6 { 6 } else { 5 };
2716 let mut display =
2717 names.iter().take(limit).map(|n| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", n))
})format!("`{n}`")).collect::<Vec<_>>().join(", ");
2718 if names.len() > limit {
2719 display = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} ... and {1} others", display,
names.len() - limit))
})format!("{} ... and {} others", display, names.len() - limit);
2720 }
2721 display
2722 }
2723
2724 fn find_adt_field(
2728 &self,
2729 base_def: ty::AdtDef<'tcx>,
2730 ident: Ident,
2731 ) -> Option<(FieldIdx, &'tcx ty::FieldDef)> {
2732 if base_def.is_enum() {
2734 return None;
2735 }
2736
2737 for (field_idx, field) in base_def.non_enum_variant().fields.iter_enumerated() {
2738 if field.ident(self.tcx).normalize_to_macros_2_0() == ident {
2739 return Some((field_idx, field));
2741 }
2742 }
2743
2744 None
2745 }
2746
2747 fn check_expr_field(
2757 &self,
2758 expr: &'tcx hir::Expr<'tcx>,
2759 base: &'tcx hir::Expr<'tcx>,
2760 field: Ident,
2761 expected: Expectation<'tcx>,
2763 ) -> Ty<'tcx> {
2764 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:2764",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(2764u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("check_field(expr: {0:?}, base: {1:?}, field: {2:?})",
expr, base, field) as &dyn Value))])
});
} else { ; }
};debug!("check_field(expr: {:?}, base: {:?}, field: {:?})", expr, base, field);
2765 let base_ty = self.check_expr(base);
2766 let base_ty = self.structurally_resolve_type(base.span, base_ty);
2767
2768 let mut private_candidate = None;
2770
2771 let mut autoderef = self.autoderef(expr.span, base_ty);
2773 while let Some((deref_base_ty, _)) = autoderef.next() {
2774 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:2774",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(2774u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("deref_base_ty: {0:?}",
deref_base_ty) as &dyn Value))])
});
} else { ; }
};debug!("deref_base_ty: {:?}", deref_base_ty);
2775 match deref_base_ty.kind() {
2776 ty::Adt(base_def, args) if !base_def.is_enum() => {
2777 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:2777",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(2777u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("struct named {0:?}",
deref_base_ty) as &dyn Value))])
});
} else { ; }
};debug!("struct named {:?}", deref_base_ty);
2778 if let Err(guar) = base_def.non_enum_variant().has_errors() {
2780 return Ty::new_error(self.tcx(), guar);
2781 }
2782
2783 let fn_body_hir_id = self.tcx.local_def_id_to_hir_id(self.body_id);
2784 let (ident, def_scope) =
2785 self.tcx.adjust_ident_and_get_scope(field, base_def.did(), fn_body_hir_id);
2786
2787 if let Some((idx, field)) = self.find_adt_field(*base_def, ident) {
2788 self.write_field_index(expr.hir_id, idx);
2789
2790 let adjustments = self.adjust_steps(&autoderef);
2791 if field.vis.is_accessible_from(def_scope, self.tcx) {
2792 self.apply_adjustments(base, adjustments);
2793 self.register_predicates(autoderef.into_obligations());
2794
2795 self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
2796 return self.field_ty(expr.span, field, args);
2797 }
2798
2799 private_candidate = Some((adjustments, base_def.did()));
2801 }
2802 }
2803 ty::Tuple(tys) => {
2804 if let Ok(index) = field.as_str().parse::<usize>() {
2805 if field.name == sym::integer(index) {
2806 if let Some(&field_ty) = tys.get(index) {
2807 let adjustments = self.adjust_steps(&autoderef);
2808 self.apply_adjustments(base, adjustments);
2809 self.register_predicates(autoderef.into_obligations());
2810
2811 self.write_field_index(expr.hir_id, FieldIdx::from_usize(index));
2812 return field_ty;
2813 }
2814 }
2815 }
2816 }
2817 _ => {}
2818 }
2819 }
2820 let final_ty = self.structurally_resolve_type(autoderef.span(), autoderef.final_ty());
2826 if let ty::Error(_) = final_ty.kind() {
2827 return final_ty;
2828 }
2829
2830 if let Some((adjustments, did)) = private_candidate {
2831 self.apply_adjustments(base, adjustments);
2834 let guar = self.ban_private_field_access(
2835 expr,
2836 base_ty,
2837 field,
2838 did,
2839 expected.only_has_type(self),
2840 );
2841 return Ty::new_error(self.tcx(), guar);
2842 }
2843
2844 let guar = if self.method_exists_for_diagnostic(
2845 field,
2846 base_ty,
2847 expr.hir_id,
2848 expected.only_has_type(self),
2849 ) {
2850 self.ban_take_value_of_method(expr, base_ty, field)
2852 } else if !base_ty.is_primitive_ty() {
2853 self.ban_nonexisting_field(field, base, expr, base_ty)
2854 } else {
2855 let field_name = field.to_string();
2856 let mut err = {
let mut err =
{
self.dcx().struct_span_err(field.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is a primitive type and therefore doesn\'t have fields",
base_ty))
})).with_code(E0610)
};
if base_ty.references_error() { err.downgrade_to_delayed_bug(); }
err
}type_error_struct!(
2857 self.dcx(),
2858 field.span,
2859 base_ty,
2860 E0610,
2861 "`{base_ty}` is a primitive type and therefore doesn't have fields",
2862 );
2863 let is_valid_suffix = |field: &str| {
2864 if field == "f32" || field == "f64" {
2865 return true;
2866 }
2867 let mut chars = field.chars().peekable();
2868 match chars.peek() {
2869 Some('e') | Some('E') => {
2870 chars.next();
2871 if let Some(c) = chars.peek()
2872 && !c.is_numeric()
2873 && *c != '-'
2874 && *c != '+'
2875 {
2876 return false;
2877 }
2878 while let Some(c) = chars.peek() {
2879 if !c.is_numeric() {
2880 break;
2881 }
2882 chars.next();
2883 }
2884 }
2885 _ => (),
2886 }
2887 let suffix = chars.collect::<String>();
2888 suffix.is_empty() || suffix == "f32" || suffix == "f64"
2889 };
2890 let maybe_partial_suffix = |field: &str| -> Option<&str> {
2891 let first_chars = ['f', 'l'];
2892 if field.len() >= 1
2893 && field.to_lowercase().starts_with(first_chars)
2894 && field[1..].chars().all(|c| c.is_ascii_digit())
2895 {
2896 if field.to_lowercase().starts_with(['f']) { Some("f32") } else { Some("f64") }
2897 } else {
2898 None
2899 }
2900 };
2901 if let ty::Infer(ty::IntVar(_)) = base_ty.kind()
2902 && let ExprKind::Lit(Spanned {
2903 node: ast::LitKind::Int(_, ast::LitIntType::Unsuffixed),
2904 ..
2905 }) = base.kind
2906 && !base.span.from_expansion()
2907 {
2908 if is_valid_suffix(&field_name) {
2909 err.span_suggestion_verbose(
2910 field.span.shrink_to_lo(),
2911 "if intended to be a floating point literal, consider adding a `0` after the period",
2912 '0',
2913 Applicability::MaybeIncorrect,
2914 );
2915 } else if let Some(correct_suffix) = maybe_partial_suffix(&field_name) {
2916 err.span_suggestion_verbose(
2917 field.span,
2918 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("if intended to be a floating point literal, consider adding a `0` after the period and a `{0}` suffix",
correct_suffix))
})format!("if intended to be a floating point literal, consider adding a `0` after the period and a `{correct_suffix}` suffix"),
2919 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("0{0}", correct_suffix))
})format!("0{correct_suffix}"),
2920 Applicability::MaybeIncorrect,
2921 );
2922 }
2923 }
2924 err.emit()
2925 };
2926
2927 Ty::new_error(self.tcx(), guar)
2928 }
2929
2930 fn suggest_await_on_field_access(
2931 &self,
2932 err: &mut Diag<'_>,
2933 field_ident: Ident,
2934 base: &'tcx hir::Expr<'tcx>,
2935 ty: Ty<'tcx>,
2936 ) {
2937 let Some(output_ty) = self.err_ctxt().get_impl_future_output_ty(ty) else {
2938 err.span_label(field_ident.span, "unknown field");
2939 return;
2940 };
2941 let ty::Adt(def, _) = output_ty.kind() else {
2942 err.span_label(field_ident.span, "unknown field");
2943 return;
2944 };
2945 if def.is_enum() {
2947 err.span_label(field_ident.span, "unknown field");
2948 return;
2949 }
2950 if !def.non_enum_variant().fields.iter().any(|field| field.ident(self.tcx) == field_ident) {
2951 err.span_label(field_ident.span, "unknown field");
2952 return;
2953 }
2954 err.span_label(
2955 field_ident.span,
2956 "field not available in `impl Future`, but it is available in its `Output`",
2957 );
2958 match self.tcx.coroutine_kind(self.body_id) {
2959 Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
2960 err.span_suggestion_verbose(
2961 base.span.shrink_to_hi(),
2962 "consider `await`ing on the `Future` to access the field",
2963 ".await",
2964 Applicability::MaybeIncorrect,
2965 );
2966 }
2967 _ => {
2968 let mut span: MultiSpan = base.span.into();
2969 span.push_span_label(self.tcx.def_span(self.body_id), "this is not `async`");
2970 err.span_note(
2971 span,
2972 "this implements `Future` and its output type has the field, \
2973 but the future cannot be awaited in a synchronous function",
2974 );
2975 }
2976 }
2977 }
2978
2979 fn ban_nonexisting_field(
2980 &self,
2981 ident: Ident,
2982 base: &'tcx hir::Expr<'tcx>,
2983 expr: &'tcx hir::Expr<'tcx>,
2984 base_ty: Ty<'tcx>,
2985 ) -> ErrorGuaranteed {
2986 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:2986",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(2986u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("ban_nonexisting_field: field={0:?}, base={1:?}, expr={2:?}, base_ty={3:?}",
ident, base, expr, base_ty) as &dyn Value))])
});
} else { ; }
};debug!(
2987 "ban_nonexisting_field: field={:?}, base={:?}, expr={:?}, base_ty={:?}",
2988 ident, base, expr, base_ty
2989 );
2990 let mut err = self.no_such_field_err(ident, base_ty, expr);
2991
2992 match *base_ty.peel_refs().kind() {
2993 ty::Array(_, len) => {
2994 self.maybe_suggest_array_indexing(&mut err, base, ident, len);
2995 }
2996 ty::RawPtr(..) => {
2997 self.suggest_first_deref_field(&mut err, base, ident);
2998 }
2999 ty::Param(param_ty) => {
3000 err.span_label(ident.span, "unknown field");
3001 self.point_at_param_definition(&mut err, param_ty);
3002 }
3003 ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }) => {
3004 self.suggest_await_on_field_access(&mut err, ident, base, base_ty.peel_refs());
3005 }
3006 _ => {
3007 err.span_label(ident.span, "unknown field");
3008 }
3009 }
3010
3011 self.suggest_fn_call(&mut err, base, base_ty, |output_ty| {
3012 if let ty::Adt(def, _) = output_ty.kind()
3013 && !def.is_enum()
3014 {
3015 def.non_enum_variant().fields.iter().any(|field| {
3016 field.ident(self.tcx) == ident
3017 && field.vis.is_accessible_from(expr.hir_id.owner.def_id, self.tcx)
3018 })
3019 } else if let ty::Tuple(tys) = output_ty.kind()
3020 && let Ok(idx) = ident.as_str().parse::<usize>()
3021 {
3022 idx < tys.len()
3023 } else {
3024 false
3025 }
3026 });
3027
3028 if ident.name == kw::Await {
3029 err.note("to `.await` a `Future`, switch to Rust 2018 or later");
3032 HelpUseLatestEdition::new().add_to_diag(&mut err);
3033 }
3034
3035 err.emit()
3036 }
3037
3038 fn ban_private_field_access(
3039 &self,
3040 expr: &hir::Expr<'tcx>,
3041 expr_t: Ty<'tcx>,
3042 field: Ident,
3043 base_did: DefId,
3044 return_ty: Option<Ty<'tcx>>,
3045 ) -> ErrorGuaranteed {
3046 let mut err = self.private_field_err(field, base_did);
3047
3048 if self.method_exists_for_diagnostic(field, expr_t, expr.hir_id, return_ty)
3050 && !self.expr_in_place(expr.hir_id)
3051 {
3052 self.suggest_method_call(
3053 &mut err,
3054 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("a method `{0}` also exists, call it with parentheses",
field))
})format!("a method `{field}` also exists, call it with parentheses"),
3055 field,
3056 expr_t,
3057 expr,
3058 None,
3059 );
3060 }
3061 err.emit()
3062 }
3063
3064 fn ban_take_value_of_method(
3065 &self,
3066 expr: &hir::Expr<'tcx>,
3067 expr_t: Ty<'tcx>,
3068 field: Ident,
3069 ) -> ErrorGuaranteed {
3070 let mut err = {
let mut err =
{
self.dcx().struct_span_err(field.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("attempted to take value of method `{0}` on type `{1}`",
field, expr_t))
})).with_code(E0615)
};
if expr_t.references_error() { err.downgrade_to_delayed_bug(); }
err
}type_error_struct!(
3071 self.dcx(),
3072 field.span,
3073 expr_t,
3074 E0615,
3075 "attempted to take value of method `{field}` on type `{expr_t}`",
3076 );
3077 err.span_label(field.span, "method, not a field");
3078 let expr_is_call =
3079 if let hir::Node::Expr(hir::Expr { kind: ExprKind::Call(callee, _args), .. }) =
3080 self.tcx.parent_hir_node(expr.hir_id)
3081 {
3082 expr.hir_id == callee.hir_id
3083 } else {
3084 false
3085 };
3086 let expr_snippet =
3087 self.tcx.sess.source_map().span_to_snippet(expr.span).unwrap_or_default();
3088 let is_wrapped = expr_snippet.starts_with('(') && expr_snippet.ends_with(')');
3089 let after_open = expr.span.lo() + rustc_span::BytePos(1);
3090 let before_close = expr.span.hi() - rustc_span::BytePos(1);
3091
3092 if expr_is_call && is_wrapped {
3093 err.multipart_suggestion(
3094 "remove wrapping parentheses to call the method",
3095 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(expr.span.with_hi(after_open), String::new()),
(expr.span.with_lo(before_close), String::new())]))vec![
3096 (expr.span.with_hi(after_open), String::new()),
3097 (expr.span.with_lo(before_close), String::new()),
3098 ],
3099 Applicability::MachineApplicable,
3100 );
3101 } else if !self.expr_in_place(expr.hir_id) {
3102 let span = if is_wrapped {
3104 expr.span.with_lo(after_open).with_hi(before_close)
3105 } else {
3106 expr.span
3107 };
3108 self.suggest_method_call(
3109 &mut err,
3110 "use parentheses to call the method",
3111 field,
3112 expr_t,
3113 expr,
3114 Some(span),
3115 );
3116 } else if let ty::RawPtr(ptr_ty, _) = expr_t.kind()
3117 && let ty::Adt(adt_def, _) = ptr_ty.kind()
3118 && let ExprKind::Field(base_expr, _) = expr.kind
3119 && let [variant] = &adt_def.variants().raw
3120 && variant.fields.iter().any(|f| f.ident(self.tcx) == field)
3121 {
3122 err.multipart_suggestion(
3123 "to access the field, dereference first",
3124 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(base_expr.span.shrink_to_lo(), "(*".to_string()),
(base_expr.span.shrink_to_hi(), ")".to_string())]))vec![
3125 (base_expr.span.shrink_to_lo(), "(*".to_string()),
3126 (base_expr.span.shrink_to_hi(), ")".to_string()),
3127 ],
3128 Applicability::MaybeIncorrect,
3129 );
3130 } else {
3131 err.help("methods are immutable and cannot be assigned to");
3132 }
3133
3134 self.dcx().try_steal_replace_and_emit_err(field.span, StashKey::GenericInFieldExpr, err)
3136 }
3137
3138 fn point_at_param_definition(&self, err: &mut Diag<'_>, param: ty::ParamTy) {
3139 let generics = self.tcx.generics_of(self.body_id);
3140 let generic_param = generics.type_param(param, self.tcx);
3141 if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param.kind {
3142 return;
3143 }
3144 let param_def_id = generic_param.def_id;
3145 let param_hir_id = match param_def_id.as_local() {
3146 Some(x) => self.tcx.local_def_id_to_hir_id(x),
3147 None => return,
3148 };
3149 let param_span = self.tcx.hir_span(param_hir_id);
3150 let param_name = self.tcx.hir_ty_param_name(param_def_id.expect_local());
3151
3152 err.span_label(param_span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("type parameter \'{0}\' declared here",
param_name))
})format!("type parameter '{param_name}' declared here"));
3153 }
3154
3155 fn maybe_suggest_array_indexing(
3156 &self,
3157 err: &mut Diag<'_>,
3158 base: &hir::Expr<'_>,
3159 field: Ident,
3160 len: ty::Const<'tcx>,
3161 ) {
3162 err.span_label(field.span, "unknown field");
3163 if let (Some(len), Ok(user_index)) = (
3164 self.try_structurally_resolve_const(base.span, len).try_to_target_usize(self.tcx),
3165 field.as_str().parse::<u64>(),
3166 ) {
3167 let help = "instead of using tuple indexing, use array indexing";
3168 let applicability = if len < user_index {
3169 Applicability::MachineApplicable
3170 } else {
3171 Applicability::MaybeIncorrect
3172 };
3173 err.multipart_suggestion(
3174 help,
3175 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(base.span.between(field.span), "[".to_string()),
(field.span.shrink_to_hi(), "]".to_string())]))vec![
3176 (base.span.between(field.span), "[".to_string()),
3177 (field.span.shrink_to_hi(), "]".to_string()),
3178 ],
3179 applicability,
3180 );
3181 }
3182 }
3183
3184 fn suggest_first_deref_field(&self, err: &mut Diag<'_>, base: &hir::Expr<'_>, field: Ident) {
3185 err.span_label(field.span, "unknown field");
3186 let val = if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span)
3187 && base.len() < 20
3188 {
3189 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}`", base))
})format!("`{base}`")
3190 } else {
3191 "the value".to_string()
3192 };
3193 err.multipart_suggestion(
3194 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} is a raw pointer; try dereferencing it",
val))
})format!("{val} is a raw pointer; try dereferencing it"),
3195 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(base.span.shrink_to_lo(), "(*".into()),
(base.span.between(field.span),
::alloc::__export::must_use({
::alloc::fmt::format(format_args!(")."))
}))]))vec![
3196 (base.span.shrink_to_lo(), "(*".into()),
3197 (base.span.between(field.span), format!(").")),
3198 ],
3199 Applicability::MaybeIncorrect,
3200 );
3201 }
3202
3203 fn no_such_field_err(
3204 &self,
3205 field: Ident,
3206 base_ty: Ty<'tcx>,
3207 expr: &hir::Expr<'tcx>,
3208 ) -> Diag<'_> {
3209 let span = field.span;
3210 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:3210",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(3210u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("no_such_field_err(span: {0:?}, field: {1:?}, expr_t: {2:?})",
span, field, base_ty) as &dyn Value))])
});
} else { ; }
};debug!("no_such_field_err(span: {:?}, field: {:?}, expr_t: {:?})", span, field, base_ty);
3211
3212 let mut err = self.dcx().create_err(NoFieldOnType { span, ty: base_ty, field });
3213 if base_ty.references_error() {
3214 err.downgrade_to_delayed_bug();
3215 }
3216
3217 if let Some(within_macro_span) = span.within_macro(expr.span, self.tcx.sess.source_map()) {
3218 err.span_label(within_macro_span, "due to this macro variable");
3219 }
3220
3221 if let Some(def_id) = base_ty.peel_refs().ty_adt_def().map(|d| d.did()) {
3223 for &impl_def_id in self.tcx.inherent_impls(def_id) {
3224 for item in self.tcx.associated_items(impl_def_id).in_definition_order() {
3225 if let ExprKind::Field(base_expr, _) = expr.kind
3226 && item.name() == field.name
3227 && #[allow(non_exhaustive_omitted_patterns)] match item.kind {
ty::AssocKind::Fn { has_self: false, .. } => true,
_ => false,
}matches!(item.kind, ty::AssocKind::Fn { has_self: false, .. })
3228 {
3229 err.span_label(field.span, "this is an associated function, not a method");
3230 err.note("found the following associated function; to be used as method, it must have a `self` parameter");
3231 let impl_ty =
3232 self.tcx.type_of(impl_def_id).instantiate_identity().skip_norm_wip();
3233 err.span_note(
3234 self.tcx.def_span(item.def_id),
3235 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the candidate is defined in an impl for the type `{0}`",
impl_ty))
})format!("the candidate is defined in an impl for the type `{impl_ty}`"),
3236 );
3237
3238 let ty_str = match base_ty.peel_refs().kind() {
3239 ty::Adt(def, args) => self.tcx.def_path_str_with_args(def.did(), args),
3240 _ => base_ty.peel_refs().to_string(),
3241 };
3242 err.multipart_suggestion(
3243 "use associated function syntax instead",
3244 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(base_expr.span, ty_str),
(base_expr.span.between(field.span), "::".to_string())]))vec![
3245 (base_expr.span, ty_str),
3246 (base_expr.span.between(field.span), "::".to_string()),
3247 ],
3248 Applicability::MaybeIncorrect,
3249 );
3250 return err;
3251 }
3252 }
3253 }
3254 }
3255
3256 let mod_id = self.tcx.parent_module(expr.hir_id).to_def_id();
3258 let (ty, unwrap) = if let ty::Adt(def, args) = base_ty.kind()
3259 && (self.tcx.is_diagnostic_item(sym::Result, def.did())
3260 || self.tcx.is_diagnostic_item(sym::Option, def.did()))
3261 && let Some(arg) = args.get(0)
3262 && let Some(ty) = arg.as_type()
3263 {
3264 (ty, "unwrap().")
3265 } else {
3266 (base_ty, "")
3267 };
3268 for found_fields in
3269 self.get_field_candidates_considering_privacy_for_diag(span, ty, mod_id, expr.hir_id)
3270 {
3271 let field_names = found_fields.iter().map(|field| field.0.name).collect::<Vec<_>>();
3272 let mut candidate_fields: Vec<_> = found_fields
3273 .into_iter()
3274 .filter_map(|candidate_field| {
3275 self.check_for_nested_field_satisfying_condition_for_diag(
3276 span,
3277 &|candidate_field, _| candidate_field == field,
3278 candidate_field,
3279 ::alloc::vec::Vec::new()vec![],
3280 mod_id,
3281 expr.hir_id,
3282 )
3283 })
3284 .map(|mut field_path| {
3285 field_path.pop();
3286 field_path.iter().map(|id| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}.", id))
})format!("{}.", id)).collect::<String>()
3287 })
3288 .collect::<Vec<_>>();
3289 candidate_fields.sort();
3290
3291 let len = candidate_fields.len();
3292 if len > 0 && expr.span.eq_ctxt(field.span) {
3295 err.span_suggestions(
3296 field.span.shrink_to_lo(),
3297 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} of the expressions\' fields {1} a field of the same name",
if len > 1 { "some" } else { "one" },
if len > 1 { "have" } else { "has" }))
})format!(
3298 "{} of the expressions' fields {} a field of the same name",
3299 if len > 1 { "some" } else { "one" },
3300 if len > 1 { "have" } else { "has" },
3301 ),
3302 candidate_fields.iter().map(|path| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", unwrap, path))
})format!("{unwrap}{path}")),
3303 Applicability::MaybeIncorrect,
3304 );
3305 } else if let Some(field_name) =
3306 find_best_match_for_name(&field_names, field.name, None)
3307 && !(field.name.as_str().parse::<usize>().is_ok()
3308 && field_name.as_str().parse::<usize>().is_ok())
3309 {
3310 err.span_suggestion_verbose(
3311 field.span,
3312 "a field with a similar name exists",
3313 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}{0}", field_name, unwrap))
})format!("{unwrap}{}", field_name),
3314 Applicability::MaybeIncorrect,
3315 );
3316 } else if !field_names.is_empty() {
3317 let is = if field_names.len() == 1 { " is" } else { "s are" };
3318 err.note(
3319 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("available field{1}: {0}",
self.name_series_display(field_names), is))
})format!("available field{is}: {}", self.name_series_display(field_names),),
3320 );
3321 }
3322 }
3323 err
3324 }
3325
3326 fn private_field_err(&self, field: Ident, base_did: DefId) -> Diag<'_> {
3327 let struct_path = self.tcx().def_path_str(base_did);
3328 let kind_name = self.tcx().def_descr(base_did);
3329 {
self.dcx().struct_span_err(field.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("field `{0}` of {1} `{2}` is private",
field, kind_name, struct_path))
})).with_code(E0616)
}struct_span_code_err!(
3330 self.dcx(),
3331 field.span,
3332 E0616,
3333 "field `{field}` of {kind_name} `{struct_path}` is private",
3334 )
3335 .with_span_label(field.span, "private field")
3336 }
3337
3338 pub(crate) fn get_field_candidates_considering_privacy_for_diag(
3339 &self,
3340 span: Span,
3341 base_ty: Ty<'tcx>,
3342 mod_id: DefId,
3343 hir_id: HirId,
3344 ) -> Vec<Vec<(Ident, Ty<'tcx>)>> {
3345 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/expr.rs:3345",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(3345u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("get_field_candidates(span: {0:?}, base_t: {1:?}",
span, base_ty) as &dyn Value))])
});
} else { ; }
};debug!("get_field_candidates(span: {:?}, base_t: {:?}", span, base_ty);
3346
3347 let mut autoderef = self.autoderef(span, base_ty).silence_errors();
3348 let deref_chain: Vec<_> = autoderef.by_ref().collect();
3349
3350 if autoderef.reached_recursion_limit() {
3354 return ::alloc::vec::Vec::new()vec![];
3355 }
3356
3357 deref_chain
3358 .into_iter()
3359 .filter_map(move |(base_t, _)| {
3360 match base_t.kind() {
3361 ty::Adt(base_def, args) if !base_def.is_enum() => {
3362 let tcx = self.tcx;
3363 let fields = &base_def.non_enum_variant().fields;
3364 if fields.iter().all(|field| !field.vis.is_accessible_from(mod_id, tcx)) {
3368 return None;
3369 }
3370 return Some(
3371 fields
3372 .iter()
3373 .filter(move |field| {
3374 field.vis.is_accessible_from(mod_id, tcx)
3375 && self.is_field_suggestable(field, hir_id, span)
3376 })
3377 .take(100)
3379 .map(|field_def| {
3380 (
3381 field_def.ident(self.tcx).normalize_to_macros_2_0(),
3382 field_def.ty(self.tcx, args),
3383 )
3384 })
3385 .collect::<Vec<_>>(),
3386 );
3387 }
3388 ty::Tuple(types) => {
3389 return Some(
3390 types
3391 .iter()
3392 .enumerate()
3393 .take(100)
3395 .map(|(i, ty)| (Ident::from_str(&i.to_string()), ty))
3396 .collect::<Vec<_>>(),
3397 );
3398 }
3399 _ => None,
3400 }
3401 })
3402 .collect()
3403 }
3404
3405 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("check_for_nested_field_satisfying_condition_for_diag",
"rustc_hir_typeck::expr", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/expr.rs"),
::tracing_core::__macro_support::Option::Some(3407u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::expr"),
::tracing_core::field::FieldSet::new(&["span",
"candidate_name", "candidate_ty", "field_path"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&span)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&candidate_name)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&candidate_ty)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&field_path)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Option<Vec<Ident>> = loop {};
return __tracing_attr_fake_return;
}
{
if field_path.len() > 3 { return None; }
field_path.push(candidate_name);
if matches(candidate_name, candidate_ty) {
return Some(field_path);
}
for nested_fields in
self.get_field_candidates_considering_privacy_for_diag(span,
candidate_ty, mod_id, hir_id) {
for field in nested_fields {
if let Some(field_path) =
self.check_for_nested_field_satisfying_condition_for_diag(span,
matches, field, field_path.clone(), mod_id, hir_id) {
return Some(field_path);
}
}
}
None
}
}
}#[instrument(skip(self, matches, mod_id, hir_id), level = "debug")]
3408 pub(crate) fn check_for_nested_field_satisfying_condition_for_diag(
3409 &self,
3410 span: Span,
3411 matches: &impl Fn(Ident, Ty<'tcx>) -> bool,
3412 (candidate_name, candidate_ty): (Ident, Ty<'tcx>),
3413 mut field_path: Vec<Ident>,
3414 mod_id: DefId,
3415 hir_id: HirId,
3416 ) -> Option<Vec<Ident>> {
3417 if field_path.len() > 3 {
3418 return None;
3421 }
3422 field_path.push(candidate_name);
3423 if matches(candidate_name, candidate_ty) {
3424 return Some(field_path);
3425 }
3426 for nested_fields in self.get_field_candidates_considering_privacy_for_diag(
3427 span,
3428 candidate_ty,
3429 mod_id,
3430 hir_id,
3431 ) {
3432 for field in nested_fields {
3434 if let Some(field_path) = self.check_for_nested_field_satisfying_condition_for_diag(
3435 span,
3436 matches,
3437 field,
3438 field_path.clone(),
3439 mod_id,
3440 hir_id,
3441 ) {
3442 return Some(field_path);
3443 }
3444 }
3445 }
3446 None
3447 }
3448
3449 fn check_expr_index(
3450 &self,
3451 base: &'tcx hir::Expr<'tcx>,
3452 idx: &'tcx hir::Expr<'tcx>,
3453 expr: &'tcx hir::Expr<'tcx>,
3454 brackets_span: Span,
3455 ) -> Ty<'tcx> {
3456 let base_t = self.check_expr(base);
3457 let idx_t = self.check_expr(idx);
3458
3459 if base_t.references_error() {
3460 base_t
3461 } else if idx_t.references_error() {
3462 idx_t
3463 } else {
3464 let base_t = self.structurally_resolve_type(base.span, base_t);
3465 match self.lookup_indexing(expr, base, base_t, idx, idx_t) {
3466 Some((index_ty, element_ty)) => {
3467 self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
3469 self.select_obligations_where_possible(|errors| {
3470 self.point_at_index(errors, idx.span);
3471 });
3472 element_ty
3473 }
3474 None => {
3475 for (base_t, _) in self.autoderef(base.span, base_t).silence_errors() {
3478 if let Some((_, index_ty, element_ty)) =
3479 self.find_and_report_unsatisfied_index_impl(base, base_t)
3480 {
3481 self.demand_coerce(idx, idx_t, index_ty, None, AllowTwoPhase::No);
3482 return element_ty;
3483 }
3484 }
3485
3486 let mut err = {
let mut err =
{
self.dcx().struct_span_err(brackets_span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("cannot index into a value of type `{0}`",
base_t))
})).with_code(E0608)
};
if base_t.references_error() { err.downgrade_to_delayed_bug(); }
err
}type_error_struct!(
3487 self.dcx(),
3488 brackets_span,
3489 base_t,
3490 E0608,
3491 "cannot index into a value of type `{base_t}`",
3492 );
3493 if let ty::Tuple(types) = base_t.kind() {
3495 err.help(
3496 "tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.",
3497 );
3498 if let ExprKind::Lit(lit) = idx.kind
3500 && let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
3501 && i.get() < types.len().try_into().expect("tuple length fits in u128")
3502 {
3503 err.span_suggestion(
3504 brackets_span,
3505 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("to access tuple element `{0}`, use",
i))
})format!("to access tuple element `{i}`, use"),
3506 ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!(".{0}", i)) })format!(".{i}"),
3507 Applicability::MachineApplicable,
3508 );
3509 }
3510 }
3511
3512 if base_t.is_raw_ptr() && idx_t.is_integral() {
3513 err.multipart_suggestion(
3514 "consider using `wrapping_add` or `add` for indexing into raw pointer",
3515 ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(base.span.between(idx.span), ".wrapping_add(".to_owned()),
(idx.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
")".to_owned())]))vec![
3516 (base.span.between(idx.span), ".wrapping_add(".to_owned()),
3517 (
3518 idx.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
3519 ")".to_owned(),
3520 ),
3521 ],
3522 Applicability::MaybeIncorrect,
3523 );
3524 }
3525
3526 let reported = err.emit();
3527 Ty::new_error(self.tcx, reported)
3528 }
3529 }
3530 }
3531 }
3532
3533 fn find_and_report_unsatisfied_index_impl(
3541 &self,
3542 base_expr: &hir::Expr<'_>,
3543 base_ty: Ty<'tcx>,
3544 ) -> Option<(ErrorGuaranteed, Ty<'tcx>, Ty<'tcx>)> {
3545 let index_trait_def_id = self.tcx.lang_items().index_trait()?;
3546 let index_trait_output_def_id = self.tcx.get_diagnostic_item(sym::IndexOutput)?;
3547
3548 let mut relevant_impls = ::alloc::vec::Vec::new()vec![];
3549 self.tcx.for_each_relevant_impl(index_trait_def_id, base_ty, |impl_def_id| {
3550 relevant_impls.push(impl_def_id);
3551 });
3552 let [impl_def_id] = relevant_impls[..] else {
3553 return None;
3555 };
3556
3557 self.commit_if_ok(|snapshot| {
3558 let outer_universe = self.universe();
3559
3560 let ocx = ObligationCtxt::new_with_diagnostics(self);
3561 let impl_args = self.fresh_args_for_item(base_expr.span, impl_def_id);
3562 let impl_trait_ref =
3563 self.tcx.impl_trait_ref(impl_def_id).instantiate(self.tcx, impl_args);
3564 let cause = self.misc(base_expr.span);
3565
3566 let impl_trait_ref = ocx.normalize(&cause, self.param_env, impl_trait_ref);
3569 ocx.eq(&cause, self.param_env, base_ty, impl_trait_ref.self_ty())?;
3570
3571 let unnormalized_predicates =
3575 self.tcx.predicates_of(impl_def_id).instantiate(self.tcx, impl_args);
3576 ocx.register_obligations(traits::predicates_for_generics(
3577 |idx, span| {
3578 cause.clone().derived_cause(
3579 ty::Binder::dummy(ty::TraitPredicate {
3580 trait_ref: impl_trait_ref,
3581 polarity: ty::PredicatePolarity::Positive,
3582 }),
3583 |derived| {
3584 ObligationCauseCode::ImplDerived(Box::new(traits::ImplDerivedCause {
3585 derived,
3586 impl_or_alias_def_id: impl_def_id,
3587 impl_def_predicate_index: Some(idx),
3588 span,
3589 }))
3590 },
3591 )
3592 },
3593 |pred| ocx.normalize(&cause, self.param_env, pred),
3594 self.param_env,
3595 unnormalized_predicates,
3596 ));
3597
3598 let element_ty = ocx.normalize(
3601 &cause,
3602 self.param_env,
3603 Unnormalized::new(Ty::new_projection_from_args(
3604 self.tcx,
3605 index_trait_output_def_id,
3606 impl_trait_ref.args,
3607 )),
3608 );
3609
3610 let true_errors = ocx.try_evaluate_obligations();
3611
3612 self.leak_check(outer_universe, Some(snapshot))?;
3616
3617 let ambiguity_errors = ocx.evaluate_obligations_error_on_ambiguity();
3619 if true_errors.is_empty() && !ambiguity_errors.is_empty() {
3620 return Err(NoSolution);
3621 }
3622
3623 Ok::<_, NoSolution>((
3626 self.err_ctxt().report_fulfillment_errors(true_errors),
3627 impl_trait_ref.args.type_at(1),
3628 element_ty,
3629 ))
3630 })
3631 .ok()
3632 }
3633
3634 fn point_at_index(&self, errors: &mut Vec<traits::FulfillmentError<'tcx>>, span: Span) {
3635 let mut seen_preds = FxHashSet::default();
3636 errors.sort_by_key(|error| error.root_obligation.recursion_depth);
3640 for error in errors {
3641 match (
3642 error.root_obligation.predicate.kind().skip_binder(),
3643 error.obligation.predicate.kind().skip_binder(),
3644 ) {
3645 (ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)), _)
3646 if self.tcx.is_lang_item(predicate.trait_ref.def_id, LangItem::Index) =>
3647 {
3648 seen_preds.insert(error.obligation.predicate.kind().skip_binder());
3649 }
3650 (_, ty::PredicateKind::Clause(ty::ClauseKind::Trait(predicate)))
3651 if self.tcx.is_diagnostic_item(sym::SliceIndex, predicate.trait_ref.def_id) =>
3652 {
3653 seen_preds.insert(error.obligation.predicate.kind().skip_binder());
3654 }
3655 (root, pred) if seen_preds.contains(&pred) || seen_preds.contains(&root) => {}
3656 _ => continue,
3657 }
3658 error.obligation.cause.span = span;
3659 }
3660 }
3661
3662 fn check_expr_yield(
3663 &self,
3664 value: &'tcx hir::Expr<'tcx>,
3665 expr: &'tcx hir::Expr<'tcx>,
3666 ) -> Ty<'tcx> {
3667 match self.coroutine_types {
3668 Some(CoroutineTypes { resume_ty, yield_ty }) => {
3669 self.check_expr_coercible_to_type(value, yield_ty, None);
3670
3671 resume_ty
3672 }
3673 _ => {
3674 self.dcx().emit_err(YieldExprOutsideOfCoroutine { span: expr.span });
3675 self.check_expr(value);
3677 self.tcx.types.unit
3678 }
3679 }
3680 }
3681
3682 fn check_expr_asm_operand(&self, expr: &'tcx hir::Expr<'tcx>, is_input: bool) {
3683 let needs = if is_input { Needs::None } else { Needs::MutPlace };
3684 let ty = self.check_expr_with_needs(expr, needs);
3685 self.require_type_is_sized(ty, expr.span, ObligationCauseCode::InlineAsmSized);
3686
3687 if !is_input && !expr.is_syntactic_place_expr() {
3688 self.dcx()
3689 .struct_span_err(expr.span, "invalid asm output")
3690 .with_span_label(expr.span, "cannot assign to this expression")
3691 .emit();
3692 }
3693
3694 if is_input {
3702 let ty = self.structurally_resolve_type(expr.span, ty);
3703 match *ty.kind() {
3704 ty::FnDef(..) => {
3705 let fnptr_ty = Ty::new_fn_ptr(self.tcx, ty.fn_sig(self.tcx));
3706 self.demand_coerce(expr, ty, fnptr_ty, None, AllowTwoPhase::No);
3707 }
3708 ty::Ref(_, base_ty, mutbl) => {
3709 let ptr_ty = Ty::new_ptr(self.tcx, base_ty, mutbl);
3710 self.demand_coerce(expr, ty, ptr_ty, None, AllowTwoPhase::No);
3711 }
3712 _ => {}
3713 }
3714 }
3715 }
3716
3717 fn check_expr_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>, span: Span) -> Ty<'tcx> {
3718 if let rustc_ast::AsmMacro::NakedAsm = asm.asm_macro {
3719 if !{
{
'done:
{
for i in
::rustc_hir::attrs::HasAttrs::get_attrs(self.body_id,
&self.tcx) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(Naked(..)) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}.is_some()find_attr!(self.tcx, self.body_id, Naked(..)) {
3720 self.tcx.dcx().emit_err(NakedAsmOutsideNakedFn { span });
3721 }
3722 }
3723
3724 let mut diverge = asm.asm_macro.diverges(asm.options);
3725
3726 for (op, _op_sp) in asm.operands {
3727 match *op {
3728 hir::InlineAsmOperand::In { expr, .. } => {
3729 self.check_expr_asm_operand(expr, true);
3730 }
3731 hir::InlineAsmOperand::Out { expr: Some(expr), .. }
3732 | hir::InlineAsmOperand::InOut { expr, .. } => {
3733 self.check_expr_asm_operand(expr, false);
3734 }
3735 hir::InlineAsmOperand::Out { expr: None, .. } => {}
3736 hir::InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
3737 self.check_expr_asm_operand(in_expr, true);
3738 if let Some(out_expr) = out_expr {
3739 self.check_expr_asm_operand(out_expr, false);
3740 }
3741 }
3742 hir::InlineAsmOperand::Const { ref anon_const } => {
3743 self.check_expr_const_block(anon_const, Expectation::NoExpectation);
3744 }
3745 hir::InlineAsmOperand::SymFn { expr } => {
3746 self.check_expr(expr);
3747 }
3748 hir::InlineAsmOperand::SymStatic { .. } => {}
3749 hir::InlineAsmOperand::Label { block } => {
3750 let previous_diverges = self.diverges.get();
3751
3752 let ty = self.check_expr_block(block, ExpectHasType(self.tcx.types.unit));
3754 if !ty.is_never() {
3755 self.demand_suptype(block.span, self.tcx.types.unit, ty);
3756 diverge = false;
3757 }
3758
3759 self.diverges.set(previous_diverges);
3761 }
3762 }
3763 }
3764
3765 if diverge { self.tcx.types.never } else { self.tcx.types.unit }
3766 }
3767
3768 fn check_expr_offset_of(
3769 &self,
3770 container: &'tcx hir::Ty<'tcx>,
3771 fields: &[Ident],
3772 expr: &'tcx hir::Expr<'tcx>,
3773 ) -> Ty<'tcx> {
3774 let mut current_container = self.lower_ty(container).normalized;
3775 let mut field_indices = Vec::with_capacity(fields.len());
3776 let mut fields = fields.into_iter();
3777
3778 while let Some(&field) = fields.next() {
3779 let container = self.structurally_resolve_type(expr.span, current_container);
3780
3781 match container.kind() {
3782 ty::Adt(container_def, args) if container_def.is_enum() => {
3783 let block = self.tcx.local_def_id_to_hir_id(self.body_id);
3784 let (ident, _def_scope) =
3785 self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
3786
3787 if !self.tcx.features().offset_of_enum() {
3788 rustc_session::parse::feature_err(
3789 &self.tcx.sess,
3790 sym::offset_of_enum,
3791 ident.span,
3792 "using enums in offset_of is experimental",
3793 )
3794 .emit();
3795 }
3796
3797 let Some((index, variant)) = container_def
3798 .variants()
3799 .iter_enumerated()
3800 .find(|(_, v)| v.ident(self.tcx).normalize_to_macros_2_0() == ident)
3801 else {
3802 self.dcx()
3803 .create_err(NoVariantNamed { span: ident.span, ident, ty: container })
3804 .with_span_label(field.span, "variant not found")
3805 .emit_unless_delay(container.references_error());
3806 break;
3807 };
3808 let Some(&subfield) = fields.next() else {
3809 {
let mut err =
{
self.dcx().struct_span_err(ident.span,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` is an enum variant; expected field at end of `offset_of`",
ident))
})).with_code(E0795)
};
if container.references_error() { err.downgrade_to_delayed_bug(); }
err
}type_error_struct!(
3810 self.dcx(),
3811 ident.span,
3812 container,
3813 E0795,
3814 "`{ident}` is an enum variant; expected field at end of `offset_of`",
3815 )
3816 .with_span_label(field.span, "enum variant")
3817 .emit();
3818 break;
3819 };
3820 let (subident, sub_def_scope) =
3821 self.tcx.adjust_ident_and_get_scope(subfield, variant.def_id, block);
3822
3823 let Some((subindex, field)) = variant
3824 .fields
3825 .iter_enumerated()
3826 .find(|(_, f)| f.ident(self.tcx).normalize_to_macros_2_0() == subident)
3827 else {
3828 self.dcx()
3829 .create_err(NoFieldOnVariant {
3830 span: ident.span,
3831 container,
3832 ident,
3833 field: subfield,
3834 enum_span: field.span,
3835 field_span: subident.span,
3836 })
3837 .emit_unless_delay(container.references_error());
3838 break;
3839 };
3840
3841 let field_ty = self.field_ty(expr.span, field, args);
3842
3843 self.require_type_is_sized(
3846 field_ty,
3847 expr.span,
3848 ObligationCauseCode::FieldSized {
3849 adt_kind: AdtKind::Enum,
3850 span: self.tcx.def_span(field.did),
3851 last: false,
3852 },
3853 );
3854
3855 if field.vis.is_accessible_from(sub_def_scope, self.tcx) {
3856 self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
3857 } else {
3858 self.private_field_err(ident, container_def.did()).emit();
3859 }
3860
3861 field_indices.push((current_container, index, subindex));
3864 current_container = field_ty;
3865
3866 continue;
3867 }
3868 ty::Adt(container_def, args) => {
3869 let block = self.tcx.local_def_id_to_hir_id(self.body_id);
3870 let (ident, def_scope) =
3871 self.tcx.adjust_ident_and_get_scope(field, container_def.did(), block);
3872
3873 let fields = &container_def.non_enum_variant().fields;
3874 if let Some((index, field)) = fields
3875 .iter_enumerated()
3876 .find(|(_, f)| f.ident(self.tcx).normalize_to_macros_2_0() == ident)
3877 {
3878 let field_ty = self.field_ty(expr.span, field, args);
3879
3880 if self.tcx.features().offset_of_slice() {
3881 self.require_type_has_static_alignment(field_ty, expr.span);
3882 } else {
3883 self.require_type_is_sized(
3884 field_ty,
3885 expr.span,
3886 ObligationCauseCode::Misc,
3887 );
3888 }
3889
3890 if field.vis.is_accessible_from(def_scope, self.tcx) {
3891 self.tcx.check_stability(field.did, Some(expr.hir_id), expr.span, None);
3892 } else {
3893 self.private_field_err(ident, container_def.did()).emit();
3894 }
3895
3896 field_indices.push((current_container, FIRST_VARIANT, index));
3899 current_container = field_ty;
3900
3901 continue;
3902 }
3903 }
3904 ty::Tuple(tys) => {
3905 if let Ok(index) = field.as_str().parse::<usize>()
3906 && field.name == sym::integer(index)
3907 {
3908 if let Some(&field_ty) = tys.get(index) {
3909 if self.tcx.features().offset_of_slice() {
3910 self.require_type_has_static_alignment(field_ty, expr.span);
3911 } else {
3912 self.require_type_is_sized(
3913 field_ty,
3914 expr.span,
3915 ObligationCauseCode::Misc,
3916 );
3917 }
3918
3919 field_indices.push((current_container, FIRST_VARIANT, index.into()));
3920 current_container = field_ty;
3921
3922 continue;
3923 }
3924 }
3925 }
3926 _ => (),
3927 };
3928
3929 self.no_such_field_err(field, container, expr).emit();
3930
3931 break;
3932 }
3933
3934 self.typeck_results.borrow_mut().offset_of_data_mut().insert(expr.hir_id, field_indices);
3935
3936 self.tcx.types.usize
3937 }
3938}