about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-12-09 00:55:49 +0000
committerbors <bors@rust-lang.org>2021-12-09 00:55:49 +0000
commit3c857f48ce12d1f98f3ea6d48eb9e33d8d60c985 (patch)
tree5db0b310af238377cfbde5bdd3ac75aad19dbb06
parente6b883c74f49f32cb5d1cbad3457f2b8805a4a38 (diff)
parent40c6606b789122f1584a4e06556b858d543bef6b (diff)
downloadrust-3c857f48ce12d1f98f3ea6d48eb9e33d8d60c985.tar.gz
rust-3c857f48ce12d1f98f3ea6d48eb9e33d8d60c985.zip
Auto merge of #91677 - matthiaskrgr:rollup-yiczced, r=matthiaskrgr
Rollup of 5 pull requests

Successful merges:

 - #91245 (suggest casting between i/u32 and char)
 - #91337 (Add a suggestion if `macro_rules` is misspelled)
 - #91534 (Make rustdoc headings black, and markdown blue)
 - #91637 (Add test for packed drops in generators)
 - #91667 (Fix indent of itemTypes in search.js)

Failed merges:

 - #91568 (Pretty print break and continue without redundant space)

r? `@ghost`
`@rustbot` modify labels: rollup
-rw-r--r--compiler/rustc_parse/src/parser/item.rs29
-rw-r--r--compiler/rustc_typeck/src/check/demand.rs12
-rw-r--r--src/librustdoc/html/render/print_item.rs2
-rw-r--r--src/librustdoc/html/static/css/themes/ayu.css2
-rw-r--r--src/librustdoc/html/static/css/themes/dark.css2
-rw-r--r--src/librustdoc/html/static/css/themes/light.css2
-rw-r--r--src/librustdoc/html/static/js/search.js54
-rw-r--r--src/test/rustdoc-gui/headers-color.goml15
-rw-r--r--src/test/ui/cast/cast-int-to-char.rs9
-rw-r--r--src/test/ui/cast/cast-int-to-char.stderr53
-rw-r--r--src/test/ui/match/match-type-err-first-arm.stderr5
-rw-r--r--src/test/ui/packed/packed-struct-drop-aligned.rs28
-rw-r--r--src/test/ui/parser/misspelled-macro-rules.fixed13
-rw-r--r--src/test/ui/parser/misspelled-macro-rules.rs13
-rw-r--r--src/test/ui/parser/misspelled-macro-rules.stderr10
-rw-r--r--src/test/ui/proc-macro/macro-brackets.stderr5
16 files changed, 215 insertions, 39 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs
index 831c64e3faf..516e301ec3a 100644
--- a/compiler/rustc_parse/src/parser/item.rs
+++ b/compiler/rustc_parse/src/parser/item.rs
@@ -15,6 +15,7 @@ use rustc_ast::{MacArgs, MacCall, MacDelimiter};
 use rustc_ast_pretty::pprust;
 use rustc_errors::{struct_span_err, Applicability, PResult, StashKey};
 use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
+use rustc_span::lev_distance::lev_distance;
 use rustc_span::source_map::{self, Span};
 use rustc_span::symbol::{kw, sym, Ident, Symbol};
 
@@ -410,10 +411,30 @@ impl<'a> Parser<'a> {
     fn parse_item_macro(&mut self, vis: &Visibility) -> PResult<'a, MacCall> {
         let path = self.parse_path(PathStyle::Mod)?; // `foo::bar`
         self.expect(&token::Not)?; // `!`
-        let args = self.parse_mac_args()?; // `( .. )` or `[ .. ]` (followed by `;`), or `{ .. }`.
-        self.eat_semi_for_macro_if_needed(&args);
-        self.complain_if_pub_macro(vis, false);
-        Ok(MacCall { path, args, prior_type_ascription: self.last_type_ascription })
+        match self.parse_mac_args() {
+            // `( .. )` or `[ .. ]` (followed by `;`), or `{ .. }`.
+            Ok(args) => {
+                self.eat_semi_for_macro_if_needed(&args);
+                self.complain_if_pub_macro(vis, false);
+                Ok(MacCall { path, args, prior_type_ascription: self.last_type_ascription })
+            }
+
+            Err(mut err) => {
+                // Maybe the user misspelled `macro_rules` (issue #91227)
+                if self.token.is_ident()
+                    && path.segments.len() == 1
+                    && lev_distance("macro_rules", &path.segments[0].ident.to_string()) <= 3
+                {
+                    err.span_suggestion(
+                        path.span,
+                        "perhaps you meant to define a macro",
+                        "macro_rules".to_string(),
+                        Applicability::MachineApplicable,
+                    );
+                }
+                Err(err)
+            }
+        }
     }
 
     /// Recover if we parsed attributes and expected an item but there was none.
diff --git a/compiler/rustc_typeck/src/check/demand.rs b/compiler/rustc_typeck/src/check/demand.rs
index b210c78cae0..b7e276b6965 100644
--- a/compiler/rustc_typeck/src/check/demand.rs
+++ b/compiler/rustc_typeck/src/check/demand.rs
@@ -1264,6 +1264,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 }
                 true
             }
+            (
+                &ty::Uint(ty::UintTy::U32 | ty::UintTy::U64 | ty::UintTy::U128)
+                | &ty::Int(ty::IntTy::I32 | ty::IntTy::I64 | ty::IntTy::I128),
+                &ty::Char,
+            ) => {
+                err.multipart_suggestion_verbose(
+                    &format!("{}, since a `char` always occupies 4 bytes", cast_msg,),
+                    cast_suggestion,
+                    Applicability::MachineApplicable,
+                );
+                true
+            }
             _ => false,
         }
     }
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index e139ac8581e..1691de93bdd 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -296,7 +296,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
             let (short, name) = item_ty_to_strs(myty.unwrap());
             write!(
                 w,
-                "<h2 id=\"{id}\" class=\"section-header\">\
+                "<h2 id=\"{id}\" class=\"small-section-header\">\
                     <a href=\"#{id}\">{name}</a>\
                  </h2>\n{}",
                 ITEM_TABLE_OPEN,
diff --git a/src/librustdoc/html/static/css/themes/ayu.css b/src/librustdoc/html/static/css/themes/ayu.css
index f4f654f4745..dea6d08396f 100644
--- a/src/librustdoc/html/static/css/themes/ayu.css
+++ b/src/librustdoc/html/static/css/themes/ayu.css
@@ -219,7 +219,7 @@ a {
 a.srclink,
 a#toggle-all-docs,
 a.anchor,
-.section-header a,
+.small-section-header a,
 #source-sidebar a,
 pre.rust a,
 .sidebar a,
diff --git a/src/librustdoc/html/static/css/themes/dark.css b/src/librustdoc/html/static/css/themes/dark.css
index b10b8020380..6e2cbbecbf7 100644
--- a/src/librustdoc/html/static/css/themes/dark.css
+++ b/src/librustdoc/html/static/css/themes/dark.css
@@ -181,7 +181,7 @@ a {
 a.srclink,
 a#toggle-all-docs,
 a.anchor,
-.section-header a,
+.small-section-header a,
 #source-sidebar a,
 pre.rust a,
 .sidebar a,
diff --git a/src/librustdoc/html/static/css/themes/light.css b/src/librustdoc/html/static/css/themes/light.css
index 97f32ac1f7d..4bf411d459a 100644
--- a/src/librustdoc/html/static/css/themes/light.css
+++ b/src/librustdoc/html/static/css/themes/light.css
@@ -176,7 +176,7 @@ a {
 a.srclink,
 a#toggle-all-docs,
 a.anchor,
-.section-header a,
+.small-section-header a,
 #source-sidebar a,
 pre.rust a,
 .sidebar a,
diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js
index c2ea54abd2e..7c55d10836c 100644
--- a/src/librustdoc/html/static/js/search.js
+++ b/src/librustdoc/html/static/js/search.js
@@ -4,32 +4,34 @@
 (function() {
 // This mapping table should match the discriminants of
 // `rustdoc::html::item_type::ItemType` type in Rust.
-var itemTypes = ["mod",
-                    "externcrate",
-                    "import",
-                    "struct",
-                    "enum",
-                    "fn",
-                    "type",
-                    "static",
-                    "trait",
-                    "impl",
-                    "tymethod",
-                    "method",
-                    "structfield",
-                    "variant",
-                    "macro",
-                    "primitive",
-                    "associatedtype",
-                    "constant",
-                    "associatedconstant",
-                    "union",
-                    "foreigntype",
-                    "keyword",
-                    "existential",
-                    "attr",
-                    "derive",
-                    "traitalias"];
+var itemTypes = [
+    "mod",
+    "externcrate",
+    "import",
+    "struct",
+    "enum",
+    "fn",
+    "type",
+    "static",
+    "trait",
+    "impl",
+    "tymethod",
+    "method",
+    "structfield",
+    "variant",
+    "macro",
+    "primitive",
+    "associatedtype",
+    "constant",
+    "associatedconstant",
+    "union",
+    "foreigntype",
+    "keyword",
+    "existential",
+    "attr",
+    "derive",
+    "traitalias",
+];
 
 // used for special search precedence
 var TY_PRIMITIVE = itemTypes.indexOf("primitive");
diff --git a/src/test/rustdoc-gui/headers-color.goml b/src/test/rustdoc-gui/headers-color.goml
index 7002812bb62..03b10e3f78d 100644
--- a/src/test/rustdoc-gui/headers-color.goml
+++ b/src/test/rustdoc-gui/headers-color.goml
@@ -18,7 +18,10 @@ goto: file://|DOC_PATH|/test_docs/struct.Foo.html#method.must_use
 assert-css: ("#method\.must_use", {"color": "rgb(197, 197, 197)", "background-color": "rgba(255, 236, 164, 0.06)"}, ALL)
 
 goto: file://|DOC_PATH|/test_docs/index.html
-assert-css: (".section-header a", {"color": "rgb(197, 197, 197)"}, ALL)
+assert-css: (".small-section-header a", {"color": "rgb(197, 197, 197)"}, ALL)
+
+goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html
+assert-css: (".section-header a", {"color": "rgb(57, 175, 215)"}, ALL)
 
 // Dark theme
 local-storage: {"rustdoc-theme": "dark", "rustdoc-preferred-dark-theme": "dark", "rustdoc-use-system-theme": "false"}
@@ -34,7 +37,10 @@ goto: file://|DOC_PATH|/test_docs/struct.Foo.html#method.must_use
 assert-css: ("#method\.must_use", {"color": "rgb(221, 221, 221)", "background-color": "rgb(73, 74, 61)"}, ALL)
 
 goto: file://|DOC_PATH|/test_docs/index.html
-assert-css: (".section-header a", {"color": "rgb(221, 221, 221)"}, ALL)
+assert-css: (".small-section-header a", {"color": "rgb(221, 221, 221)"}, ALL)
+
+goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html
+assert-css: (".section-header a", {"color": "rgb(210, 153, 29)"}, ALL)
 
 // Light theme
 local-storage: {"rustdoc-theme": "light", "rustdoc-use-system-theme": "false"}
@@ -52,4 +58,7 @@ goto: file://|DOC_PATH|/test_docs/struct.Foo.html#method.must_use
 assert-css: ("#method\.must_use", {"color": "rgb(0, 0, 0)", "background-color": "rgb(253, 255, 211)"}, ALL)
 
 goto: file://|DOC_PATH|/test_docs/index.html
-assert-css: (".section-header a", {"color": "rgb(0, 0, 0)"}, ALL)
+assert-css: (".small-section-header a", {"color": "rgb(0, 0, 0)"}, ALL)
+
+goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html
+assert-css: (".section-header a", {"color": "rgb(56, 115, 173)"}, ALL)
diff --git a/src/test/ui/cast/cast-int-to-char.rs b/src/test/ui/cast/cast-int-to-char.rs
new file mode 100644
index 00000000000..379956968bd
--- /dev/null
+++ b/src/test/ui/cast/cast-int-to-char.rs
@@ -0,0 +1,9 @@
+fn foo<T>(_t: T) {}
+
+fn main() {
+    foo::<u32>('0');  //~ ERROR
+    foo::<i32>('0');  //~ ERROR
+    foo::<u64>('0');  //~ ERROR
+    foo::<i64>('0');  //~ ERROR
+    foo::<char>(0u32);  //~ ERROR
+}
diff --git a/src/test/ui/cast/cast-int-to-char.stderr b/src/test/ui/cast/cast-int-to-char.stderr
new file mode 100644
index 00000000000..55b9462db8d
--- /dev/null
+++ b/src/test/ui/cast/cast-int-to-char.stderr
@@ -0,0 +1,53 @@
+error[E0308]: mismatched types
+  --> $DIR/cast-int-to-char.rs:4:16
+   |
+LL |     foo::<u32>('0');
+   |                ^^^ expected `u32`, found `char`
+   |
+help: you can cast a `char` to a `u32`, since a `char` always occupies 4 bytes
+   |
+LL |     foo::<u32>('0' as u32);
+   |                    ++++++
+
+error[E0308]: mismatched types
+  --> $DIR/cast-int-to-char.rs:5:16
+   |
+LL |     foo::<i32>('0');
+   |                ^^^ expected `i32`, found `char`
+   |
+help: you can cast a `char` to an `i32`, since a `char` always occupies 4 bytes
+   |
+LL |     foo::<i32>('0' as i32);
+   |                    ++++++
+
+error[E0308]: mismatched types
+  --> $DIR/cast-int-to-char.rs:6:16
+   |
+LL |     foo::<u64>('0');
+   |                ^^^ expected `u64`, found `char`
+   |
+help: you can cast a `char` to a `u64`, since a `char` always occupies 4 bytes
+   |
+LL |     foo::<u64>('0' as u64);
+   |                    ++++++
+
+error[E0308]: mismatched types
+  --> $DIR/cast-int-to-char.rs:7:16
+   |
+LL |     foo::<i64>('0');
+   |                ^^^ expected `i64`, found `char`
+   |
+help: you can cast a `char` to an `i64`, since a `char` always occupies 4 bytes
+   |
+LL |     foo::<i64>('0' as i64);
+   |                    ++++++
+
+error[E0308]: mismatched types
+  --> $DIR/cast-int-to-char.rs:8:17
+   |
+LL |     foo::<char>(0u32);
+   |                 ^^^^ expected `char`, found `u32`
+
+error: aborting due to 5 previous errors
+
+For more information about this error, try `rustc --explain E0308`.
diff --git a/src/test/ui/match/match-type-err-first-arm.stderr b/src/test/ui/match/match-type-err-first-arm.stderr
index fd489afa84d..1cfe7ce1ed7 100644
--- a/src/test/ui/match/match-type-err-first-arm.stderr
+++ b/src/test/ui/match/match-type-err-first-arm.stderr
@@ -6,6 +6,11 @@ LL | fn test_func1(n: i32) -> i32 {
 LL |     match n {
 LL |         12 => 'b',
    |               ^^^ expected `i32`, found `char`
+   |
+help: you can cast a `char` to an `i32`, since a `char` always occupies 4 bytes
+   |
+LL |         12 => 'b' as i32,
+   |                   ++++++
 
 error[E0308]: `match` arms have incompatible types
   --> $DIR/match-type-err-first-arm.rs:18:14
diff --git a/src/test/ui/packed/packed-struct-drop-aligned.rs b/src/test/ui/packed/packed-struct-drop-aligned.rs
index fab3bbedac6..b95cdbbbaad 100644
--- a/src/test/ui/packed/packed-struct-drop-aligned.rs
+++ b/src/test/ui/packed/packed-struct-drop-aligned.rs
@@ -1,6 +1,10 @@
 // run-pass
+#![feature(generators)]
+#![feature(generator_trait)]
 use std::cell::Cell;
 use std::mem;
+use std::ops::Generator;
+use std::pin::Pin;
 
 struct Aligned<'a> {
     drop_count: &'a Cell<usize>
@@ -19,15 +23,35 @@ impl<'a> Drop for Aligned<'a> {
     }
 }
 
+#[repr(transparent)]
+struct NotCopy(u8);
+
 #[repr(packed)]
-struct Packed<'a>(u8, Aligned<'a>);
+struct Packed<'a>(NotCopy, Aligned<'a>);
 
 fn main() {
     let drop_count = &Cell::new(0);
     {
-        let mut p = Packed(0, Aligned { drop_count });
+        let mut p = Packed(NotCopy(0), Aligned { drop_count });
         p.1 = Aligned { drop_count };
         assert_eq!(drop_count.get(), 1);
     }
     assert_eq!(drop_count.get(), 2);
+
+    let drop_count = &Cell::new(0);
+    let mut g = || {
+        let mut p = Packed(NotCopy(0), Aligned { drop_count });
+        let _ = &p;
+        p.1 = Aligned { drop_count };
+        assert_eq!(drop_count.get(), 1);
+        // Test that a generator drop function moves a value from a packed
+        // struct to a separate local before dropping it. We move out the
+        // first field to generate and open drop for the second field.
+        drop(p.0);
+        yield;
+    };
+    Pin::new(&mut g).resume(());
+    assert_eq!(drop_count.get(), 1);
+    drop(g);
+    assert_eq!(drop_count.get(), 2);
 }
diff --git a/src/test/ui/parser/misspelled-macro-rules.fixed b/src/test/ui/parser/misspelled-macro-rules.fixed
new file mode 100644
index 00000000000..62be913d85f
--- /dev/null
+++ b/src/test/ui/parser/misspelled-macro-rules.fixed
@@ -0,0 +1,13 @@
+// Regression test for issue #91227.
+
+// run-rustfix
+
+#![allow(unused_macros)]
+
+macro_rules! thing {
+//~^ ERROR: expected one of
+//~| HELP: perhaps you meant to define a macro
+    () => {}
+}
+
+fn main() {}
diff --git a/src/test/ui/parser/misspelled-macro-rules.rs b/src/test/ui/parser/misspelled-macro-rules.rs
new file mode 100644
index 00000000000..4290e6e5e4c
--- /dev/null
+++ b/src/test/ui/parser/misspelled-macro-rules.rs
@@ -0,0 +1,13 @@
+// Regression test for issue #91227.
+
+// run-rustfix
+
+#![allow(unused_macros)]
+
+marco_rules! thing {
+//~^ ERROR: expected one of
+//~| HELP: perhaps you meant to define a macro
+    () => {}
+}
+
+fn main() {}
diff --git a/src/test/ui/parser/misspelled-macro-rules.stderr b/src/test/ui/parser/misspelled-macro-rules.stderr
new file mode 100644
index 00000000000..56df7123819
--- /dev/null
+++ b/src/test/ui/parser/misspelled-macro-rules.stderr
@@ -0,0 +1,10 @@
+error: expected one of `(`, `[`, or `{`, found `thing`
+  --> $DIR/misspelled-macro-rules.rs:7:14
+   |
+LL | marco_rules! thing {
+   | -----------  ^^^^^ expected one of `(`, `[`, or `{`
+   | |
+   | help: perhaps you meant to define a macro: `macro_rules`
+
+error: aborting due to previous error
+
diff --git a/src/test/ui/proc-macro/macro-brackets.stderr b/src/test/ui/proc-macro/macro-brackets.stderr
index 9aaf612eb54..d3516375291 100644
--- a/src/test/ui/proc-macro/macro-brackets.stderr
+++ b/src/test/ui/proc-macro/macro-brackets.stderr
@@ -3,6 +3,11 @@ error[E0308]: mismatched types
    |
 LL | id![static X: u32 = 'a';];
    |                     ^^^ expected `u32`, found `char`
+   |
+help: you can cast a `char` to a `u32`, since a `char` always occupies 4 bytes
+   |
+LL | id![static X: u32 = 'a' as u32;];
+   |                         ++++++
 
 error: aborting due to previous error