Skip to content

Commit 98d027c

Browse files
authored
Rollup merge of rust-lang#94708 - notriddle:notriddle/cargo-toml-warning, r=lcnr
diagnostics: only talk about `Cargo.toml` if running under Cargo Fixes rust-lang#94646
2 parents e4a3627 + fbd4cfa commit 98d027c

13 files changed

+132
-33
lines changed

compiler/rustc_errors/src/diagnostic.rs

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::SuggestionStyle;
77
use crate::ToolMetadata;
88
use rustc_lint_defs::Applicability;
99
use rustc_serialize::json::Json;
10+
use rustc_span::edition::LATEST_STABLE_EDITION;
1011
use rustc_span::{MultiSpan, Span, DUMMY_SP};
1112
use std::fmt;
1213
use std::hash::{Hash, Hasher};
@@ -342,6 +343,18 @@ impl Diagnostic {
342343
self
343344
}
344345

346+
/// Help the user upgrade to the latest edition.
347+
/// This is factored out to make sure it does the right thing with `Cargo.toml`.
348+
pub fn help_use_latest_edition(&mut self) -> &mut Self {
349+
if std::env::var_os("CARGO").is_some() {
350+
self.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
351+
} else {
352+
self.help(&format!("pass `--edition {}` to `rustc`", LATEST_STABLE_EDITION));
353+
}
354+
self.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
355+
self
356+
}
357+
345358
/// Disallow attaching suggestions this diagnostic.
346359
/// Any suggestions attached e.g. with the `span_suggestion_*` methods
347360
/// (before and after the call to `disable_suggestions`) will be ignored.

compiler/rustc_errors/src/diagnostic_builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
409409
sp: impl Into<MultiSpan>,
410410
msg: &str,
411411
) -> &mut Self);
412+
forward!(pub fn help_use_latest_edition(&mut self,) -> &mut Self);
412413
forward!(pub fn set_is_lint(&mut self,) -> &mut Self);
413414

414415
forward!(pub fn disable_suggestions(&mut self,) -> &mut Self);

compiler/rustc_parse/src/parser/expr.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc_ast_pretty::pprust;
2020
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, PResult};
2121
use rustc_session::lint::builtin::BREAK_WITH_LABEL_AND_LOOP;
2222
use rustc_session::lint::BuiltinLintDiagnostics;
23-
use rustc_span::edition::LATEST_STABLE_EDITION;
2423
use rustc_span::source_map::{self, Span, Spanned};
2524
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2625
use rustc_span::{BytePos, Pos};
@@ -2712,8 +2711,7 @@ impl<'a> Parser<'a> {
27122711
let mut async_block_err = |e: &mut Diagnostic, span: Span| {
27132712
recover_async = true;
27142713
e.span_label(span, "`async` blocks are only allowed in Rust 2018 or later");
2715-
e.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
2716-
e.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
2714+
e.help_use_latest_edition();
27172715
};
27182716

27192717
while self.token != token::CloseDelim(close_delim) {

compiler/rustc_parse/src/parser/item.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, Visibility
1414
use rustc_ast::{MacArgs, MacCall, MacDelimiter};
1515
use rustc_ast_pretty::pprust;
1616
use rustc_errors::{struct_span_err, Applicability, PResult, StashKey};
17-
use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
17+
use rustc_span::edition::Edition;
1818
use rustc_span::lev_distance::lev_distance;
1919
use rustc_span::source_map::{self, Span};
2020
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -2102,8 +2102,7 @@ impl<'a> Parser<'a> {
21022102
let diag = self.diagnostic();
21032103
struct_span_err!(diag, span, E0670, "`async fn` is not permitted in Rust 2015")
21042104
.span_label(span, "to use `async fn`, switch to Rust 2018 or later")
2105-
.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION))
2106-
.note("for more on editions, read https://doc.rust-lang.org/edition-guide")
2105+
.help_use_latest_edition()
21072106
.emit();
21082107
}
21092108
}

compiler/rustc_typeck/src/check/expr.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use rustc_middle::ty::error::TypeError::{FieldMisMatch, Sorts};
4343
use rustc_middle::ty::subst::SubstsRef;
4444
use rustc_middle::ty::{self, AdtKind, Ty, TypeFoldable};
4545
use rustc_session::parse::feature_err;
46-
use rustc_span::edition::LATEST_STABLE_EDITION;
4746
use rustc_span::hygiene::DesugaringKind;
4847
use rustc_span::lev_distance::find_best_match_for_name;
4948
use rustc_span::source_map::Span;
@@ -2010,8 +2009,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20102009
// We know by construction that `<expr>.await` is either on Rust 2015
20112010
// or results in `ExprKind::Await`. Suggest switching the edition to 2018.
20122011
err.note("to `.await` a `Future`, switch to Rust 2018 or later");
2013-
err.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
2014-
err.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
2012+
err.help_use_latest_edition();
20152013
}
20162014

20172015
err.emit();

src/test/ui/async-await/edition-deny-async-fns-2015.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
44
LL | async fn foo() {}
55
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
66
|
7-
= help: set `edition = "2021"` in `Cargo.toml`
7+
= help: pass `--edition 2021` to `rustc`
88
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
99

1010
error[E0670]: `async fn` is not permitted in Rust 2015
@@ -13,7 +13,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
1313
LL | fn baz() { async fn foo() {} }
1414
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
1515
|
16-
= help: set `edition = "2021"` in `Cargo.toml`
16+
= help: pass `--edition 2021` to `rustc`
1717
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
1818

1919
error[E0670]: `async fn` is not permitted in Rust 2015
@@ -22,7 +22,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
2222
LL | async fn async_baz() {
2323
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
2424
|
25-
= help: set `edition = "2021"` in `Cargo.toml`
25+
= help: pass `--edition 2021` to `rustc`
2626
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
2727

2828
error[E0670]: `async fn` is not permitted in Rust 2015
@@ -31,7 +31,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
3131
LL | async fn bar() {}
3232
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
3333
|
34-
= help: set `edition = "2021"` in `Cargo.toml`
34+
= help: pass `--edition 2021` to `rustc`
3535
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
3636

3737
error[E0670]: `async fn` is not permitted in Rust 2015
@@ -40,7 +40,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
4040
LL | async fn foo() {}
4141
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
4242
|
43-
= help: set `edition = "2021"` in `Cargo.toml`
43+
= help: pass `--edition 2021` to `rustc`
4444
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
4545

4646
error[E0670]: `async fn` is not permitted in Rust 2015
@@ -49,7 +49,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
4949
LL | async fn foo() {}
5050
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
5151
|
52-
= help: set `edition = "2021"` in `Cargo.toml`
52+
= help: pass `--edition 2021` to `rustc`
5353
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
5454

5555
error[E0670]: `async fn` is not permitted in Rust 2015
@@ -58,7 +58,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
5858
LL | async fn bar() {}
5959
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
6060
|
61-
= help: set `edition = "2021"` in `Cargo.toml`
61+
= help: pass `--edition 2021` to `rustc`
6262
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
6363

6464
error[E0670]: `async fn` is not permitted in Rust 2015
@@ -67,7 +67,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
6767
LL | async fn foo() {}
6868
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
6969
|
70-
= help: set `edition = "2021"` in `Cargo.toml`
70+
= help: pass `--edition 2021` to `rustc`
7171
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
7272

7373
error[E0670]: `async fn` is not permitted in Rust 2015
@@ -76,7 +76,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
7676
LL | async fn bar() {}
7777
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
7878
|
79-
= help: set `edition = "2021"` in `Cargo.toml`
79+
= help: pass `--edition 2021` to `rustc`
8080
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
8181

8282
error[E0706]: functions in traits cannot be declared `async`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// rustc-env:CARGO=/usr/bin/cargo
2+
3+
use std::pin::Pin;
4+
use std::future::Future;
5+
6+
fn main() {}
7+
8+
fn await_on_struct_missing() {
9+
struct S;
10+
let x = S;
11+
x.await;
12+
//~^ ERROR no field `await` on type
13+
//~| NOTE unknown field
14+
//~| NOTE to `.await` a `Future`, switch to Rust 2018
15+
//~| HELP set `edition = "2021"` in `Cargo.toml`
16+
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
17+
}
18+
19+
fn await_on_struct_similar() {
20+
struct S {
21+
awai: u8,
22+
}
23+
let x = S { awai: 42 };
24+
x.await;
25+
//~^ ERROR no field `await` on type
26+
//~| HELP a field with a similar name exists
27+
//~| NOTE to `.await` a `Future`, switch to Rust 2018
28+
//~| HELP set `edition = "2021"` in `Cargo.toml`
29+
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
30+
}
31+
32+
fn await_on_63533(x: Pin<&mut dyn Future<Output = ()>>) {
33+
x.await;
34+
//~^ ERROR no field `await` on type
35+
//~| NOTE unknown field
36+
//~| NOTE to `.await` a `Future`, switch to Rust 2018
37+
//~| HELP set `edition = "2021"` in `Cargo.toml`
38+
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
39+
}
40+
41+
fn await_on_apit(x: impl Future<Output = ()>) {
42+
x.await;
43+
//~^ ERROR no field `await` on type
44+
//~| NOTE to `.await` a `Future`, switch to Rust 2018
45+
//~| HELP set `edition = "2021"` in `Cargo.toml`
46+
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0609]: no field `await` on type `await_on_struct_missing::S`
2+
--> $DIR/suggest-switching-edition-on-await-cargo.rs:11:7
3+
|
4+
LL | x.await;
5+
| ^^^^^ unknown field
6+
|
7+
= note: to `.await` a `Future`, switch to Rust 2018 or later
8+
= help: set `edition = "2021"` in `Cargo.toml`
9+
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
10+
11+
error[E0609]: no field `await` on type `await_on_struct_similar::S`
12+
--> $DIR/suggest-switching-edition-on-await-cargo.rs:24:7
13+
|
14+
LL | x.await;
15+
| ^^^^^ help: a field with a similar name exists: `awai`
16+
|
17+
= note: to `.await` a `Future`, switch to Rust 2018 or later
18+
= help: set `edition = "2021"` in `Cargo.toml`
19+
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
20+
21+
error[E0609]: no field `await` on type `Pin<&mut dyn Future<Output = ()>>`
22+
--> $DIR/suggest-switching-edition-on-await-cargo.rs:33:7
23+
|
24+
LL | x.await;
25+
| ^^^^^ unknown field
26+
|
27+
= note: to `.await` a `Future`, switch to Rust 2018 or later
28+
= help: set `edition = "2021"` in `Cargo.toml`
29+
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
30+
31+
error[E0609]: no field `await` on type `impl Future<Output = ()>`
32+
--> $DIR/suggest-switching-edition-on-await-cargo.rs:42:7
33+
|
34+
LL | x.await;
35+
| ^^^^^
36+
|
37+
= note: to `.await` a `Future`, switch to Rust 2018 or later
38+
= help: set `edition = "2021"` in `Cargo.toml`
39+
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
40+
41+
error: aborting due to 4 previous errors
42+
43+
For more information about this error, try `rustc --explain E0609`.

src/test/ui/async-await/suggest-switching-edition-on-await.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn await_on_struct_missing() {
1010
//~^ ERROR no field `await` on type
1111
//~| NOTE unknown field
1212
//~| NOTE to `.await` a `Future`, switch to Rust 2018
13-
//~| HELP set `edition = "2021"` in `Cargo.toml`
13+
//~| HELP pass `--edition 2021` to `rustc`
1414
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
1515
}
1616

@@ -23,7 +23,7 @@ fn await_on_struct_similar() {
2323
//~^ ERROR no field `await` on type
2424
//~| HELP a field with a similar name exists
2525
//~| NOTE to `.await` a `Future`, switch to Rust 2018
26-
//~| HELP set `edition = "2021"` in `Cargo.toml`
26+
//~| HELP pass `--edition 2021` to `rustc`
2727
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
2828
}
2929

@@ -32,14 +32,14 @@ fn await_on_63533(x: Pin<&mut dyn Future<Output = ()>>) {
3232
//~^ ERROR no field `await` on type
3333
//~| NOTE unknown field
3434
//~| NOTE to `.await` a `Future`, switch to Rust 2018
35-
//~| HELP set `edition = "2021"` in `Cargo.toml`
35+
//~| HELP pass `--edition 2021` to `rustc`
3636
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
3737
}
3838

3939
fn await_on_apit(x: impl Future<Output = ()>) {
4040
x.await;
4141
//~^ ERROR no field `await` on type
4242
//~| NOTE to `.await` a `Future`, switch to Rust 2018
43-
//~| HELP set `edition = "2021"` in `Cargo.toml`
43+
//~| HELP pass `--edition 2021` to `rustc`
4444
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
4545
}

src/test/ui/async-await/suggest-switching-edition-on-await.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | x.await;
55
| ^^^^^ unknown field
66
|
77
= note: to `.await` a `Future`, switch to Rust 2018 or later
8-
= help: set `edition = "2021"` in `Cargo.toml`
8+
= help: pass `--edition 2021` to `rustc`
99
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
1010

1111
error[E0609]: no field `await` on type `await_on_struct_similar::S`
@@ -15,7 +15,7 @@ LL | x.await;
1515
| ^^^^^ help: a field with a similar name exists: `awai`
1616
|
1717
= note: to `.await` a `Future`, switch to Rust 2018 or later
18-
= help: set `edition = "2021"` in `Cargo.toml`
18+
= help: pass `--edition 2021` to `rustc`
1919
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
2020

2121
error[E0609]: no field `await` on type `Pin<&mut dyn Future<Output = ()>>`
@@ -25,7 +25,7 @@ LL | x.await;
2525
| ^^^^^ unknown field
2626
|
2727
= note: to `.await` a `Future`, switch to Rust 2018 or later
28-
= help: set `edition = "2021"` in `Cargo.toml`
28+
= help: pass `--edition 2021` to `rustc`
2929
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
3030

3131
error[E0609]: no field `await` on type `impl Future<Output = ()>`
@@ -35,7 +35,7 @@ LL | x.await;
3535
| ^^^^^
3636
|
3737
= note: to `.await` a `Future`, switch to Rust 2018 or later
38-
= help: set `edition = "2021"` in `Cargo.toml`
38+
= help: pass `--edition 2021` to `rustc`
3939
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
4040

4141
error: aborting due to 4 previous errors

src/test/ui/editions/async-block-2015.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
async fn foo() {
22
//~^ ERROR `async fn` is not permitted in Rust 2015
33
//~| NOTE to use `async fn`, switch to Rust 2018 or later
4-
//~| HELP set `edition = "2021"` in `Cargo.toml`
4+
//~| HELP pass `--edition 2021` to `rustc`
55
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
66

77
let x = async {};
@@ -11,15 +11,15 @@ async fn foo() {
1111
let x = 42;
1212
//~^ ERROR expected identifier, found keyword `let`
1313
//~| NOTE expected identifier, found keyword
14-
//~| HELP set `edition = "2021"` in `Cargo.toml`
14+
//~| HELP pass `--edition 2021` to `rustc`
1515
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
1616
42
1717
};
1818
let z = async { //~ NOTE `async` blocks are only allowed in Rust 2018 or later
1919
42
2020
//~^ ERROR expected identifier, found `42`
2121
//~| NOTE expected identifier
22-
//~| HELP set `edition = "2021"` in `Cargo.toml`
22+
//~| HELP pass `--edition 2021` to `rustc`
2323
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
2424
};
2525
y.await;

src/test/ui/editions/async-block-2015.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
44
LL | async fn foo() {
55
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
66
|
7-
= help: set `edition = "2021"` in `Cargo.toml`
7+
= help: pass `--edition 2021` to `rustc`
88
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
99

1010
error: expected identifier, found keyword `let`
@@ -15,7 +15,7 @@ LL | let y = async {
1515
LL | let x = 42;
1616
| ^^^ expected identifier, found keyword
1717
|
18-
= help: set `edition = "2021"` in `Cargo.toml`
18+
= help: pass `--edition 2021` to `rustc`
1919
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
2020

2121
error: expected identifier, found `42`
@@ -26,7 +26,7 @@ LL | let z = async {
2626
LL | 42
2727
| ^^ expected identifier
2828
|
29-
= help: set `edition = "2021"` in `Cargo.toml`
29+
= help: pass `--edition 2021` to `rustc`
3030
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
3131

3232
error[E0422]: cannot find struct, variant or union type `async` in this scope

src/test/ui/impl-trait/issues/issue-79099.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | let f: impl core::future::Future<Output = u8> = async { 1 };
66
| |
77
| `async` blocks are only allowed in Rust 2018 or later
88
|
9-
= help: set `edition = "2021"` in `Cargo.toml`
9+
= help: pass `--edition 2021` to `rustc`
1010
= note: for more on editions, read https://doc.rust-lang.org/edition-guide
1111

1212
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in variable binding

0 commit comments

Comments
 (0)