rustc_middle/
values.rs

1use std::collections::VecDeque;
2use std::fmt::Write;
3use std::ops::ControlFlow;
4
5use rustc_data_structures::fx::FxHashSet;
6use rustc_errors::codes::*;
7use rustc_errors::{Applicability, MultiSpan, pluralize, struct_span_code_err};
8use rustc_hir as hir;
9use rustc_hir::def::{DefKind, Res};
10use rustc_query_system::Value;
11use rustc_query_system::query::{CycleError, report_cycle};
12use rustc_span::def_id::LocalDefId;
13use rustc_span::{ErrorGuaranteed, Span};
14
15use crate::dep_graph::dep_kinds;
16use crate::query::plumbing::CyclePlaceholder;
17use crate::ty::{self, Representability, Ty, TyCtxt};
18
19impl<'tcx> Value<TyCtxt<'tcx>> for Ty<'_> {
20    fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &CycleError, guar: ErrorGuaranteed) -> Self {
21        // SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
22        // FIXME: Represent the above fact in the trait system somehow.
23        unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(Ty::new_error(tcx, guar)) }
24    }
25}
26
27impl<'tcx> Value<TyCtxt<'tcx>> for Result<ty::EarlyBinder<'_, Ty<'_>>, CyclePlaceholder> {
28    fn from_cycle_error(_tcx: TyCtxt<'tcx>, _: &CycleError, guar: ErrorGuaranteed) -> Self {
29        Err(CyclePlaceholder(guar))
30    }
31}
32
33impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> {
34    fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &CycleError, _guar: ErrorGuaranteed) -> Self {
35        // SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`.
36        // FIXME: Represent the above fact in the trait system somehow.
37        unsafe {
38            std::mem::transmute::<ty::SymbolName<'tcx>, ty::SymbolName<'_>>(ty::SymbolName::new(
39                tcx, "<error>",
40            ))
41        }
42    }
43}
44
45impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
46    fn from_cycle_error(
47        tcx: TyCtxt<'tcx>,
48        cycle_error: &CycleError,
49        guar: ErrorGuaranteed,
50    ) -> Self {
51        let err = Ty::new_error(tcx, guar);
52
53        let arity = if let Some(frame) = cycle_error.cycle.get(0)
54            && frame.query.dep_kind == dep_kinds::fn_sig
55            && let Some(def_id) = frame.query.def_id
56            && let Some(node) = tcx.hir_get_if_local(def_id)
57            && let Some(sig) = node.fn_sig()
58        {
59            sig.decl.inputs.len()
60        } else {
61            tcx.dcx().abort_if_errors();
62            unreachable!()
63        };
64
65        let fn_sig = ty::Binder::dummy(tcx.mk_fn_sig(
66            std::iter::repeat(err).take(arity),
67            err,
68            false,
69            rustc_hir::Safety::Safe,
70            rustc_abi::ExternAbi::Rust,
71        ));
72
73        // SAFETY: This is never called when `Self` is not `ty::Binder<'tcx, ty::FnSig<'tcx>>`.
74        // FIXME: Represent the above fact in the trait system somehow.
75        unsafe { std::mem::transmute::<ty::PolyFnSig<'tcx>, ty::Binder<'_, ty::FnSig<'_>>>(fn_sig) }
76    }
77}
78
79impl<'tcx> Value<TyCtxt<'tcx>> for Representability {
80    fn from_cycle_error(
81        tcx: TyCtxt<'tcx>,
82        cycle_error: &CycleError,
83        _guar: ErrorGuaranteed,
84    ) -> Self {
85        let mut item_and_field_ids = Vec::new();
86        let mut representable_ids = FxHashSet::default();
87        for info in &cycle_error.cycle {
88            if info.query.dep_kind == dep_kinds::representability
89                && let Some(field_id) = info.query.def_id
90                && let Some(field_id) = field_id.as_local()
91                && let Some(DefKind::Field) = info.query.info.def_kind
92            {
93                let parent_id = tcx.parent(field_id.to_def_id());
94                let item_id = match tcx.def_kind(parent_id) {
95                    DefKind::Variant => tcx.parent(parent_id),
96                    _ => parent_id,
97                };
98                item_and_field_ids.push((item_id.expect_local(), field_id));
99            }
100        }
101        for info in &cycle_error.cycle {
102            if info.query.dep_kind == dep_kinds::representability_adt_ty
103                && let Some(def_id) = info.query.def_id_for_ty_in_cycle
104                && let Some(def_id) = def_id.as_local()
105                && !item_and_field_ids.iter().any(|&(id, _)| id == def_id)
106            {
107                representable_ids.insert(def_id);
108            }
109        }
110        let guar = recursive_type_error(tcx, item_and_field_ids, &representable_ids);
111        Representability::Infinite(guar)
112    }
113}
114
115impl<'tcx> Value<TyCtxt<'tcx>> for ty::EarlyBinder<'_, Ty<'_>> {
116    fn from_cycle_error(
117        tcx: TyCtxt<'tcx>,
118        cycle_error: &CycleError,
119        guar: ErrorGuaranteed,
120    ) -> Self {
121        ty::EarlyBinder::bind(Ty::from_cycle_error(tcx, cycle_error, guar))
122    }
123}
124
125impl<'tcx> Value<TyCtxt<'tcx>> for ty::EarlyBinder<'_, ty::Binder<'_, ty::FnSig<'_>>> {
126    fn from_cycle_error(
127        tcx: TyCtxt<'tcx>,
128        cycle_error: &CycleError,
129        guar: ErrorGuaranteed,
130    ) -> Self {
131        ty::EarlyBinder::bind(ty::Binder::from_cycle_error(tcx, cycle_error, guar))
132    }
133}
134
135impl<'tcx> Value<TyCtxt<'tcx>> for &[ty::Variance] {
136    fn from_cycle_error(
137        tcx: TyCtxt<'tcx>,
138        cycle_error: &CycleError,
139        _guar: ErrorGuaranteed,
140    ) -> Self {
141        if let Some(frame) = cycle_error.cycle.get(0)
142            && frame.query.dep_kind == dep_kinds::variances_of
143            && let Some(def_id) = frame.query.def_id
144        {
145            let n = tcx.generics_of(def_id).own_params.len();
146            vec![ty::Bivariant; n].leak()
147        } else {
148            span_bug!(
149                cycle_error.usage.as_ref().unwrap().0,
150                "only `variances_of` returns `&[ty::Variance]`"
151            );
152        }
153    }
154}
155
156// Take a cycle of `Q` and try `try_cycle` on every permutation, falling back to `otherwise`.
157fn search_for_cycle_permutation<Q, T>(
158    cycle: &[Q],
159    try_cycle: impl Fn(&mut VecDeque<&Q>) -> ControlFlow<T, ()>,
160    otherwise: impl FnOnce() -> T,
161) -> T {
162    let mut cycle: VecDeque<_> = cycle.iter().collect();
163    for _ in 0..cycle.len() {
164        match try_cycle(&mut cycle) {
165            ControlFlow::Continue(_) => {
166                cycle.rotate_left(1);
167            }
168            ControlFlow::Break(t) => return t,
169        }
170    }
171
172    otherwise()
173}
174
175impl<'tcx, T> Value<TyCtxt<'tcx>> for Result<T, &'_ ty::layout::LayoutError<'_>> {
176    fn from_cycle_error(
177        tcx: TyCtxt<'tcx>,
178        cycle_error: &CycleError,
179        _guar: ErrorGuaranteed,
180    ) -> Self {
181        let diag = search_for_cycle_permutation(
182            &cycle_error.cycle,
183            |cycle| {
184                if cycle[0].query.dep_kind == dep_kinds::layout_of
185                    && let Some(def_id) = cycle[0].query.def_id_for_ty_in_cycle
186                    && let Some(def_id) = def_id.as_local()
187                    && let def_kind = tcx.def_kind(def_id)
188                    && matches!(def_kind, DefKind::Closure)
189                    && let Some(coroutine_kind) = tcx.coroutine_kind(def_id)
190                {
191                    // FIXME: `def_span` for an fn-like coroutine will point to the fn's body
192                    // due to interactions between the desugaring into a closure expr and the
193                    // def_span code. I'm not motivated to fix it, because I tried and it was
194                    // not working, so just hack around it by grabbing the parent fn's span.
195                    let span = if coroutine_kind.is_fn_like() {
196                        tcx.def_span(tcx.local_parent(def_id))
197                    } else {
198                        tcx.def_span(def_id)
199                    };
200                    let mut diag = struct_span_code_err!(
201                        tcx.sess.dcx(),
202                        span,
203                        E0733,
204                        "recursion in {} {} requires boxing",
205                        tcx.def_kind_descr_article(def_kind, def_id.to_def_id()),
206                        tcx.def_kind_descr(def_kind, def_id.to_def_id()),
207                    );
208                    for (i, frame) in cycle.iter().enumerate() {
209                        if frame.query.dep_kind != dep_kinds::layout_of {
210                            continue;
211                        }
212                        let Some(frame_def_id) = frame.query.def_id_for_ty_in_cycle else {
213                            continue;
214                        };
215                        let Some(frame_coroutine_kind) = tcx.coroutine_kind(frame_def_id) else {
216                            continue;
217                        };
218                        let frame_span =
219                            frame.query.info.default_span(cycle[(i + 1) % cycle.len()].span);
220                        if frame_span.is_dummy() {
221                            continue;
222                        }
223                        if i == 0 {
224                            diag.span_label(frame_span, "recursive call here");
225                        } else {
226                            let coroutine_span: Span = if frame_coroutine_kind.is_fn_like() {
227                                tcx.def_span(tcx.parent(frame_def_id))
228                            } else {
229                                tcx.def_span(frame_def_id)
230                            };
231                            let mut multispan = MultiSpan::from_span(coroutine_span);
232                            multispan
233                                .push_span_label(frame_span, "...leading to this recursive call");
234                            diag.span_note(
235                                multispan,
236                                format!("which leads to this {}", tcx.def_descr(frame_def_id)),
237                            );
238                        }
239                    }
240                    // FIXME: We could report a structured suggestion if we had
241                    // enough info here... Maybe we can use a hacky HIR walker.
242                    if matches!(
243                        coroutine_kind,
244                        hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)
245                    ) {
246                        diag.note("a recursive `async fn` call must introduce indirection such as `Box::pin` to avoid an infinitely sized future");
247                    }
248
249                    ControlFlow::Break(diag)
250                } else {
251                    ControlFlow::Continue(())
252                }
253            },
254            || report_cycle(tcx.sess, cycle_error),
255        );
256
257        let guar = diag.emit();
258
259        // tcx.arena.alloc cannot be used because we are not allowed to use &'tcx LayoutError under
260        // min_specialization. Since this is an error path anyways, leaking doesn't matter (and really,
261        // tcx.arena.alloc is pretty much equal to leaking).
262        Err(Box::leak(Box::new(ty::layout::LayoutError::Cycle(guar))))
263    }
264}
265
266// item_and_field_ids should form a cycle where each field contains the
267// type in the next element in the list
268pub fn recursive_type_error(
269    tcx: TyCtxt<'_>,
270    mut item_and_field_ids: Vec<(LocalDefId, LocalDefId)>,
271    representable_ids: &FxHashSet<LocalDefId>,
272) -> ErrorGuaranteed {
273    const ITEM_LIMIT: usize = 5;
274
275    // Rotate the cycle so that the item with the lowest span is first
276    let start_index = item_and_field_ids
277        .iter()
278        .enumerate()
279        .min_by_key(|&(_, &(id, _))| tcx.def_span(id))
280        .unwrap()
281        .0;
282    item_and_field_ids.rotate_left(start_index);
283
284    let cycle_len = item_and_field_ids.len();
285    let show_cycle_len = cycle_len.min(ITEM_LIMIT);
286
287    let mut err_span = MultiSpan::from_spans(
288        item_and_field_ids[..show_cycle_len]
289            .iter()
290            .map(|(id, _)| tcx.def_span(id.to_def_id()))
291            .collect(),
292    );
293    let mut suggestion = Vec::with_capacity(show_cycle_len * 2);
294    for i in 0..show_cycle_len {
295        let (_, field_id) = item_and_field_ids[i];
296        let (next_item_id, _) = item_and_field_ids[(i + 1) % cycle_len];
297        // Find the span(s) that contain the next item in the cycle
298        let hir::Node::Field(field) = tcx.hir_node_by_def_id(field_id) else {
299            bug!("expected field")
300        };
301        let mut found = Vec::new();
302        find_item_ty_spans(tcx, field.ty, next_item_id, &mut found, representable_ids);
303
304        // Couldn't find the type. Maybe it's behind a type alias?
305        // In any case, we'll just suggest boxing the whole field.
306        if found.is_empty() {
307            found.push(field.ty.span);
308        }
309
310        for span in found {
311            err_span.push_span_label(span, "recursive without indirection");
312            // FIXME(compiler-errors): This suggestion might be erroneous if Box is shadowed
313            suggestion.push((span.shrink_to_lo(), "Box<".to_string()));
314            suggestion.push((span.shrink_to_hi(), ">".to_string()));
315        }
316    }
317    let items_list = {
318        let mut s = String::new();
319        for (i, &(item_id, _)) in item_and_field_ids.iter().enumerate() {
320            let path = tcx.def_path_str(item_id);
321            write!(&mut s, "`{path}`").unwrap();
322            if i == (ITEM_LIMIT - 1) && cycle_len > ITEM_LIMIT {
323                write!(&mut s, " and {} more", cycle_len - 5).unwrap();
324                break;
325            }
326            if cycle_len > 1 && i < cycle_len - 2 {
327                s.push_str(", ");
328            } else if cycle_len > 1 && i == cycle_len - 2 {
329                s.push_str(" and ")
330            }
331        }
332        s
333    };
334    struct_span_code_err!(
335        tcx.dcx(),
336        err_span,
337        E0072,
338        "recursive type{} {} {} infinite size",
339        pluralize!(cycle_len),
340        items_list,
341        pluralize!("has", cycle_len),
342    )
343    .with_multipart_suggestion(
344        "insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle",
345        suggestion,
346        Applicability::HasPlaceholders,
347    )
348    .emit()
349}
350
351fn find_item_ty_spans(
352    tcx: TyCtxt<'_>,
353    ty: &hir::Ty<'_>,
354    needle: LocalDefId,
355    spans: &mut Vec<Span>,
356    seen_representable: &FxHashSet<LocalDefId>,
357) {
358    match ty.kind {
359        hir::TyKind::Path(hir::QPath::Resolved(_, path)) => {
360            if let Res::Def(kind, def_id) = path.res
361                && matches!(kind, DefKind::Enum | DefKind::Struct | DefKind::Union)
362            {
363                let check_params = def_id.as_local().is_none_or(|def_id| {
364                    if def_id == needle {
365                        spans.push(ty.span);
366                    }
367                    seen_representable.contains(&def_id)
368                });
369                if check_params && let Some(args) = path.segments.last().unwrap().args {
370                    let params_in_repr = tcx.params_in_repr(def_id);
371                    // the domain size check is needed because the HIR may not be well-formed at this point
372                    for (i, arg) in args.args.iter().enumerate().take(params_in_repr.domain_size())
373                    {
374                        if let hir::GenericArg::Type(ty) = arg
375                            && params_in_repr.contains(i as u32)
376                        {
377                            find_item_ty_spans(
378                                tcx,
379                                ty.as_unambig_ty(),
380                                needle,
381                                spans,
382                                seen_representable,
383                            );
384                        }
385                    }
386                }
387            }
388        }
389        hir::TyKind::Array(ty, _) => find_item_ty_spans(tcx, ty, needle, spans, seen_representable),
390        hir::TyKind::Tup(tys) => {
391            tys.iter().for_each(|ty| find_item_ty_spans(tcx, ty, needle, spans, seen_representable))
392        }
393        _ => {}
394    }
395}