Skip to content

Commit f2f526c

Browse files
committed
Auto merge of rust-lang#117996 - matthiaskrgr:rollup-sp48bl4, r=matthiaskrgr
Rollup of 5 pull requests Successful merges: - rust-lang#117892 (Detect more `=>` typos) - rust-lang#117959 (Better handle type errors involving `Self` literals) - rust-lang#117980 (fix: Update CONTRIBUTING.md recommend -> recommended) - rust-lang#117982 (bootstrap: only show PGO warnings when verbose) - rust-lang#117990 (Tweak error and move tests) Failed merges: - rust-lang#117944 (some additional region refactorings) r? `@ghost` `@rustbot` modify labels: rollup
2 parents a577704 + a5d7f8b commit f2f526c

File tree

74 files changed

+193
-28
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+193
-28
lines changed

CONTRIBUTING.md

+1-1

compiler/rustc_ast/src/token.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ impl TokenKind {
388388
match *self {
389389
Comma => Some(vec![Dot, Lt, Semi]),
390390
Semi => Some(vec![Colon, Comma]),
391-
FatArrow => Some(vec![Eq, RArrow]),
391+
Colon => Some(vec![Semi]),
392+
FatArrow => Some(vec![Eq, RArrow, Ge, Gt]),
392393
_ => None,
393394
}
394395
}

compiler/rustc_hir_typeck/src/demand.rs

+54
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3131
}
3232

3333
self.annotate_alternative_method_deref(err, expr, error);
34+
self.explain_self_literal(err, expr, expected, expr_ty);
3435

3536
// Use `||` to give these suggestions a precedence
3637
let suggested = self.suggest_missing_parentheses(err, expr)
@@ -1027,6 +1028,59 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10271028
return false;
10281029
}
10291030

1031+
fn explain_self_literal(
1032+
&self,
1033+
err: &mut Diagnostic,
1034+
expr: &hir::Expr<'tcx>,
1035+
expected: Ty<'tcx>,
1036+
found: Ty<'tcx>,
1037+
) {
1038+
match expr.peel_drop_temps().kind {
1039+
hir::ExprKind::Struct(
1040+
hir::QPath::Resolved(
1041+
None,
1042+
hir::Path { res: hir::def::Res::SelfTyAlias { alias_to, .. }, span, .. },
1043+
),
1044+
..,
1045+
)
1046+
| hir::ExprKind::Call(
1047+
hir::Expr {
1048+
kind:
1049+
hir::ExprKind::Path(hir::QPath::Resolved(
1050+
None,
1051+
hir::Path {
1052+
res: hir::def::Res::SelfTyAlias { alias_to, .. },
1053+
span,
1054+
..
1055+
},
1056+
)),
1057+
..
1058+
},
1059+
..,
1060+
) => {
1061+
if let Some(hir::Node::Item(hir::Item {
1062+
kind: hir::ItemKind::Impl(hir::Impl { self_ty, .. }),
1063+
..
1064+
})) = self.tcx.hir().get_if_local(*alias_to)
1065+
{
1066+
err.span_label(self_ty.span, "this is the type of the `Self` literal");
1067+
}
1068+
if let ty::Adt(e_def, e_args) = expected.kind()
1069+
&& let ty::Adt(f_def, _f_args) = found.kind()
1070+
&& e_def == f_def
1071+
{
1072+
err.span_suggestion_verbose(
1073+
*span,
1074+
"use the type name directly",
1075+
self.tcx.value_path_str_with_args(*alias_to, e_args),
1076+
Applicability::MaybeIncorrect,
1077+
);
1078+
}
1079+
}
1080+
_ => {}
1081+
}
1082+
}
1083+
10301084
fn note_wrong_return_ty_due_to_generic_arg(
10311085
&self,
10321086
err: &mut Diagnostic,

compiler/rustc_parse/src/errors.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2278,9 +2278,8 @@ pub(crate) enum InvalidMutInPattern {
22782278
#[note(parse_note_mut_pattern_usage)]
22792279
NonIdent {
22802280
#[primary_span]
2281-
#[suggestion(code = "{pat}", applicability = "machine-applicable")]
2281+
#[suggestion(code = "", applicability = "machine-applicable")]
22822282
span: Span,
2283-
pat: String,
22842283
},
22852284
}
22862285

compiler/rustc_parse/src/parser/expr.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -2904,15 +2904,16 @@ impl<'a> Parser<'a> {
29042904
"=>",
29052905
Applicability::MachineApplicable,
29062906
);
2907-
err.emit();
2908-
this.bump();
2909-
} else if matches!(
2910-
(&this.prev_token.kind, &this.token.kind),
2911-
(token::DotDotEq, token::Gt)
2912-
) {
2913-
// `error_inclusive_range_match_arrow` handles cases like `0..=> {}`,
2914-
// so we suppress the error here
2915-
err.delay_as_bug();
2907+
if matches!(
2908+
(&this.prev_token.kind, &this.token.kind),
2909+
(token::DotDotEq, token::Gt)
2910+
) {
2911+
// `error_inclusive_range_match_arrow` handles cases like `0..=> {}`,
2912+
// so we suppress the error here
2913+
err.delay_as_bug();
2914+
} else {
2915+
err.emit();
2916+
}
29162917
this.bump();
29172918
} else {
29182919
return Err(err);

compiler/rustc_parse/src/parser/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -830,8 +830,8 @@ impl<'a> Parser<'a> {
830830
// https://github.com/rust-lang/rust/issues/72373
831831
if self.prev_token.is_ident() && self.token.kind == token::DotDot {
832832
let msg = format!(
833-
"if you meant to bind the contents of \
834-
the rest of the array pattern into `{}`, use `@`",
833+
"if you meant to bind the contents of the rest of the array \
834+
pattern into `{}`, use `@`",
835835
pprust::token_to_string(&self.prev_token)
836836
);
837837
expect_err

compiler/rustc_parse/src/parser/pat.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,13 @@ impl<'a> Parser<'a> {
638638

639639
/// Error on `mut $pat` where `$pat` is not an ident.
640640
fn ban_mut_general_pat(&self, lo: Span, pat: &Pat, changed_any_binding: bool) {
641-
let span = lo.to(pat.span);
642-
let pat = pprust::pat_to_string(&pat);
643-
644641
self.sess.emit_err(if changed_any_binding {
645-
InvalidMutInPattern::NestedIdent { span, pat }
642+
InvalidMutInPattern::NestedIdent {
643+
span: lo.to(pat.span),
644+
pat: pprust::pat_to_string(&pat),
645+
}
646646
} else {
647-
InvalidMutInPattern::NonIdent { span, pat }
647+
InvalidMutInPattern::NonIdent { span: lo.until(pat.span) }
648648
});
649649
}
650650

src/bootstrap/src/core/build_steps/compile.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,9 @@ impl Step for Rustc {
887887
} else if let Some(path) = &builder.config.rust_profile_use {
888888
if compiler.stage == 1 {
889889
cargo.rustflag(&format!("-Cprofile-use={path}"));
890-
cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function");
890+
if builder.is_verbose() {
891+
cargo.rustflag("-Cllvm-args=-pgo-warn-missing-function");
892+
}
891893
true
892894
} else {
893895
false

tests/ui/parser/issues/issue-32501.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: `mut` must be followed by a named binding
22
--> $DIR/issue-32501.rs:7:9
33
|
44
LL | let mut _ = 0;
5-
| ^^^^^ help: remove the `mut` prefix: `_`
5+
| ^^^^ help: remove the `mut` prefix
66
|
77
= note: `mut` may be followed by `variable` and `variable @ pattern`
88

tests/ui/parser/issues/issue-65122-mac-invoc-in-mut-patterns.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: `mut` must be followed by a named binding
22
--> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:6:13
33
|
44
LL | let mut $eval = ();
5-
| ^^^^^^^^^ help: remove the `mut` prefix: `does_not_exist!()`
5+
| ^^^^ help: remove the `mut` prefix
66
...
77
LL | mac1! { does_not_exist!() }
88
| --------------------------- in this macro invocation
@@ -25,7 +25,7 @@ error: `mut` must be followed by a named binding
2525
--> $DIR/issue-65122-mac-invoc-in-mut-patterns.rs:13:13
2626
|
2727
LL | let mut $eval = ();
28-
| ^^^ help: remove the `mut` prefix: `does_not_exist!()`
28+
| ^^^ help: remove the `mut` prefix
2929
...
3030
LL | mac2! { does_not_exist!() }
3131
| --------------------------- in this macro invocation
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-rustfix
2+
fn main() {
3+
match 1 {
4+
1 => {} //~ ERROR
5+
_ => { let _: u16 = 2u16; } //~ ERROR
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// run-rustfix
2+
fn main() {
3+
match 1 {
4+
1 >= {} //~ ERROR
5+
_ => { let _: u16 = 2u8; } //~ ERROR
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: expected one of `...`, `..=`, `..`, `=>`, `if`, or `|`, found `>=`
2+
--> $DIR/recover-ge-as-fat-arrow.rs:4:11
3+
|
4+
LL | 1 >= {}
5+
| ^^
6+
| |
7+
| expected one of `...`, `..=`, `..`, `=>`, `if`, or `|`
8+
| help: use a fat arrow to start a match arm: `=>`
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/recover-ge-as-fat-arrow.rs:5:29
12+
|
13+
LL | _ => { let _: u16 = 2u8; }
14+
| --- ^^^ expected `u16`, found `u8`
15+
| |
16+
| expected due to this
17+
|
18+
help: change the type of the numeric literal from `u8` to `u16`
19+
|
20+
LL | _ => { let _: u16 = 2u16; }
21+
| ~~~
22+
23+
error: aborting due to 2 previous errors
24+
25+
For more information about this error, try `rustc --explain E0308`.

tests/ui/parser/mut-patterns.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ error: `mut` must be followed by a named binding
22
--> $DIR/mut-patterns.rs:9:9
33
|
44
LL | let mut _ = 0;
5-
| ^^^^^ help: remove the `mut` prefix: `_`
5+
| ^^^^ help: remove the `mut` prefix
66
|
77
= note: `mut` may be followed by `variable` and `variable @ pattern`
88

99
error: `mut` must be followed by a named binding
1010
--> $DIR/mut-patterns.rs:10:9
1111
|
1212
LL | let mut (_, _) = (0, 0);
13-
| ^^^^^^^^^^ help: remove the `mut` prefix: `(_, _)`
13+
| ^^^^ help: remove the `mut` prefix
1414
|
1515
= note: `mut` may be followed by `variable` and `variable @ pattern`
1616

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fn main() {
2+
let val = 42;
3+
let x = match val {
4+
(0 if true) => {
5+
//~^ ERROR expected identifier, found keyword `if`
6+
//~| ERROR expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found keyword `if`
7+
//~| ERROR expected one of `)`, `,`, `@`, or `|`, found keyword `true`
8+
//~| ERROR mismatched types
9+
42u8
10+
}
11+
_ => 0u8,
12+
};
13+
let _y: u32 = x; //~ ERROR mismatched types
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error: expected identifier, found keyword `if`
2+
--> $DIR/recover-parens-around-match-arm-head.rs:4:12
3+
|
4+
LL | (0 if true) => {
5+
| ^^ expected identifier, found keyword
6+
7+
error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found keyword `if`
8+
--> $DIR/recover-parens-around-match-arm-head.rs:4:12
9+
|
10+
LL | (0 if true) => {
11+
| -^^ expected one of `)`, `,`, `...`, `..=`, `..`, or `|`
12+
| |
13+
| help: missing `,`
14+
15+
error: expected one of `)`, `,`, `@`, or `|`, found keyword `true`
16+
--> $DIR/recover-parens-around-match-arm-head.rs:4:15
17+
|
18+
LL | (0 if true) => {
19+
| -^^^^ expected one of `)`, `,`, `@`, or `|`
20+
| |
21+
| help: missing `,`
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/recover-parens-around-match-arm-head.rs:4:9
25+
|
26+
LL | let x = match val {
27+
| --- this expression has type `{integer}`
28+
LL | (0 if true) => {
29+
| ^^^^^^^^^^^ expected integer, found `(_, _, _)`
30+
|
31+
= note: expected type `{integer}`
32+
found tuple `(_, _, _)`
33+
34+
error[E0308]: mismatched types
35+
--> $DIR/recover-parens-around-match-arm-head.rs:13:19
36+
|
37+
LL | let _y: u32 = x;
38+
| --- ^ expected `u32`, found `u8`
39+
| |
40+
| expected due to this
41+
|
42+
help: you can convert a `u8` to a `u32`
43+
|
44+
LL | let _y: u32 = x.into();
45+
| +++++++
46+
47+
error: aborting due to 5 previous errors
48+
49+
For more information about this error, try `rustc --explain E0308`.

tests/ui/self/self_type_keyword.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error: `mut` must be followed by a named binding
1414
--> $DIR/self_type_keyword.rs:16:9
1515
|
1616
LL | mut Self => (),
17-
| ^^^^^^^^ help: remove the `mut` prefix: `Self`
17+
| ^^^^ help: remove the `mut` prefix
1818
|
1919
= note: `mut` may be followed by `variable` and `variable @ pattern`
2020

tests/ui/structs/struct-path-self-type-mismatch.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ error[E0308]: mismatched types
2424
--> $DIR/struct-path-self-type-mismatch.rs:13:9
2525
|
2626
LL | impl<T> Foo<T> {
27-
| - found type parameter
27+
| - ------ this is the type of the `Self` literal
28+
| |
29+
| found type parameter
2830
LL | fn new<U>(u: U) -> Foo<U> {
2931
| - ------ expected `Foo<U>` because of return type
3032
| |
@@ -40,6 +42,10 @@ LL | | }
4042
found struct `Foo<T>`
4143
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
4244
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
45+
help: use the type name directly
46+
|
47+
LL | Foo::<U> {
48+
| ~~~~~~~~
4349

4450
error: aborting due to 3 previous errors
4551

0 commit comments

Comments
 (0)