about summary refs log tree commit diff
diff options
context:
space:
mode:
authorcsmoe <35686186+csmoe@users.noreply.github.com>2018-10-20 11:11:01 +0800
committercsmoe <35686186+csmoe@users.noreply.github.com>2018-10-22 20:28:37 +0800
commita76690f6a4292a9ca67e9b0efc70c2a82fb9f187 (patch)
tree4392924f6170a025ecb0d6c734c25a4650d81838
parentfb7c76bad551255b174958ccbfe7b7cda7f53897 (diff)
downloadrust-a76690f6a4292a9ca67e9b0efc70c2a82fb9f187.tar.gz
rust-a76690f6a4292a9ca67e9b0efc70c2a82fb9f187.zip
optimize unsupported literal diag message
-rw-r--r--src/libsyntax/attr/builtin.rs109
-rw-r--r--src/test/ui/conditional-compilation/cfg-attr-syntax-validation.rs4
-rw-r--r--src/test/ui/conditional-compilation/cfg-attr-syntax-validation.stderr18
-rw-r--r--src/test/ui/error-codes/E0565-1.stderr2
-rw-r--r--src/test/ui/error-codes/E0565-2.stderr2
-rw-r--r--src/test/ui/error-codes/E0565.stderr2
6 files changed, 87 insertions, 50 deletions
diff --git a/src/libsyntax/attr/builtin.rs b/src/libsyntax/attr/builtin.rs
index 86aebe6a2ab..1bbc1accc07 100644
--- a/src/libsyntax/attr/builtin.rs
+++ b/src/libsyntax/attr/builtin.rs
@@ -24,10 +24,10 @@ enum AttrError {
     MissingSince,
     MissingFeature,
     MultipleStabilityLevels,
-    UnsupportedLiteral
+    UnsupportedLiteral(&'static str, /* is_bytestr */ bool),
 }
 
-fn handle_errors(sess: &ParseSess, span: Span, error: AttrError, is_bytestr: bool) {
+fn handle_errors(sess: &ParseSess, span: Span, error: AttrError) {
     let diag = &sess.span_diagnostic;
     match error {
         AttrError::MultipleItem(item) => span_err!(diag, span, E0538,
@@ -45,13 +45,11 @@ fn handle_errors(sess: &ParseSess, span: Span, error: AttrError, is_bytestr: boo
         AttrError::MissingFeature => span_err!(diag, span, E0546, "missing 'feature'"),
         AttrError::MultipleStabilityLevels => span_err!(diag, span, E0544,
                                                         "multiple stability levels"),
-        AttrError::UnsupportedLiteral => {
-            let mut err = struct_span_err!(
-                diag,
-                span,
-                E0565,
-                "unsupported literal",
-            );
+        AttrError::UnsupportedLiteral(
+            msg,
+            is_bytestr,
+        ) => {
+            let mut err = struct_span_err!(diag, span, E0565, "{}", msg);
             if is_bytestr {
                 if let Ok(lint_str) = sess.source_map().span_to_snippet(span) {
                     err.span_suggestion_with_applicability(
@@ -222,7 +220,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
             let meta = meta.as_ref().unwrap();
             let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
                 if item.is_some() {
-                    handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()), false);
+                    handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()));
                     return false
                 }
                 if let Some(v) = meta.value_str() {
@@ -252,13 +250,19 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
                                         sess,
                                         mi.span,
                                         AttrError::UnknownMetaItem(mi.name(), expected),
-                                        false,
                                     );
                                     continue 'outer
                                 }
                             }
                         } else {
-                            handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, false);
+                            handle_errors(
+                                sess,
+                                meta.span,
+                                AttrError::UnsupportedLiteral(
+                                    "unsupported literal",
+                                    false,
+                                ),
+                            );
                             continue 'outer
                         }
                     }
@@ -283,7 +287,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
                             })
                         }
                         (None, _) => {
-                            handle_errors(sess, attr.span(), AttrError::MissingSince, false);
+                            handle_errors(sess, attr.span(), AttrError::MissingSince);
                             continue
                         }
                         _ => {
@@ -309,7 +313,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
                 }
                 "unstable" => {
                     if stab.is_some() {
-                        handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels, false);
+                        handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels);
                         break
                     }
 
@@ -330,13 +334,19 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
                                             mi.name(),
                                             &["feature", "reason", "issue"]
                                         ),
-                                        false,
                                     );
                                     continue 'outer
                                 }
                             }
                         } else {
-                            handle_errors(sess, meta.span, AttrError::UnsupportedLiteral, false);
+                            handle_errors(
+                                sess,
+                                meta.span,
+                                AttrError::UnsupportedLiteral(
+                                    "unsupported literal",
+                                    false,
+                                ),
+                            );
                             continue 'outer
                         }
                     }
@@ -363,7 +373,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
                             })
                         }
                         (None, _, _) => {
-                            handle_errors(sess, attr.span(), AttrError::MissingFeature, false);
+                            handle_errors(sess, attr.span(), AttrError::MissingFeature);
                             continue
                         }
                         _ => {
@@ -374,7 +384,7 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
                 }
                 "stable" => {
                     if stab.is_some() {
-                        handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels, false);
+                        handle_errors(sess, attr.span(), AttrError::MultipleStabilityLevels);
                         break
                     }
 
@@ -393,7 +403,6 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
                                             AttrError::UnknownMetaItem(
                                                 mi.name(), &["since", "note"],
                                             ),
-                                            false,
                                         );
                                         continue 'outer
                                     }
@@ -402,9 +411,11 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
                             NestedMetaItemKind::Literal(lit) => {
                                 handle_errors(
                                     sess,
-                                    meta.span,
-                                    AttrError::UnsupportedLiteral,
-                                    lit.node.is_bytestr()
+                                    lit.span,
+                                    AttrError::UnsupportedLiteral(
+                                        "unsupported literal",
+                                        false,
+                                    ),
                                 );
                                 continue 'outer
                             }
@@ -424,11 +435,11 @@ fn find_stability_generic<'a, I>(sess: &ParseSess,
                             })
                         }
                         (None, _) => {
-                            handle_errors(sess, attr.span(), AttrError::MissingFeature, false);
+                            handle_errors(sess, attr.span(), AttrError::MissingFeature);
                             continue
                         }
                         _ => {
-                            handle_errors(sess, attr.span(), AttrError::MissingSince, false);
+                            handle_errors(sess, attr.span(), AttrError::MissingSince);
                             continue
                         }
                     }
@@ -498,8 +509,11 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
             MetaItemKind::NameValue(lit) if !lit.node.is_str() => {
                 handle_errors(
                     sess,
-                    lit.span, AttrError::UnsupportedLiteral,
-                    lit.node.is_bytestr(),
+                    lit.span,
+                    AttrError::UnsupportedLiteral(
+                        "literal in `cfg` predicate value must be a string",
+                        lit.node.is_bytestr()
+                    ),
                 );
                 true
             }
@@ -520,7 +534,14 @@ pub fn eval_condition<F>(cfg: &ast::MetaItem, sess: &ParseSess, eval: &mut F)
         ast::MetaItemKind::List(ref mis) => {
             for mi in mis.iter() {
                 if !mi.is_meta_item() {
-                    handle_errors(sess, mi.span, AttrError::UnsupportedLiteral, false);
+                    handle_errors(
+                        sess,
+                        mi.span,
+                        AttrError::UnsupportedLiteral(
+                            "unsupported literal",
+                            false
+                        ),
+                    );
                     return false;
                 }
             }
@@ -591,7 +612,7 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
         depr = if let Some(metas) = attr.meta_item_list() {
             let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
                 if item.is_some() {
-                    handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()), false);
+                    handle_errors(sess, meta.span, AttrError::MultipleItem(meta.name()));
                     return false
                 }
                 if let Some(v) = meta.value_str() {
@@ -602,8 +623,11 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
                         handle_errors(
                             sess,
                             lit.span,
-                            AttrError::UnsupportedLiteral,
-                            lit.node.is_bytestr(),
+                            AttrError::UnsupportedLiteral(
+                                "literal in `deprecated` \
+                                value must be a string",
+                                lit.node.is_bytestr()
+                            ),
                         );
                     } else {
                         span_err!(diagnostic, meta.span, E0551, "incorrect meta item");
@@ -626,15 +650,20 @@ fn find_deprecation_generic<'a, I>(sess: &ParseSess,
                                     sess,
                                     meta.span,
                                     AttrError::UnknownMetaItem(mi.name(), &["since", "note"]),
-                                    false,
                                 );
                                 continue 'outer
                             }
                         }
                     }
                     NestedMetaItemKind::Literal(lit) => {
-                        let is_bytestr = lit.node.is_bytestr();
-                        handle_errors(sess, lit.span, AttrError::UnsupportedLiteral, is_bytestr);
+                        handle_errors(
+                            sess,
+                            lit.span,
+                            AttrError::UnsupportedLiteral(
+                                "item in `deprecated` must be a key/value pair",
+                                false,
+                            ),
+                        );
                         continue 'outer
                     }
                 }
@@ -694,12 +723,14 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
             mark_used(attr);
             for item in items {
                 if !item.is_meta_item() {
-                    let (span, is_bytestr) = if let Some(lit) = item.literal() {
-                        (lit.span, lit.node.is_bytestr())
-                    } else {
-                        (item.span, false)
-                    };
-                    handle_errors(sess, span, AttrError::UnsupportedLiteral, is_bytestr);
+                    handle_errors(
+                        sess,
+                        item.span,
+                        AttrError::UnsupportedLiteral(
+                            "meta item in `repr` must be an identifier",
+                            false,
+                        ),
+                    );
                     continue
                 }
 
diff --git a/src/test/ui/conditional-compilation/cfg-attr-syntax-validation.rs b/src/test/ui/conditional-compilation/cfg-attr-syntax-validation.rs
index 8a9740f8309..539c0777d66 100644
--- a/src/test/ui/conditional-compilation/cfg-attr-syntax-validation.rs
+++ b/src/test/ui/conditional-compilation/cfg-attr-syntax-validation.rs
@@ -19,10 +19,10 @@ struct S6;
 #[cfg(a())] //~ ERROR invalid predicate `a`
 struct S7;
 
-#[cfg(a = 10)] //~ ERROR unsupported literal
+#[cfg(a = 10)] //~ ERROR literal in `cfg` predicate value must be a string
 struct S8;
 
-#[deprecated(since = b"1.30", note = "hi")] //~ ERROR E0565
+#[cfg(a = b"hi")]
 struct S9;
 
 macro_rules! generate_s10 {
diff --git a/src/test/ui/conditional-compilation/cfg-attr-syntax-validation.stderr b/src/test/ui/conditional-compilation/cfg-attr-syntax-validation.stderr
index 0b8f2c864eb..84a5728c6ce 100644
--- a/src/test/ui/conditional-compilation/cfg-attr-syntax-validation.stderr
+++ b/src/test/ui/conditional-compilation/cfg-attr-syntax-validation.stderr
@@ -40,22 +40,28 @@ error[E0537]: invalid predicate `a`
 LL | #[cfg(a())] //~ ERROR invalid predicate `a`
    |       ^^^
 
-error[E0565]: unsupported literal
+error[E0565]: literal in `cfg` predicate value must be a string
   --> $DIR/cfg-attr-syntax-validation.rs:22:11
    |
-LL | #[cfg(a = 10)] //~ ERROR unsupported literal
+LL | #[cfg(a = 10)] //~ ERROR literal in `cfg` predicate value must be a string
    |           ^^
 
+error[E0565]: literal in `cfg` predicate value must be a string
+  --> $DIR/cfg-attr-syntax-validation.rs:25:11
+   |
+LL | #[cfg(a = b"hi")]
+   |           ^^^^^ help: consider removing the prefix: `"hi"`
+
 error: `cfg` is not a well-formed meta-item
-  --> $DIR/cfg-attr-syntax-validation.rs:27:9
+  --> $DIR/cfg-attr-syntax-validation.rs:30:9
    |
 LL |         #[cfg(feature = $expr)] //~ ERROR `cfg` is not a well-formed meta-item
    |         ^^^^^^^^^^^^^^^^^^^^^^^ help: expected syntax is: `#[cfg(/* predicate */)]`
 ...
-LL | generate_s9!(concat!("nonexistent"));
-   | ------------------------------------- in this macro invocation
+LL | generate_s10!(concat!("nonexistent"));
+   | -------------------------------------- in this macro invocation
 
-error: aborting due to 9 previous errors
+error: aborting due to 10 previous errors
 
 Some errors occurred: E0537, E0565.
 For more information about an error, try `rustc --explain E0537`.
diff --git a/src/test/ui/error-codes/E0565-1.stderr b/src/test/ui/error-codes/E0565-1.stderr
index 2a9bf92e9dd..a2e099acd3c 100644
--- a/src/test/ui/error-codes/E0565-1.stderr
+++ b/src/test/ui/error-codes/E0565-1.stderr
@@ -1,4 +1,4 @@
-error[E0565]: unsupported literal
+error[E0565]: item in `deprecated` must be a key/value pair
   --> $DIR/E0565-1.rs:12:14
    |
 LL | #[deprecated("since")] //~ ERROR E0565
diff --git a/src/test/ui/error-codes/E0565-2.stderr b/src/test/ui/error-codes/E0565-2.stderr
index 46125b71420..68093e4e2f0 100644
--- a/src/test/ui/error-codes/E0565-2.stderr
+++ b/src/test/ui/error-codes/E0565-2.stderr
@@ -1,4 +1,4 @@
-error[E0565]: unsupported literal
+error[E0565]: literal in `deprecated` value must be a string
   --> $DIR/E0565-2.rs:12:22
    |
 LL | #[deprecated(since = b"1.29", note = "hi")] //~ ERROR E0565
diff --git a/src/test/ui/error-codes/E0565.stderr b/src/test/ui/error-codes/E0565.stderr
index abea4290f0a..04edff8ec69 100644
--- a/src/test/ui/error-codes/E0565.stderr
+++ b/src/test/ui/error-codes/E0565.stderr
@@ -1,4 +1,4 @@
-error[E0565]: unsupported literal
+error[E0565]: meta item in `repr` must be an identifier
   --> $DIR/E0565.rs:12:8
    |
 LL | #[repr("C")] //~ ERROR E0565