1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use rustc_attr as attr;
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{self, Visitor};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_session::parse::feature_err;
use rustc_span::{sym, Span, Symbol};
use crate::errors::ExprNotAllowedInContext;
#[derive(Clone, Copy)]
enum NonConstExpr {
Loop(hir::LoopSource),
Match(hir::MatchSource),
}
impl NonConstExpr {
fn name(self) -> String {
match self {
Self::Loop(src) => format!("`{}`", src.name()),
Self::Match(src) => format!("`{}`", src.name()),
}
}
fn required_feature_gates(self) -> Option<&'static [Symbol]> {
use hir::LoopSource::*;
use hir::MatchSource::*;
let gates: &[_] = match self {
Self::Match(AwaitDesugar) => {
return None;
}
Self::Loop(ForLoop) | Self::Match(ForLoopDesugar) => &[sym::const_for],
Self::Match(TryDesugar) => &[sym::const_try],
Self::Loop(Loop | While) | Self::Match(Normal) => &[],
};
Some(gates)
}
}
fn check_mod_const_bodies(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
let mut vis = CheckConstVisitor::new(tcx);
tcx.hir().visit_item_likes_in_module(module_def_id, &mut vis);
}
pub(crate) fn provide(providers: &mut Providers) {
*providers = Providers { check_mod_const_bodies, ..*providers };
}
#[derive(Copy, Clone)]
struct CheckConstVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
const_kind: Option<hir::ConstContext>,
def_id: Option<LocalDefId>,
}
impl<'tcx> CheckConstVisitor<'tcx> {
fn new(tcx: TyCtxt<'tcx>) -> Self {
CheckConstVisitor { tcx, const_kind: None, def_id: None }
}
fn const_check_violated(&self, expr: NonConstExpr, span: Span) {
let Self { tcx, def_id, const_kind } = *self;
let features = tcx.features();
let required_gates = expr.required_feature_gates();
let is_feature_allowed = |feature_gate| {
if !tcx.features().enabled(feature_gate) {
return false;
}
let def_id = match def_id {
Some(x) => x,
None => return true,
};
if tcx.trait_of_item(def_id.to_def_id()).is_some() {
return true;
}
if !tcx.features().staged_api
|| tcx.has_attr(def_id.to_def_id(), sym::rustc_const_unstable)
{
return true;
}
let attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(def_id));
attr::rustc_allow_const_fn_unstable(&tcx.sess, attrs).any(|name| name == feature_gate)
};
match required_gates {
Some(gates) if gates.iter().copied().all(is_feature_allowed) => return,
None if tcx.sess.opts.unstable_opts.unleash_the_miri_inside_of_you => {
tcx.sess.span_warn(span, "skipping const checks");
return;
}
_ => {}
}
let const_kind =
const_kind.expect("`const_check_violated` may only be called inside a const context");
let required_gates = required_gates.unwrap_or(&[]);
let missing_gates: Vec<_> =
required_gates.iter().copied().filter(|&g| !features.enabled(g)).collect();
match missing_gates.as_slice() {
[] => {
tcx.sess.emit_err(ExprNotAllowedInContext {
span,
expr: expr.name(),
context: const_kind.keyword_name(),
});
}
[missing_primary, ref missing_secondary @ ..] => {
let msg =
format!("{} is not allowed in a `{}`", expr.name(), const_kind.keyword_name());
let mut err = feature_err(&tcx.sess.parse_sess, *missing_primary, span, &msg);
if tcx.sess.is_nightly_build() {
for gate in missing_secondary {
let note = format!(
"add `#![feature({})]` to the crate attributes to enable",
gate,
);
err.help(¬e);
}
}
err.emit();
}
}
}
fn recurse_into(
&mut self,
kind: Option<hir::ConstContext>,
def_id: Option<LocalDefId>,
f: impl FnOnce(&mut Self),
) {
let parent_def_id = self.def_id;
let parent_kind = self.const_kind;
self.def_id = def_id;
self.const_kind = kind;
f(self);
self.def_id = parent_def_id;
self.const_kind = parent_kind;
}
}
impl<'tcx> Visitor<'tcx> for CheckConstVisitor<'tcx> {
type NestedFilter = nested_filter::OnlyBodies;
fn nested_visit_map(&mut self) -> Self::Map {
self.tcx.hir()
}
fn visit_anon_const(&mut self, anon: &'tcx hir::AnonConst) {
let kind = Some(hir::ConstContext::Const);
self.recurse_into(kind, None, |this| intravisit::walk_anon_const(this, anon));
}
fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
let owner = self.tcx.hir().body_owner_def_id(body.id());
let kind = self.tcx.hir().body_const_context(owner);
self.recurse_into(kind, Some(owner), |this| intravisit::walk_body(this, body));
}
fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
match &e.kind {
_ if self.const_kind.is_none() => {}
hir::ExprKind::Loop(_, _, source, _) => {
self.const_check_violated(NonConstExpr::Loop(*source), e.span);
}
hir::ExprKind::Match(_, _, source) => {
let non_const_expr = match source {
hir::MatchSource::ForLoopDesugar => None,
_ => Some(NonConstExpr::Match(*source)),
};
if let Some(expr) = non_const_expr {
self.const_check_violated(expr, e.span);
}
}
_ => {}
}
intravisit::walk_expr(self, e);
}
}