Skip to content

Commit 9b0b16d

Browse files
committed
Auto merge of #67291 - Centril:rollup-p9gxgqp, r=Centril
Rollup of 5 pull requests Successful merges: - #67151 (doc comments: Less attribute mimicking) - #67216 (Enable `loop` and `while` in constants behind a feature flag) - #67255 (Remove i686-unknown-dragonfly target) - #67267 (Fix signature of `__wasilibc_find_relpath`) - #67282 (Fix example code of OpenOptions::open) Failed merges: r? @ghost
2 parents c8ea4ac + 389aa5f commit 9b0b16d

File tree

39 files changed

+660
-260
lines changed

39 files changed

+660
-260
lines changed

src/librustc_error_codes/error_codes/E0744.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Control-flow expressions are not allowed inside a const context.
33
At the moment, `if` and `match`, as well as the looping constructs `for`,
44
`while`, and `loop`, are forbidden inside a `const`, `static`, or `const fn`.
55

6-
```compile_fail,E0744
6+
```compile_fail,E0658
77
const _: i32 = {
88
let mut x = 0;
99
loop {

src/librustc_feature/active.rs

+14
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ macro_rules! declare_features {
5252
pub fn walk_feature_fields(&self, mut f: impl FnMut(&str, bool)) {
5353
$(f(stringify!($feature), self.$feature);)+
5454
}
55+
56+
/// Is the given feature enabled?
57+
///
58+
/// Panics if the symbol doesn't correspond to a declared feature.
59+
pub fn enabled(&self, feature: Symbol) -> bool {
60+
match feature {
61+
$( sym::$feature => self.$feature, )*
62+
63+
_ => panic!("`{}` was not listed in `declare_features`", feature),
64+
}
65+
}
5566
}
5667
};
5768
}
@@ -522,6 +533,9 @@ declare_features! (
522533
/// Allows using `&mut` in constant functions.
523534
(active, const_mut_refs, "1.41.0", Some(57349), None),
524535

536+
/// Allows the use of `loop` and `while` in constants.
537+
(active, const_loop, "1.41.0", Some(52000), None),
538+
525539
// -------------------------------------------------------------------------
526540
// feature-group-end: actual feature gates
527541
// -------------------------------------------------------------------------

src/librustc_lint/builtin.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ pub struct MissingDoc {
295295
impl_lint_pass!(MissingDoc => [MISSING_DOCS]);
296296

297297
fn has_doc(attr: &ast::Attribute) -> bool {
298+
if attr.is_doc_comment() {
299+
return true;
300+
}
301+
298302
if !attr.check_name(sym::doc) {
299303
return false;
300304
}
@@ -751,7 +755,7 @@ impl UnusedDocComment {
751755

752756
let span = sugared_span.take().unwrap_or_else(|| attr.span);
753757

754-
if attr.check_name(sym::doc) {
758+
if attr.is_doc_comment() || attr.check_name(sym::doc) {
755759
let mut err = cx.struct_span_lint(UNUSED_DOC_COMMENTS, span, "unused doc comment");
756760

757761
err.span_label(

src/librustc_lint/unused.rs

+4
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
296296
fn check_attribute(&mut self, cx: &LateContext<'_, '_>, attr: &ast::Attribute) {
297297
debug!("checking attribute: {:?}", attr);
298298

299+
if attr.is_doc_comment() {
300+
return;
301+
}
302+
299303
let attr_info = attr.ident().and_then(|ident| self.builtin_attributes.get(&ident.name));
300304

301305
if let Some(&&(name, ty, ..)) = attr_info {

src/librustc_mir/transform/check_consts/ops.rs

+4
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ impl NonConstOp for LiveDrop {
170170
#[derive(Debug)]
171171
pub struct Loop;
172172
impl NonConstOp for Loop {
173+
fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
174+
Some(tcx.features().const_loop)
175+
}
176+
173177
fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
174178
// This should be caught by the HIR const-checker.
175179
item.tcx.sess.delay_span_bug(

src/librustc_mir/transform/qualify_min_const_fn.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,12 @@ fn check_terminator(
390390
cleanup: _,
391391
} => check_operand(tcx, cond, span, def_id, body),
392392

393+
| TerminatorKind::FalseUnwind { .. }
394+
if feature_allowed(tcx, def_id, sym::const_loop)
395+
=> Ok(()),
396+
393397
TerminatorKind::FalseUnwind { .. } => {
394398
Err((span, "loops are not allowed in const fn".into()))
395-
},
399+
}
396400
}
397401
}

src/librustc_parse/validate_attr.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ use crate::parse_in;
44

55
use rustc_errors::{PResult, Applicability};
66
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
7-
use syntax::ast::{self, Attribute, AttrKind, Ident, MacArgs, MacDelimiter, MetaItem, MetaItemKind};
8-
use syntax::attr::mk_name_value_item_str;
7+
use syntax::ast::{self, Attribute, MacArgs, MacDelimiter, MetaItem, MetaItemKind};
98
use syntax::early_buffered_lints::ILL_FORMED_ATTRIBUTE_INPUT;
109
use syntax::tokenstream::DelimSpan;
1110
use syntax::sess::ParseSess;
1211
use syntax_pos::{Symbol, sym};
1312

1413
pub fn check_meta(sess: &ParseSess, attr: &Attribute) {
14+
if attr.is_doc_comment() {
15+
return;
16+
}
17+
1518
let attr_info =
1619
attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
1720

@@ -28,25 +31,21 @@ pub fn check_meta(sess: &ParseSess, attr: &Attribute) {
2831
}
2932

3033
pub fn parse_meta<'a>(sess: &'a ParseSess, attr: &Attribute) -> PResult<'a, MetaItem> {
31-
Ok(match attr.kind {
32-
AttrKind::Normal(ref item) => MetaItem {
33-
span: attr.span,
34-
path: item.path.clone(),
35-
kind: match &attr.get_normal_item().args {
36-
MacArgs::Empty => MetaItemKind::Word,
37-
MacArgs::Eq(_, t) => {
38-
let v = parse_in(sess, t.clone(), "name value", |p| p.parse_unsuffixed_lit())?;
39-
MetaItemKind::NameValue(v)
40-
}
41-
MacArgs::Delimited(dspan, delim, t) => {
42-
check_meta_bad_delim(sess, *dspan, *delim, "wrong meta list delimiters");
43-
let nmis = parse_in(sess, t.clone(), "meta list", |p| p.parse_meta_seq_top())?;
44-
MetaItemKind::List(nmis)
45-
}
34+
let item = attr.get_normal_item();
35+
Ok(MetaItem {
36+
span: attr.span,
37+
path: item.path.clone(),
38+
kind: match &item.args {
39+
MacArgs::Empty => MetaItemKind::Word,
40+
MacArgs::Eq(_, t) => {
41+
let v = parse_in(sess, t.clone(), "name value", |p| p.parse_unsuffixed_lit())?;
42+
MetaItemKind::NameValue(v)
43+
}
44+
MacArgs::Delimited(dspan, delim, t) => {
45+
check_meta_bad_delim(sess, *dspan, *delim, "wrong meta list delimiters");
46+
let nmis = parse_in(sess, t.clone(), "meta list", |p| p.parse_meta_seq_top())?;
47+
MetaItemKind::List(nmis)
4648
}
47-
},
48-
AttrKind::DocComment(comment) => {
49-
mk_name_value_item_str(Ident::new(sym::doc, attr.span), comment, attr.span)
5049
}
5150
})
5251
}

src/librustc_passes/check_const.rs

+68-19
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ use rustc::hir::map::Map;
1313
use rustc::hir;
1414
use rustc::ty::TyCtxt;
1515
use rustc::ty::query::Providers;
16-
use rustc_feature::Features;
16+
use rustc::session::config::nightly_options;
1717
use syntax::ast::Mutability;
1818
use syntax::feature_gate::feature_err;
1919
use syntax::span_err;
20-
use syntax_pos::{sym, Span};
20+
use syntax_pos::{sym, Span, Symbol};
2121
use rustc_error_codes::*;
2222

2323
use std::fmt;
@@ -37,18 +37,31 @@ impl NonConstExpr {
3737
}
3838
}
3939

40-
/// Returns `true` if all feature gates required to enable this expression are turned on, or
41-
/// `None` if there is no feature gate corresponding to this expression.
42-
fn is_feature_gate_enabled(self, features: &Features) -> Option<bool> {
40+
fn required_feature_gates(self) -> Option<&'static [Symbol]> {
4341
use hir::MatchSource::*;
44-
match self {
42+
use hir::LoopSource::*;
43+
44+
let gates: &[_] = match self {
4545
| Self::Match(Normal)
4646
| Self::Match(IfDesugar { .. })
4747
| Self::Match(IfLetDesugar { .. })
48-
=> Some(features.const_if_match),
48+
=> &[sym::const_if_match],
4949

50-
_ => None,
51-
}
50+
| Self::Loop(Loop)
51+
=> &[sym::const_loop],
52+
53+
| Self::Loop(While)
54+
| Self::Loop(WhileLet)
55+
| Self::Match(WhileDesugar)
56+
| Self::Match(WhileLetDesugar)
57+
=> &[sym::const_loop, sym::const_if_match],
58+
59+
// A `for` loop's desugaring contains a call to `IntoIterator::into_iter`,
60+
// so they are not yet allowed with `#![feature(const_loop)]`.
61+
_ => return None,
62+
};
63+
64+
Some(gates)
5265
}
5366
}
5467

@@ -120,11 +133,15 @@ impl<'tcx> CheckConstVisitor<'tcx> {
120133

121134
/// Emits an error when an unsupported expression is found in a const context.
122135
fn const_check_violated(&self, expr: NonConstExpr, span: Span) {
123-
match expr.is_feature_gate_enabled(self.tcx.features()) {
136+
let features = self.tcx.features();
137+
let required_gates = expr.required_feature_gates();
138+
match required_gates {
124139
// Don't emit an error if the user has enabled the requisite feature gates.
125-
Some(true) => return,
140+
Some(gates) if gates.iter().all(|&g| features.enabled(g)) => return,
126141

127-
// Users of `-Zunleash-the-miri-inside-of-you` must use feature gates when possible.
142+
// `-Zunleash-the-miri-inside-of-you` only works for expressions that don't have a
143+
// corresponding feature gate. This encourages nightly users to use feature gates when
144+
// possible.
128145
None if self.tcx.sess.opts.debugging_opts.unleash_the_miri_inside_of_you => {
129146
self.tcx.sess.span_warn(span, "skipping const checks");
130147
return;
@@ -135,15 +152,47 @@ impl<'tcx> CheckConstVisitor<'tcx> {
135152

136153
let const_kind = self.const_kind
137154
.expect("`const_check_violated` may only be called inside a const context");
138-
139155
let msg = format!("`{}` is not allowed in a `{}`", expr.name(), const_kind);
140-
match expr {
141-
| NonConstExpr::Match(hir::MatchSource::Normal)
142-
| NonConstExpr::Match(hir::MatchSource::IfDesugar { .. })
143-
| NonConstExpr::Match(hir::MatchSource::IfLetDesugar { .. })
144-
=> feature_err(&self.tcx.sess.parse_sess, sym::const_if_match, span, &msg).emit(),
145156

146-
_ => span_err!(self.tcx.sess, span, E0744, "{}", msg),
157+
let required_gates = required_gates.unwrap_or(&[]);
158+
let missing_gates: Vec<_> = required_gates
159+
.iter()
160+
.copied()
161+
.filter(|&g| !features.enabled(g))
162+
.collect();
163+
164+
match missing_gates.as_slice() {
165+
&[] => span_err!(self.tcx.sess, span, E0744, "{}", msg),
166+
167+
// If the user enabled `#![feature(const_loop)]` but not `#![feature(const_if_match)]`,
168+
// explain why their `while` loop is being rejected.
169+
&[gate @ sym::const_if_match] if required_gates.contains(&sym::const_loop) => {
170+
feature_err(&self.tcx.sess.parse_sess, gate, span, &msg)
171+
.note("`#![feature(const_loop)]` alone is not sufficient, \
172+
since this loop expression contains an implicit conditional")
173+
.emit();
174+
}
175+
176+
&[missing_primary, ref missing_secondary @ ..] => {
177+
let mut err = feature_err(&self.tcx.sess.parse_sess, missing_primary, span, &msg);
178+
179+
// If multiple feature gates would be required to enable this expression, include
180+
// them as help messages. Don't emit a separate error for each missing feature gate.
181+
//
182+
// FIXME(ecstaticmorse): Maybe this could be incorporated into `feature_err`? This
183+
// is a pretty narrow case, however.
184+
if nightly_options::is_nightly_build() {
185+
for gate in missing_secondary {
186+
let note = format!(
187+
"add `#![feature({})]` to the crate attributes to enable",
188+
gate,
189+
);
190+
err.help(&note);
191+
}
192+
}
193+
194+
err.emit();
195+
}
147196
}
148197
}
149198

src/librustc_passes/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#![feature(in_band_lifetimes)]
1010
#![feature(nll)]
11+
#![feature(slice_patterns)]
1112

1213
#![recursion_limit="256"]
1314

src/librustc_save_analysis/lib.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -883,15 +883,15 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
883883
let mut result = String::new();
884884

885885
for attr in attrs {
886-
if attr.check_name(sym::doc) {
887-
if let Some(val) = attr.value_str() {
888-
if attr.is_doc_comment() {
889-
result.push_str(&strip_doc_comment_decoration(&val.as_str()));
890-
} else {
891-
result.push_str(&val.as_str());
892-
}
893-
result.push('\n');
894-
} else if let Some(meta_list) = attr.meta_item_list() {
886+
if let Some(val) = attr.doc_str() {
887+
if attr.is_doc_comment() {
888+
result.push_str(&strip_doc_comment_decoration(&val.as_str()));
889+
} else {
890+
result.push_str(&val.as_str());
891+
}
892+
result.push('\n');
893+
} else if attr.check_name(sym::doc) {
894+
if let Some(meta_list) = attr.meta_item_list() {
895895
meta_list.into_iter()
896896
.filter(|it| it.check_name(sym::include))
897897
.filter_map(|it| it.meta_item_list().map(|l| l.to_owned()))

src/librustc_target/spec/i686_unknown_dragonfly.rs

-23
This file was deleted.

src/librustc_target/spec/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,6 @@ supported_targets! {
398398
("powerpc64-unknown-freebsd", powerpc64_unknown_freebsd),
399399
("x86_64-unknown-freebsd", x86_64_unknown_freebsd),
400400

401-
("i686-unknown-dragonfly", i686_unknown_dragonfly),
402401
("x86_64-unknown-dragonfly", x86_64_unknown_dragonfly),
403402

404403
("aarch64-unknown-openbsd", aarch64_unknown_openbsd),

0 commit comments

Comments
 (0)