1use std::cell::OnceCell;
2use std::ops::ControlFlow;
3
4use rustc_data_structures::fx::FxHashSet;
5use rustc_data_structures::graph;
6use rustc_data_structures::graph::vec_graph::VecGraph;
7use rustc_data_structures::unord::{UnordMap, UnordSet};
8use rustc_hir::attrs::DivergingFallbackBehavior;
9use rustc_hir::def::{DefKind, Res};
10use rustc_hir::def_id::DefId;
11use rustc_hir::intravisit::{InferKind, Visitor};
12use rustc_hir::{self as hir, CRATE_HIR_ID, HirId};
13use rustc_lint::builtin::FLOAT_LITERAL_F32_FALLBACK;
14use rustc_middle::ty::{self, FloatVid, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable};
15use rustc_session::lint;
16use rustc_span::def_id::LocalDefId;
17use rustc_span::{DUMMY_SP, Span};
18use rustc_trait_selection::traits::{ObligationCause, ObligationCtxt};
19use tracing::debug;
20
21use crate::{FnCtxt, errors};
22
23impl<'tcx> FnCtxt<'_, 'tcx> {
24 pub(super) fn type_inference_fallback(&self) {
27 {
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/fallback.rs:27",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(27u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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-inference-fallback start obligations: {0:#?}",
self.fulfillment_cx.borrow_mut().pending_obligations()) as
&dyn Value))])
});
} else { ; }
};debug!(
28 "type-inference-fallback start obligations: {:#?}",
29 self.fulfillment_cx.borrow_mut().pending_obligations()
30 );
31
32 self.select_obligations_where_possible(|_| {});
34
35 {
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/fallback.rs:35",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(35u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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-inference-fallback post selection obligations: {0:#?}",
self.fulfillment_cx.borrow_mut().pending_obligations()) as
&dyn Value))])
});
} else { ; }
};debug!(
36 "type-inference-fallback post selection obligations: {:#?}",
37 self.fulfillment_cx.borrow_mut().pending_obligations()
38 );
39
40 let fallback_occurred = self.fallback_types();
41
42 if fallback_occurred {
43 self.select_obligations_where_possible(|_| {});
45 }
46 }
47
48 fn fallback_types(&self) -> bool {
49 let unresolved_variables = self.unresolved_variables();
51
52 if unresolved_variables.is_empty() {
53 return false;
54 }
55
56 let (diverging_fallback, diverging_fallback_ty) =
57 self.calculate_diverging_fallback(&unresolved_variables);
58 let fallback_to_f32 = self.calculate_fallback_to_f32(&unresolved_variables);
59
60 let mut fallback_occurred = false;
64 for ty in unresolved_variables {
65 {
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/fallback.rs:65",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(65u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("unsolved_variable = {0:?}",
ty) as &dyn Value))])
});
} else { ; }
};debug!("unsolved_variable = {:?}", ty);
66 fallback_occurred |= self.fallback_if_possible(
67 ty,
68 &diverging_fallback,
69 diverging_fallback_ty,
70 &fallback_to_f32,
71 );
72 }
73
74 fallback_occurred
75 }
76
77 fn fallback_if_possible(
94 &self,
95 ty: Ty<'tcx>,
96 diverging_fallback: &UnordSet<Ty<'tcx>>,
97 diverging_fallback_ty: Ty<'tcx>,
98 fallback_to_f32: &UnordSet<FloatVid>,
99 ) -> bool {
100 let fallback = match ty.kind() {
119 _ if let Some(e) = self.tainted_by_errors() => Ty::new_error(self.tcx, e),
120 ty::Infer(ty::IntVar(_)) => self.tcx.types.i32,
121 ty::Infer(ty::FloatVar(vid)) if fallback_to_f32.contains(vid) => self.tcx.types.f32,
122 ty::Infer(ty::FloatVar(_)) => self.tcx.types.f64,
123 _ if diverging_fallback.contains(&ty) => {
124 self.diverging_fallback_has_occurred.set(true);
125 diverging_fallback_ty
126 }
127 _ => return false,
128 };
129 {
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/fallback.rs:129",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(129u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("fallback_if_possible(ty={0:?}): defaulting to `{1:?}`",
ty, fallback) as &dyn Value))])
});
} else { ; }
};debug!("fallback_if_possible(ty={:?}): defaulting to `{:?}`", ty, fallback);
130
131 let span = ty.ty_vid().map_or(DUMMY_SP, |vid| self.infcx.type_var_origin(vid).span);
132 self.demand_eqtype(span, ty, fallback);
133 true
134 }
135
136 fn calculate_fallback_to_f32(&self, unresolved_variables: &[Ty<'tcx>]) -> UnordSet<FloatVid> {
150 let roots: UnordSet<ty::FloatVid> = self.from_float_for_f32_root_vids();
151 if roots.is_empty() {
152 return UnordSet::new();
155 }
156 let fallback_to_f32 = unresolved_variables
160 .iter()
161 .flat_map(|ty| ty.float_vid())
162 .filter(|vid| roots.contains(&self.root_float_var(*vid)))
163 .inspect(|vid| {
164 let origin = self.float_var_origin(*vid);
165 let literal = self.tcx.sess.source_map().span_to_snippet(origin.span).ok();
167 self.tcx.emit_node_span_lint(
168 FLOAT_LITERAL_F32_FALLBACK,
169 origin.lint_id.unwrap_or(CRATE_HIR_ID),
170 origin.span,
171 errors::FloatLiteralF32Fallback {
172 span: literal.as_ref().map(|_| origin.span),
173 literal: literal.unwrap_or_default(),
174 },
175 );
176 })
177 .collect();
178 {
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/fallback.rs:178",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(178u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("calculate_fallback_to_f32: fallback_to_f32={0:?}",
fallback_to_f32) as &dyn Value))])
});
} else { ; }
};debug!("calculate_fallback_to_f32: fallback_to_f32={:?}", fallback_to_f32);
179 fallback_to_f32
180 }
181
182 fn calculate_diverging_fallback(
183 &self,
184 unresolved_variables: &[Ty<'tcx>],
185 ) -> (UnordSet<Ty<'tcx>>, Ty<'tcx>) {
186 {
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/fallback.rs:186",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(186u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("calculate_diverging_fallback({0:?})",
unresolved_variables) as &dyn Value))])
});
} else { ; }
};debug!("calculate_diverging_fallback({:?})", unresolved_variables);
187
188 let diverging_fallback_ty = match self.diverging_fallback_behavior {
189 DivergingFallbackBehavior::ToUnit => self.tcx.types.unit,
190 DivergingFallbackBehavior::ToNever => self.tcx.types.never,
191 DivergingFallbackBehavior::NoFallback => {
192 return (UnordSet::new(), self.tcx.types.unit);
194 }
195 };
196
197 let coercion_graph = self.create_coercion_graph();
200
201 let unsolved_vids = unresolved_variables.iter().filter_map(|ty| ty.ty_vid());
204
205 let diverging_roots: UnordSet<ty::TyVid> = self
212 .diverging_type_vars
213 .borrow()
214 .items()
215 .map(|&ty| self.shallow_resolve(ty))
216 .filter_map(|ty| ty.ty_vid())
217 .map(|vid| self.root_var(vid))
218 .collect();
219 {
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/fallback.rs:219",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(219u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("calculate_diverging_fallback: diverging_type_vars={0:?}",
self.diverging_type_vars.borrow()) as &dyn Value))])
});
} else { ; }
};debug!(
220 "calculate_diverging_fallback: diverging_type_vars={:?}",
221 self.diverging_type_vars.borrow()
222 );
223 {
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/fallback.rs:223",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(223u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("calculate_diverging_fallback: diverging_roots={0:?}",
diverging_roots) as &dyn Value))])
});
} else { ; }
};debug!("calculate_diverging_fallback: diverging_roots={:?}", diverging_roots);
224
225 let mut diverging_vids = ::alloc::vec::Vec::new()vec![];
230 for unsolved_vid in unsolved_vids {
231 let root_vid = self.root_var(unsolved_vid);
232 {
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/fallback.rs:232",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(232u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("calculate_diverging_fallback: unsolved_vid={0:?} root_vid={1:?} diverges={2:?}",
unsolved_vid, root_vid, diverging_roots.contains(&root_vid))
as &dyn Value))])
});
} else { ; }
};debug!(
233 "calculate_diverging_fallback: unsolved_vid={:?} root_vid={:?} diverges={:?}",
234 unsolved_vid,
235 root_vid,
236 diverging_roots.contains(&root_vid),
237 );
238 if diverging_roots.contains(&root_vid) {
239 diverging_vids.push(unsolved_vid);
240
241 {
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/fallback.rs:241",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(241u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("calculate_diverging_fallback: root_vid={0:?} reaches {1:?}",
root_vid,
graph::depth_first_search(&coercion_graph,
root_vid).collect::<Vec<_>>()) as &dyn Value))])
});
} else { ; }
};debug!(
242 "calculate_diverging_fallback: root_vid={:?} reaches {:?}",
243 root_vid,
244 graph::depth_first_search(&coercion_graph, root_vid).collect::<Vec<_>>()
245 );
246 }
247 }
248
249 {
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/fallback.rs:249",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(249u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("obligations: {0:#?}",
self.fulfillment_cx.borrow_mut().pending_obligations()) as
&dyn Value))])
});
} else { ; }
};debug!("obligations: {:#?}", self.fulfillment_cx.borrow_mut().pending_obligations());
250
251 let mut diverging_fallback = UnordSet::with_capacity(diverging_vids.len());
252 let unsafe_infer_vars = OnceCell::new();
253
254 self.lint_obligations_broken_by_never_type_fallback_change(
255 &diverging_vids,
256 &coercion_graph,
257 );
258
259 for &diverging_vid in &diverging_vids {
260 let diverging_ty = Ty::new_var(self.tcx, diverging_vid);
261 let root_vid = self.root_var(diverging_vid);
262
263 self.lint_never_type_fallback_flowing_into_unsafe_code(
264 &unsafe_infer_vars,
265 &coercion_graph,
266 root_vid,
267 );
268
269 diverging_fallback.insert(diverging_ty);
270 }
271
272 (diverging_fallback, diverging_fallback_ty)
273 }
274
275 fn lint_never_type_fallback_flowing_into_unsafe_code(
276 &self,
277 unsafe_infer_vars: &OnceCell<UnordMap<ty::TyVid, (HirId, Span, UnsafeUseReason)>>,
278 coercion_graph: &VecGraph<ty::TyVid, true>,
279 root_vid: ty::TyVid,
280 ) {
281 let unsafe_infer_vars = unsafe_infer_vars.get_or_init(|| {
282 let unsafe_infer_vars = compute_unsafe_infer_vars(self, self.body_id);
283 {
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/fallback.rs:283",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(283u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::tracing_core::field::FieldSet::new(&["unsafe_infer_vars"],
::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(&unsafe_infer_vars)
as &dyn Value))])
});
} else { ; }
};debug!(?unsafe_infer_vars);
284 unsafe_infer_vars
285 });
286
287 let affected_unsafe_infer_vars =
288 graph::depth_first_search_as_undirected(&coercion_graph, root_vid)
289 .filter_map(|x| unsafe_infer_vars.get(&x).copied())
290 .collect::<Vec<_>>();
291
292 let sugg = self.try_to_suggest_annotations(&[root_vid], coercion_graph);
293
294 for (hir_id, span, reason) in affected_unsafe_infer_vars {
295 self.tcx.emit_node_span_lint(
296 lint::builtin::NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE,
297 hir_id,
298 span,
299 match reason {
300 UnsafeUseReason::Call => {
301 errors::NeverTypeFallbackFlowingIntoUnsafe::Call { sugg: sugg.clone() }
302 }
303 UnsafeUseReason::Method => {
304 errors::NeverTypeFallbackFlowingIntoUnsafe::Method { sugg: sugg.clone() }
305 }
306 UnsafeUseReason::Path => {
307 errors::NeverTypeFallbackFlowingIntoUnsafe::Path { sugg: sugg.clone() }
308 }
309 UnsafeUseReason::UnionField => {
310 errors::NeverTypeFallbackFlowingIntoUnsafe::UnionField {
311 sugg: sugg.clone(),
312 }
313 }
314 UnsafeUseReason::Deref => {
315 errors::NeverTypeFallbackFlowingIntoUnsafe::Deref { sugg: sugg.clone() }
316 }
317 },
318 );
319 }
320 }
321
322 fn lint_obligations_broken_by_never_type_fallback_change(
323 &self,
324 diverging_vids: &[ty::TyVid],
325 coercions: &VecGraph<ty::TyVid, true>,
326 ) {
327 let DivergingFallbackBehavior::ToUnit = self.diverging_fallback_behavior else { return };
328
329 if diverging_vids.is_empty() {
331 return;
332 }
333
334 let remaining_errors_if_fallback_to = |fallback| {
336 self.probe(|_| {
337 let obligations = self.fulfillment_cx.borrow().pending_obligations();
338 let ocx = ObligationCtxt::new_with_diagnostics(&self.infcx);
339 ocx.register_obligations(obligations.iter().cloned());
340
341 for &diverging_vid in diverging_vids {
342 let diverging_ty = Ty::new_var(self.tcx, diverging_vid);
343
344 ocx.eq(&ObligationCause::dummy(), self.param_env, diverging_ty, fallback)
345 .expect("expected diverging var to be unconstrained");
346 }
347
348 ocx.try_evaluate_obligations()
349 })
350 };
351
352 let unit_errors = remaining_errors_if_fallback_to(self.tcx.types.unit);
355 if unit_errors.is_empty()
356 && let mut never_errors = remaining_errors_if_fallback_to(self.tcx.types.never)
357 && let [never_error, ..] = never_errors.as_mut_slice()
358 {
359 self.adjust_fulfillment_error_for_expr_obligation(never_error);
360 let sugg = self.try_to_suggest_annotations(diverging_vids, coercions);
361 self.tcx.emit_node_span_lint(
362 lint::builtin::DEPENDENCY_ON_UNIT_NEVER_TYPE_FALLBACK,
363 self.tcx.local_def_id_to_hir_id(self.body_id),
364 self.tcx.def_span(self.body_id),
365 errors::DependencyOnUnitNeverTypeFallback {
366 obligation_span: never_error.obligation.cause.span,
367 obligation: never_error.obligation.predicate,
368 sugg,
369 },
370 )
371 }
372 }
373
374 fn create_coercion_graph(&self) -> VecGraph<ty::TyVid, true> {
377 let pending_obligations = self.fulfillment_cx.borrow_mut().pending_obligations();
378 {
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/fallback.rs:378",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(378u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("create_coercion_graph: pending_obligations={0:?}",
pending_obligations) as &dyn Value))])
});
} else { ; }
};debug!("create_coercion_graph: pending_obligations={:?}", pending_obligations);
379 let coercion_edges: Vec<(ty::TyVid, ty::TyVid)> = pending_obligations
380 .into_iter()
381 .filter_map(|obligation| {
382 obligation.predicate.kind().no_bound_vars()
385 })
386 .filter_map(|atom| {
387 let (a, b) = match atom {
396 ty::PredicateKind::Coerce(ty::CoercePredicate { a, b }) => (a, b),
397 ty::PredicateKind::Subtype(ty::SubtypePredicate { a_is_expected: _, a, b }) => {
398 (a, b)
399 }
400 _ => return None,
401 };
402
403 let a_vid = self.root_vid(a)?;
404 let b_vid = self.root_vid(b)?;
405 Some((a_vid, b_vid))
406 })
407 .collect();
408 {
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/fallback.rs:408",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(408u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::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!("create_coercion_graph: coercion_edges={0:?}",
coercion_edges) as &dyn Value))])
});
} else { ; }
};debug!("create_coercion_graph: coercion_edges={:?}", coercion_edges);
409 let num_ty_vars = self.num_ty_vars();
410
411 VecGraph::new(num_ty_vars, coercion_edges)
412 }
413
414 fn root_vid(&self, ty: Ty<'tcx>) -> Option<ty::TyVid> {
416 Some(self.root_var(self.shallow_resolve(ty).ty_vid()?))
417 }
418
419 pub(crate) fn root_float_vid(&self, ty: Ty<'tcx>) -> Option<ty::FloatVid> {
421 Some(self.root_float_var(self.shallow_resolve(ty).float_vid()?))
422 }
423
424 fn try_to_suggest_annotations(
427 &self,
428 diverging_vids: &[ty::TyVid],
429 coercions: &VecGraph<ty::TyVid, true>,
430 ) -> errors::SuggestAnnotations {
431 let body =
432 self.tcx.hir_maybe_body_owned_by(self.body_id).expect("body id must have an owner");
433 let suggestions = diverging_vids
437 .iter()
438 .copied()
439 .filter_map(|vid| {
440 let reachable_vids =
441 graph::depth_first_search_as_undirected(coercions, vid).collect();
442 AnnotateUnitFallbackVisitor { reachable_vids, fcx: self }
443 .visit_expr(body.value)
444 .break_value()
445 })
446 .collect();
447 errors::SuggestAnnotations { suggestions }
448 }
449}
450
451struct AnnotateUnitFallbackVisitor<'a, 'tcx> {
454 reachable_vids: FxHashSet<ty::TyVid>,
455 fcx: &'a FnCtxt<'a, 'tcx>,
456}
457impl<'tcx> AnnotateUnitFallbackVisitor<'_, 'tcx> {
458 fn suggest_for_segment(
464 &self,
465 arg_segment: &'tcx hir::PathSegment<'tcx>,
466 def_id: DefId,
467 id: HirId,
468 ) -> ControlFlow<errors::SuggestAnnotation> {
469 if arg_segment.args.is_none()
470 && let Some(all_args) = self.fcx.typeck_results.borrow().node_args_opt(id)
471 && let generics = self.fcx.tcx.generics_of(def_id)
472 && let args = all_args[generics.parent_count..].iter().zip(&generics.own_params)
473 && args.clone().all(|(_, param)| #[allow(non_exhaustive_omitted_patterns)] match param.kind {
ty::GenericParamDefKind::Type { .. } | ty::GenericParamDefKind::Lifetime
=> true,
_ => false,
}matches!(param.kind, ty::GenericParamDefKind::Type { .. } | ty::GenericParamDefKind::Lifetime))
475 {
476 let non_apit_type_args = args.filter(|(_, param)| {
478 #[allow(non_exhaustive_omitted_patterns)] match param.kind {
ty::GenericParamDefKind::Type { synthetic: false, .. } => true,
_ => false,
}matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: false, .. })
479 });
480 let n_tys = non_apit_type_args.clone().count();
481 for (idx, (arg, _)) in non_apit_type_args.enumerate() {
482 if let Some(ty) = arg.as_type()
483 && let Some(vid) = self.fcx.root_vid(ty)
484 && self.reachable_vids.contains(&vid)
485 {
486 return ControlFlow::Break(errors::SuggestAnnotation::Turbo(
487 arg_segment.ident.span.shrink_to_hi(),
488 n_tys,
489 idx,
490 ));
491 }
492 }
493 }
494 ControlFlow::Continue(())
495 }
496}
497impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
498 type Result = ControlFlow<errors::SuggestAnnotation>;
499
500 fn visit_infer(
501 &mut self,
502 inf_id: HirId,
503 inf_span: Span,
504 _kind: InferKind<'tcx>,
505 ) -> Self::Result {
506 if let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(inf_id)
508 && let Some(vid) = self.fcx.root_vid(ty)
509 && self.reachable_vids.contains(&vid)
510 && inf_span.can_be_used_for_suggestions()
511 {
512 return ControlFlow::Break(errors::SuggestAnnotation::Unit(inf_span));
513 }
514
515 ControlFlow::Continue(())
516 }
517
518 fn visit_qpath(
519 &mut self,
520 qpath: &'tcx rustc_hir::QPath<'tcx>,
521 id: HirId,
522 span: Span,
523 ) -> Self::Result {
524 let arg_segment = match qpath {
525 hir::QPath::Resolved(_, path) => {
526 path.segments.last().expect("paths should have a segment")
527 }
528 hir::QPath::TypeRelative(_, segment) => segment,
529 };
530 if let Some(def_id) = self.fcx.typeck_results.borrow().qpath_res(qpath, id).opt_def_id()
532 && span.can_be_used_for_suggestions()
533 {
534 self.suggest_for_segment(arg_segment, def_id, id)?;
535 }
536 hir::intravisit::walk_qpath(self, qpath, id)
537 }
538
539 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Self::Result {
540 if let hir::ExprKind::Closure(&hir::Closure { body, .. })
541 | hir::ExprKind::ConstBlock(hir::ConstBlock { body, .. }) = expr.kind
542 {
543 self.visit_body(self.fcx.tcx.hir_body(body))?;
544 }
545
546 if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
549 && let Res::Def(DefKind::AssocFn, def_id) = path.res
550 && self.fcx.tcx.trait_of_assoc(def_id).is_some()
551 && let Some(args) = self.fcx.typeck_results.borrow().node_args_opt(expr.hir_id)
552 && let self_ty = args.type_at(0)
553 && let Some(vid) = self.fcx.root_vid(self_ty)
554 && self.reachable_vids.contains(&vid)
555 && let [.., trait_segment, _method_segment] = path.segments
556 && expr.span.can_be_used_for_suggestions()
557 {
558 let span = path.span.shrink_to_lo().to(trait_segment.ident.span);
559 return ControlFlow::Break(errors::SuggestAnnotation::Path(span));
560 }
561
562 if let hir::ExprKind::MethodCall(segment, ..) = expr.kind
564 && let Some(def_id) =
565 self.fcx.typeck_results.borrow().type_dependent_def_id(expr.hir_id)
566 && expr.span.can_be_used_for_suggestions()
567 {
568 self.suggest_for_segment(segment, def_id, expr.hir_id)?;
569 }
570
571 hir::intravisit::walk_expr(self, expr)
572 }
573
574 fn visit_local(&mut self, local: &'tcx hir::LetStmt<'tcx>) -> Self::Result {
575 if let hir::LocalSource::Normal = local.source
577 && let None = local.ty
578 && let Some(ty) = self.fcx.typeck_results.borrow().node_type_opt(local.hir_id)
579 && let Some(vid) = self.fcx.root_vid(ty)
580 && self.reachable_vids.contains(&vid)
581 && local.span.can_be_used_for_suggestions()
582 {
583 return ControlFlow::Break(errors::SuggestAnnotation::Local(
584 local.pat.span.shrink_to_hi(),
585 ));
586 }
587 hir::intravisit::walk_local(self, local)
588 }
589}
590
591#[derive(#[automatically_derived]
impl ::core::fmt::Debug for UnsafeUseReason {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
UnsafeUseReason::Call => "Call",
UnsafeUseReason::Method => "Method",
UnsafeUseReason::Path => "Path",
UnsafeUseReason::UnionField => "UnionField",
UnsafeUseReason::Deref => "Deref",
})
}
}Debug, #[automatically_derived]
impl ::core::marker::Copy for UnsafeUseReason { }Copy, #[automatically_derived]
impl ::core::clone::Clone for UnsafeUseReason {
#[inline]
fn clone(&self) -> UnsafeUseReason { *self }
}Clone)]
592pub(crate) enum UnsafeUseReason {
593 Call,
594 Method,
595 Path,
596 UnionField,
597 Deref,
598}
599
600fn compute_unsafe_infer_vars<'a, 'tcx>(
617 fcx: &'a FnCtxt<'a, 'tcx>,
618 body_id: LocalDefId,
619) -> UnordMap<ty::TyVid, (HirId, Span, UnsafeUseReason)> {
620 let body = fcx.tcx.hir_maybe_body_owned_by(body_id).expect("body id must have an owner");
621 let mut res = UnordMap::default();
622
623 struct UnsafeInferVarsVisitor<'a, 'tcx> {
624 fcx: &'a FnCtxt<'a, 'tcx>,
625 res: &'a mut UnordMap<ty::TyVid, (HirId, Span, UnsafeUseReason)>,
626 }
627
628 impl Visitor<'_> for UnsafeInferVarsVisitor<'_, '_> {
629 fn visit_expr(&mut self, ex: &'_ hir::Expr<'_>) {
630 let typeck_results = self.fcx.typeck_results.borrow();
631
632 match ex.kind {
633 hir::ExprKind::MethodCall(..) => {
634 if let Some(def_id) = typeck_results.type_dependent_def_id(ex.hir_id)
635 && let method_ty =
636 self.fcx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip()
637 && let sig = method_ty.fn_sig(self.fcx.tcx)
638 && sig.safety().is_unsafe()
639 {
640 let mut collector = InferVarCollector {
641 value: (ex.hir_id, ex.span, UnsafeUseReason::Method),
642 res: self.res,
643 };
644
645 typeck_results
647 .node_args(ex.hir_id)
648 .types()
649 .for_each(|t| t.visit_with(&mut collector));
650 }
651 }
652
653 hir::ExprKind::Call(func, ..) => {
654 let func_ty = typeck_results.expr_ty(func);
655
656 if func_ty.is_fn()
657 && let sig = func_ty.fn_sig(self.fcx.tcx)
658 && sig.safety().is_unsafe()
659 {
660 let mut collector = InferVarCollector {
661 value: (ex.hir_id, ex.span, UnsafeUseReason::Call),
662 res: self.res,
663 };
664
665 typeck_results
670 .node_args(func.hir_id)
671 .types()
672 .for_each(|t| t.visit_with(&mut collector));
673
674 sig.output().visit_with(&mut collector);
676 }
677 }
678
679 hir::ExprKind::Path(_) => {
683 let ty = typeck_results.expr_ty(ex);
684
685 if ty.is_fn()
688 && let sig = ty.fn_sig(self.fcx.tcx)
689 && sig.safety().is_unsafe()
690 {
691 let mut collector = InferVarCollector {
692 value: (ex.hir_id, ex.span, UnsafeUseReason::Path),
693 res: self.res,
694 };
695
696 typeck_results
698 .node_args(ex.hir_id)
699 .types()
700 .for_each(|t| t.visit_with(&mut collector));
701 }
702 }
703
704 hir::ExprKind::Unary(hir::UnOp::Deref, pointer) => {
705 if let ty::RawPtr(pointee, _) = typeck_results.expr_ty(pointer).kind() {
706 pointee.visit_with(&mut InferVarCollector {
707 value: (ex.hir_id, ex.span, UnsafeUseReason::Deref),
708 res: self.res,
709 });
710 }
711 }
712
713 hir::ExprKind::Field(base, _) => {
714 let base_ty = typeck_results.expr_ty(base);
715
716 if base_ty.is_union() {
717 typeck_results.expr_ty(ex).visit_with(&mut InferVarCollector {
718 value: (ex.hir_id, ex.span, UnsafeUseReason::UnionField),
719 res: self.res,
720 });
721 }
722 }
723
724 _ => (),
725 };
726
727 hir::intravisit::walk_expr(self, ex);
728 }
729 }
730
731 struct InferVarCollector<'r, V> {
732 value: V,
733 res: &'r mut UnordMap<ty::TyVid, V>,
734 }
735
736 impl<'tcx, V: Copy> ty::TypeVisitor<TyCtxt<'tcx>> for InferVarCollector<'_, V> {
737 fn visit_ty(&mut self, t: Ty<'tcx>) {
738 if let Some(vid) = t.ty_vid() {
739 _ = self.res.try_insert(vid, self.value);
740 } else {
741 t.super_visit_with(self)
742 }
743 }
744 }
745
746 UnsafeInferVarsVisitor { fcx, res: &mut res }.visit_expr(&body.value);
747
748 {
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/fallback.rs:748",
"rustc_hir_typeck::fallback", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/fallback.rs"),
::tracing_core::__macro_support::Option::Some(748u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::fallback"),
::tracing_core::field::FieldSet::new(&["message", "res"],
::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!("collected the following unsafe vars for {0:?}",
body_id) as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&res) as
&dyn Value))])
});
} else { ; }
};debug!(?res, "collected the following unsafe vars for {body_id:?}");
749
750 res
751}