]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/values.rs
Rollup merge of #103117 - joshtriplett:use-is-terminal, r=eholk
[rust.git] / compiler / rustc_middle / src / values.rs
1 use rustc_data_structures::fx::FxHashSet;
2 use rustc_errors::{pluralize, struct_span_err, Applicability, MultiSpan};
3 use rustc_hir as hir;
4 use rustc_hir::def::DefKind;
5 use rustc_middle::ty::Representability;
6 use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt};
7 use rustc_query_system::query::QueryInfo;
8 use rustc_query_system::Value;
9 use rustc_span::def_id::LocalDefId;
10 use rustc_span::Span;
11
12 use std::fmt::Write;
13
14 impl<'tcx> Value<TyCtxt<'tcx>> for Ty<'_> {
15     fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self {
16         // SAFETY: This is never called when `Self` is not `Ty<'tcx>`.
17         // FIXME: Represent the above fact in the trait system somehow.
18         unsafe { std::mem::transmute::<Ty<'tcx>, Ty<'_>>(tcx.ty_error()) }
19     }
20 }
21
22 impl<'tcx> Value<TyCtxt<'tcx>> for ty::SymbolName<'_> {
23     fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self {
24         // SAFETY: This is never called when `Self` is not `SymbolName<'tcx>`.
25         // FIXME: Represent the above fact in the trait system somehow.
26         unsafe {
27             std::mem::transmute::<ty::SymbolName<'tcx>, ty::SymbolName<'_>>(ty::SymbolName::new(
28                 tcx, "<error>",
29             ))
30         }
31     }
32 }
33
34 impl<'tcx> Value<TyCtxt<'tcx>> for ty::Binder<'_, ty::FnSig<'_>> {
35     fn from_cycle_error(tcx: TyCtxt<'tcx>, _: &[QueryInfo]) -> Self {
36         let err = tcx.ty_error();
37         // FIXME(compiler-errors): It would be nice if we could get the
38         // query key, so we could at least generate a fn signature that
39         // has the right arity.
40         let fn_sig = ty::Binder::dummy(tcx.mk_fn_sig(
41             [].into_iter(),
42             err,
43             false,
44             rustc_hir::Unsafety::Normal,
45             rustc_target::spec::abi::Abi::Rust,
46         ));
47
48         // SAFETY: This is never called when `Self` is not `ty::Binder<'tcx, ty::FnSig<'tcx>>`.
49         // FIXME: Represent the above fact in the trait system somehow.
50         unsafe { std::mem::transmute::<ty::PolyFnSig<'tcx>, ty::Binder<'_, ty::FnSig<'_>>>(fn_sig) }
51     }
52 }
53
54 impl<'tcx> Value<TyCtxt<'tcx>> for Representability {
55     fn from_cycle_error(tcx: TyCtxt<'tcx>, cycle: &[QueryInfo]) -> Self {
56         let mut item_and_field_ids = Vec::new();
57         let mut representable_ids = FxHashSet::default();
58         for info in cycle {
59             if info.query.name == "representability"
60                 && let Some(field_id) = info.query.def_id
61                 && let Some(field_id) = field_id.as_local()
62                 && let Some(DefKind::Field) = info.query.def_kind
63             {
64                 let parent_id = tcx.parent(field_id.to_def_id());
65                 let item_id = match tcx.def_kind(parent_id) {
66                     DefKind::Variant => tcx.parent(parent_id),
67                     _ => parent_id,
68                 };
69                 item_and_field_ids.push((item_id.expect_local(), field_id));
70             }
71         }
72         for info in cycle {
73             if info.query.name == "representability_adt_ty"
74                 && let Some(def_id) = info.query.ty_adt_id
75                 && let Some(def_id) = def_id.as_local()
76                 && !item_and_field_ids.iter().any(|&(id, _)| id == def_id)
77             {
78                 representable_ids.insert(def_id);
79             }
80         }
81         recursive_type_error(tcx, item_and_field_ids, &representable_ids);
82         Representability::Infinite
83     }
84 }
85
86 // item_and_field_ids should form a cycle where each field contains the
87 // type in the next element in the list
88 pub fn recursive_type_error(
89     tcx: TyCtxt<'_>,
90     mut item_and_field_ids: Vec<(LocalDefId, LocalDefId)>,
91     representable_ids: &FxHashSet<LocalDefId>,
92 ) {
93     const ITEM_LIMIT: usize = 5;
94
95     // Rotate the cycle so that the item with the lowest span is first
96     let start_index = item_and_field_ids
97         .iter()
98         .enumerate()
99         .min_by_key(|&(_, &(id, _))| tcx.def_span(id))
100         .unwrap()
101         .0;
102     item_and_field_ids.rotate_left(start_index);
103
104     let cycle_len = item_and_field_ids.len();
105     let show_cycle_len = cycle_len.min(ITEM_LIMIT);
106
107     let mut err_span = MultiSpan::from_spans(
108         item_and_field_ids[..show_cycle_len]
109             .iter()
110             .map(|(id, _)| tcx.def_span(id.to_def_id()))
111             .collect(),
112     );
113     let mut suggestion = Vec::with_capacity(show_cycle_len * 2);
114     for i in 0..show_cycle_len {
115         let (_, field_id) = item_and_field_ids[i];
116         let (next_item_id, _) = item_and_field_ids[(i + 1) % cycle_len];
117         // Find the span(s) that contain the next item in the cycle
118         let hir_id = tcx.hir().local_def_id_to_hir_id(field_id);
119         let hir::Node::Field(field) = tcx.hir().get(hir_id) else { bug!("expected field") };
120         let mut found = Vec::new();
121         find_item_ty_spans(tcx, field.ty, next_item_id, &mut found, representable_ids);
122
123         // Couldn't find the type. Maybe it's behind a type alias?
124         // In any case, we'll just suggest boxing the whole field.
125         if found.is_empty() {
126             found.push(field.ty.span);
127         }
128
129         for span in found {
130             err_span.push_span_label(span, "recursive without indirection");
131             // FIXME(compiler-errors): This suggestion might be erroneous if Box is shadowed
132             suggestion.push((span.shrink_to_lo(), "Box<".to_string()));
133             suggestion.push((span.shrink_to_hi(), ">".to_string()));
134         }
135     }
136     let items_list = {
137         let mut s = String::new();
138         for (i, (item_id, _)) in item_and_field_ids.iter().enumerate() {
139             let path = tcx.def_path_str(item_id.to_def_id());
140             write!(&mut s, "`{path}`").unwrap();
141             if i == (ITEM_LIMIT - 1) && cycle_len > ITEM_LIMIT {
142                 write!(&mut s, " and {} more", cycle_len - 5).unwrap();
143                 break;
144             }
145             if cycle_len > 1 && i < cycle_len - 2 {
146                 s.push_str(", ");
147             } else if cycle_len > 1 && i == cycle_len - 2 {
148                 s.push_str(" and ")
149             }
150         }
151         s
152     };
153     let mut err = struct_span_err!(
154         tcx.sess,
155         err_span,
156         E0072,
157         "recursive type{} {} {} infinite size",
158         pluralize!(cycle_len),
159         items_list,
160         pluralize!("has", cycle_len),
161     );
162     err.multipart_suggestion(
163         "insert some indirection (e.g., a `Box`, `Rc`, or `&`) to break the cycle",
164         suggestion,
165         Applicability::HasPlaceholders,
166     );
167     err.emit();
168 }
169
170 fn find_item_ty_spans(
171     tcx: TyCtxt<'_>,
172     ty: &hir::Ty<'_>,
173     needle: LocalDefId,
174     spans: &mut Vec<Span>,
175     seen_representable: &FxHashSet<LocalDefId>,
176 ) {
177     match ty.kind {
178         hir::TyKind::Path(hir::QPath::Resolved(_, path)) => {
179             if let Some(def_id) = path.res.opt_def_id() {
180                 let check_params = def_id.as_local().map_or(true, |def_id| {
181                     if def_id == needle {
182                         spans.push(ty.span);
183                     }
184                     seen_representable.contains(&def_id)
185                 });
186                 if check_params && let Some(args) = path.segments.last().unwrap().args {
187                     let params_in_repr = tcx.params_in_repr(def_id);
188                     // the domain size check is needed because the HIR may not be well-formed at this point
189                     for (i, arg) in args.args.iter().enumerate().take(params_in_repr.domain_size()) {
190                         if let hir::GenericArg::Type(ty) = arg && params_in_repr.contains(i as u32) {
191                             find_item_ty_spans(tcx, ty, needle, spans, seen_representable);
192                         }
193                     }
194                 }
195             }
196         }
197         hir::TyKind::Array(ty, _) => find_item_ty_spans(tcx, ty, needle, spans, seen_representable),
198         hir::TyKind::Tup(tys) => {
199             tys.iter().for_each(|ty| find_item_ty_spans(tcx, ty, needle, spans, seen_representable))
200         }
201         _ => {}
202     }
203 }