Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 5 pull requests #91677

Merged
merged 10 commits into from
Dec 9, 2021
29 changes: 25 additions & 4 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_typeck/src/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
54 changes: 28 additions & 26 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
15 changes: 12 additions & 3 deletions src/test/rustdoc-gui/headers-color.goml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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"}
Expand All @@ -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)
9 changes: 9 additions & 0 deletions src/test/ui/cast/cast-int-to-char.rs
Original file line number Diff line number Diff line change
@@ -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
}
53 changes: 53 additions & 0 deletions src/test/ui/cast/cast-int-to-char.stderr
Original file line number Diff line number Diff line change
@@ -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`.
5 changes: 5 additions & 0 deletions src/test/ui/match/match-type-err-first-arm.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 26 additions & 2 deletions src/test/ui/packed/packed-struct-drop-aligned.rs
Original file line number Diff line number Diff line change
@@ -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>
Expand All @@ -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);
}
13 changes: 13 additions & 0 deletions src/test/ui/parser/misspelled-macro-rules.fixed
Original file line number Diff line number Diff line change
@@ -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() {}
13 changes: 13 additions & 0 deletions src/test/ui/parser/misspelled-macro-rules.rs
Original file line number Diff line number Diff line change
@@ -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() {}
10 changes: 10 additions & 0 deletions src/test/ui/parser/misspelled-macro-rules.stderr
Original file line number Diff line number Diff line change
@@ -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

5 changes: 5 additions & 0 deletions src/test/ui/proc-macro/macro-brackets.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down