Skip to content

Rollup of 7 pull requests #112512

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

Merged
merged 18 commits into from
Jun 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
3983881
take care module name for suggesting surround the struct literal in p…
chenyukang Jun 6, 2023
e3071ea
reword the message to suggest surrounding with parentheses
chenyukang Jun 6, 2023
f531fec
Give more helpful progress messages in `Assemble`
jyn514 Jun 9, 2023
f54e757
remove unwrap
chenyukang Jun 10, 2023
48e410e
Lazy load ntdll functions on UWP
bdbai Jun 10, 2023
08e0c12
Migrate GUI colors test to original CSS color format
GuillaumeGomez Jun 10, 2023
cd523f2
Keep uwp specific code in sync with windows-sys
bdbai Jun 10, 2023
83d7826
abs_sub: fix typo 0[-:][+.]0
Jun 10, 2023
e5fccf9
Update links to Rust Reference page on literals in diagnostic
SamZhang3 Jun 10, 2023
6336da9
Use a better link
SamZhang3 Jun 10, 2023
a995255
iat selection: normalize self ty & completely erase bound vars
fmease Jun 10, 2023
e19a509
Rollup merge of #112475 - chenyukang:yukang-fix-112278, r=compiler-er…
matthiaskrgr Jun 10, 2023
dbe31bb
Rollup merge of #112477 - jyn514:assemble-info, r=clubby789
matthiaskrgr Jun 10, 2023
e4f6b3d
Rollup merge of #112484 - bdbai:fix/uwpntdll, r=ChrisDenton
matthiaskrgr Jun 10, 2023
fcf621e
Rollup merge of #112492 - GuillaumeGomez:migrate-gui-test-color-13, r…
matthiaskrgr Jun 10, 2023
e9666d4
Rollup merge of #112493 - fmease:iat-select-complete-bound-var-erasur…
matthiaskrgr Jun 10, 2023
0ff35f2
Rollup merge of #112497 - icecream17:patch-1, r=Mark-Simulacrum
matthiaskrgr Jun 10, 2023
46b64aa
Rollup merge of #112498 - SamZhang3:rust-reference-link-update, r=Nil…
matthiaskrgr Jun 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ parse_struct_literal_body_without_path =

parse_struct_literal_needing_parens =
invalid struct literal
.suggestion = you might need to surround the struct literal in parentheses
.suggestion = you might need to surround the struct literal with parentheses

parse_struct_literal_not_allowed_here = struct literals are not allowed here
.suggestion = surround the struct literal with parentheses
Expand Down
25 changes: 18 additions & 7 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,13 +751,24 @@ impl<'a> Parser<'a> {
tail.could_be_bare_literal = true;
if maybe_struct_name.is_ident() && can_be_struct_literal {
// Account for `if Example { a: one(), }.is_pos() {}`.
Err(self.sess.create_err(StructLiteralNeedingParens {
span: maybe_struct_name.span.to(expr.span),
sugg: StructLiteralNeedingParensSugg {
before: maybe_struct_name.span.shrink_to_lo(),
after: expr.span.shrink_to_hi(),
},
}))
// expand `before` so that we take care of module path such as:
// `foo::Bar { ... } `
// we expect to suggest `(foo::Bar { ... })` instead of `foo::(Bar { ... })`
let sm = self.sess.source_map();
let before = maybe_struct_name.span.shrink_to_lo();
if let Ok(extend_before) = sm.span_extend_prev_while(before, |t| {
t.is_alphanumeric() || t == ':' || t == '_'
}) {
Err(self.sess.create_err(StructLiteralNeedingParens {
span: maybe_struct_name.span.to(expr.span),
sugg: StructLiteralNeedingParensSugg {
before: extend_before.shrink_to_lo(),
after: expr.span.shrink_to_hi(),
},
}))
} else {
return None;
}
} else {
self.sess.emit_err(StructLiteralBodyWithoutPath {
span: expr.span,
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,21 @@ impl SourceMap {
})
}

/// Extends the given `Span` to previous character while the previous character matches the predicate
pub fn span_extend_prev_while(
&self,
span: Span,
f: impl Fn(char) -> bool,
) -> Result<Span, SpanSnippetError> {
self.span_to_source(span, |s, start, _end| {
let n = s[..start]
.char_indices()
.rfind(|&(_, c)| !f(c))
.map_or(start, |(i, _)| start - i - 1);
Ok(span.with_lo(span.lo() - BytePos(n as u32)))
})
}

/// Extends the given `Span` to just before the next occurrence of `c`.
pub fn span_extend_to_next_char(&self, sp: Span, c: char, accept_newlines: bool) -> Span {
if let Ok(next_source) = self.span_to_next_source(sp) {
Expand Down
32 changes: 32 additions & 0 deletions tests/ui/parser/issues/issue-111692.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
mod module {
#[derive(Eq, PartialEq)]
pub struct Type {
pub x: u8,
pub y: u8,
}

pub const C: u8 = 32u8;
}

fn test(x: module::Type) {
if x == module::Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal
}
}

fn test2(x: module::Type) {
if x ==module::Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal
}
}


fn test3(x: module::Type) {
if x == Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal
}
}

fn test4(x: module::Type) {
if x == demo_module::Type { x: module::C, y: 1 } { //~ ERROR invalid struct literal
}
}

fn main() { }
46 changes: 46 additions & 0 deletions tests/ui/parser/issues/issue-111692.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
error: invalid struct literal
--> $DIR/issue-111692.rs:12:21
|
LL | if x == module::Type { x: module::C, y: 1 } {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: you might need to surround the struct literal with parentheses
|
LL | if x == (module::Type { x: module::C, y: 1 }) {
| + +

error: invalid struct literal
--> $DIR/issue-111692.rs:17:20
|
LL | if x ==module::Type { x: module::C, y: 1 } {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: you might need to surround the struct literal with parentheses
|
LL | if x ==(module::Type { x: module::C, y: 1 }) {
| + +

error: invalid struct literal
--> $DIR/issue-111692.rs:23:13
|
LL | if x == Type { x: module::C, y: 1 } {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: you might need to surround the struct literal with parentheses
|
LL | if x == (Type { x: module::C, y: 1 }) {
| + +

error: invalid struct literal
--> $DIR/issue-111692.rs:28:26
|
LL | if x == demo_module::Type { x: module::C, y: 1 } {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: you might need to surround the struct literal with parentheses
|
LL | if x == (demo_module::Type { x: module::C, y: 1 }) {
| + +

error: aborting due to 4 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error: invalid struct literal
LL | if Example { a: one(), }.is_pos() {
| ^^^^^^^^^^^^^^^^^^^^^
|
help: you might need to surround the struct literal in parentheses
help: you might need to surround the struct literal with parentheses
|
LL | if (Example { a: one(), }).is_pos() {
| + +
Expand Down