Skip to content

Commit 9234f1f

Browse files
committed
Migrate cfg
1 parent 885a9f9 commit 9234f1f

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

compiler/rustc_error_messages/locales/en-US/rustdoc.ftl

+15
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,18 @@ rustdoc_missing_doc_code_examples =
134134
135135
rustdoc_private_doc_tests =
136136
documentation test in private item
137+
138+
rustdoc_cfg_unexpected_literal =
139+
unexpected literal
140+
141+
rustdoc_cfg_expected_single_identifier =
142+
expected a single identifier
143+
144+
rustdoc_cfg_option_value_not_string_literal =
145+
value of cfg option should be a string literal
146+
147+
rustdoc_cfg_expected_one_cfg_pattern =
148+
expected 1 cfg-pattern
149+
150+
rustdoc_cfg_invalid_predicate =
151+
invalid predicate

src/librustdoc/clean/cfg.rs

+9-25
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use rustc_feature::Features;
1313
use rustc_session::parse::ParseSess;
1414
use rustc_span::symbol::{sym, Symbol};
1515

16-
use rustc_span::Span;
17-
16+
use crate::errors::InvalidCfgError;
1817
use crate::html::escape::Escape;
1918

2019
#[cfg(test)]
@@ -36,12 +35,6 @@ pub(crate) enum Cfg {
3635
All(Vec<Cfg>),
3736
}
3837

39-
#[derive(PartialEq, Debug)]
40-
pub(crate) struct InvalidCfgError {
41-
pub(crate) msg: &'static str,
42-
pub(crate) span: Span,
43-
}
44-
4538
impl Cfg {
4639
/// Parses a `NestedMetaItem` into a `Cfg`.
4740
fn parse_nested(
@@ -51,7 +44,7 @@ impl Cfg {
5144
match nested_cfg {
5245
NestedMetaItem::MetaItem(ref cfg) => Cfg::parse_without(cfg, exclude),
5346
NestedMetaItem::Lit(ref lit) => {
54-
Err(InvalidCfgError { msg: "unexpected literal", span: lit.span })
47+
Err(InvalidCfgError::UnexpectedLiteral { span: lit.span })
5548
}
5649
}
5750
}
@@ -63,10 +56,7 @@ impl Cfg {
6356
let name = match cfg.ident() {
6457
Some(ident) => ident.name,
6558
None => {
66-
return Err(InvalidCfgError {
67-
msg: "expected a single identifier",
68-
span: cfg.span,
69-
});
59+
return Err(InvalidCfgError::ExpectedSingleIdentifier { span: cfg.span });
7060
}
7161
};
7262
match cfg.kind {
@@ -79,12 +69,9 @@ impl Cfg {
7969
let cfg = Cfg::Cfg(name, Some(value));
8070
if exclude.contains(&cfg) { Ok(None) } else { Ok(Some(cfg)) }
8171
}
82-
_ => Err(InvalidCfgError {
83-
// FIXME: if the main #[cfg] syntax decided to support non-string literals,
84-
// this should be changed as well.
85-
msg: "value of cfg option should be a string literal",
86-
span: lit.span,
87-
}),
72+
// FIXME: if the main #[cfg] syntax decided to support non-string literals, this
73+
// should be changed as well.
74+
_ => Err(InvalidCfgError::OptionValueNotStringLiteral { span: lit.span }),
8875
},
8976
MetaItemKind::List(ref items) => {
9077
let orig_len = items.len();
@@ -102,15 +89,12 @@ impl Cfg {
10289
return Ok(None);
10390
}
10491
} else {
105-
Err(InvalidCfgError { msg: "expected 1 cfg-pattern", span: cfg.span })
92+
Err(InvalidCfgError::ExpectedOneCfgPattern { span: cfg.span })
10693
}
10794
}
108-
_ => Err(InvalidCfgError { msg: "invalid predicate", span: cfg.span }),
95+
_ => Err(InvalidCfgError::InvalidPredicate { span: cfg.span }),
10996
};
110-
match ret {
111-
Ok(c) => Ok(Some(c)),
112-
Err(e) => Err(e),
113-
}
97+
ret.map(|c| Some(c))
11498
}
11599
}
116100
}

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ impl AttributesExt for [ast::Attribute] {
971971
match Cfg::parse(cfg_mi) {
972972
Ok(new_cfg) => cfg &= new_cfg,
973973
Err(e) => {
974-
sess.span_err(e.span, e.msg);
974+
sess.emit_err(e);
975975
}
976976
}
977977
}

src/librustdoc/errors.rs

+29
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,32 @@ pub struct MissingDocCodeExamples;
256256
#[derive(LintDiagnostic)]
257257
#[diag(rustdoc_private_doc_tests)]
258258
pub struct PrivateDocTests;
259+
260+
#[derive(Diagnostic)]
261+
pub enum InvalidCfgError {
262+
#[diag(rustdoc_cfg_unexpected_literal)]
263+
UnexpectedLiteral {
264+
#[primary_span]
265+
span: Span,
266+
},
267+
#[diag(rustdoc_cfg_expected_single_identifier)]
268+
ExpectedSingleIdentifier {
269+
#[primary_span]
270+
span: Span,
271+
},
272+
#[diag(rustdoc_cfg_option_value_not_string_literal)]
273+
OptionValueNotStringLiteral {
274+
#[primary_span]
275+
span: Span,
276+
},
277+
#[diag(rustdoc_cfg_expected_one_cfg_pattern)]
278+
ExpectedOneCfgPattern {
279+
#[primary_span]
280+
span: Span,
281+
},
282+
#[diag(rustdoc_cfg_invalid_predicate)]
283+
InvalidPredicate {
284+
#[primary_span]
285+
span: Span,
286+
},
287+
}

src/librustdoc/visit_ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
139139
.iter()
140140
.filter_map(|attr| {
141141
Cfg::parse(attr.meta_item()?)
142-
.map_err(|e| self.cx.sess().diagnostic().span_err(e.span, e.msg))
142+
.map_err(|e| self.cx.sess().diagnostic().emit_err(e))
143143
.ok()
144144
})
145145
.collect::<Vec<_>>()

0 commit comments

Comments
 (0)