Skip to content

Commit

Permalink
Rollup merge of rust-lang#94708 - notriddle:notriddle/cargo-toml-warn…
Browse files Browse the repository at this point in the history
…ing, r=lcnr

diagnostics: only talk about `Cargo.toml` if running under Cargo

Fixes rust-lang#94646
  • Loading branch information
matthiaskrgr authored Mar 8, 2022
2 parents e4a3627 + fbd4cfa commit 98d027c
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 33 deletions.
13 changes: 13 additions & 0 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::SuggestionStyle;
use crate::ToolMetadata;
use rustc_lint_defs::Applicability;
use rustc_serialize::json::Json;
use rustc_span::edition::LATEST_STABLE_EDITION;
use rustc_span::{MultiSpan, Span, DUMMY_SP};
use std::fmt;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -342,6 +343,18 @@ impl Diagnostic {
self
}

/// Help the user upgrade to the latest edition.
/// This is factored out to make sure it does the right thing with `Cargo.toml`.
pub fn help_use_latest_edition(&mut self) -> &mut Self {
if std::env::var_os("CARGO").is_some() {
self.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
} else {
self.help(&format!("pass `--edition {}` to `rustc`", LATEST_STABLE_EDITION));
}
self.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
self
}

/// Disallow attaching suggestions this diagnostic.
/// Any suggestions attached e.g. with the `span_suggestion_*` methods
/// (before and after the call to `disable_suggestions`) will be ignored.
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_errors/src/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
sp: impl Into<MultiSpan>,
msg: &str,
) -> &mut Self);
forward!(pub fn help_use_latest_edition(&mut self,) -> &mut Self);
forward!(pub fn set_is_lint(&mut self,) -> &mut Self);

forward!(pub fn disable_suggestions(&mut self,) -> &mut Self);
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use rustc_ast_pretty::pprust;
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, PResult};
use rustc_session::lint::builtin::BREAK_WITH_LABEL_AND_LOOP;
use rustc_session::lint::BuiltinLintDiagnostics;
use rustc_span::edition::LATEST_STABLE_EDITION;
use rustc_span::source_map::{self, Span, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, Pos};
Expand Down Expand Up @@ -2712,8 +2711,7 @@ impl<'a> Parser<'a> {
let mut async_block_err = |e: &mut Diagnostic, span: Span| {
recover_async = true;
e.span_label(span, "`async` blocks are only allowed in Rust 2018 or later");
e.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
e.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
e.help_use_latest_edition();
};

while self.token != token::CloseDelim(close_delim) {
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, Visibility
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::edition::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 @@ -2102,8 +2102,7 @@ impl<'a> Parser<'a> {
let diag = self.diagnostic();
struct_span_err!(diag, span, E0670, "`async fn` is not permitted in Rust 2015")
.span_label(span, "to use `async fn`, switch to Rust 2018 or later")
.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION))
.note("for more on editions, read https://doc.rust-lang.org/edition-guide")
.help_use_latest_edition()
.emit();
}
}
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_typeck/src/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use rustc_middle::ty::error::TypeError::{FieldMisMatch, Sorts};
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, AdtKind, Ty, TypeFoldable};
use rustc_session::parse::feature_err;
use rustc_span::edition::LATEST_STABLE_EDITION;
use rustc_span::hygiene::DesugaringKind;
use rustc_span::lev_distance::find_best_match_for_name;
use rustc_span::source_map::Span;
Expand Down Expand Up @@ -2010,8 +2009,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// We know by construction that `<expr>.await` is either on Rust 2015
// or results in `ExprKind::Await`. Suggest switching the edition to 2018.
err.note("to `.await` a `Future`, switch to Rust 2018 or later");
err.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
err.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
err.help_use_latest_edition();
}

err.emit();
Expand Down
18 changes: 9 additions & 9 deletions src/test/ui/async-await/edition-deny-async-fns-2015.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
LL | async fn foo() {}
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
|
= help: set `edition = "2021"` in `Cargo.toml`
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

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

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

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

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

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

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

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

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

error[E0706]: functions in traits cannot be declared `async`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// rustc-env:CARGO=/usr/bin/cargo

use std::pin::Pin;
use std::future::Future;

fn main() {}

fn await_on_struct_missing() {
struct S;
let x = S;
x.await;
//~^ ERROR no field `await` on type
//~| NOTE unknown field
//~| NOTE to `.await` a `Future`, switch to Rust 2018
//~| HELP set `edition = "2021"` in `Cargo.toml`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
}

fn await_on_struct_similar() {
struct S {
awai: u8,
}
let x = S { awai: 42 };
x.await;
//~^ ERROR no field `await` on type
//~| HELP a field with a similar name exists
//~| NOTE to `.await` a `Future`, switch to Rust 2018
//~| HELP set `edition = "2021"` in `Cargo.toml`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
}

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

fn await_on_apit(x: impl Future<Output = ()>) {
x.await;
//~^ ERROR no field `await` on type
//~| NOTE to `.await` a `Future`, switch to Rust 2018
//~| HELP set `edition = "2021"` in `Cargo.toml`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
error[E0609]: no field `await` on type `await_on_struct_missing::S`
--> $DIR/suggest-switching-edition-on-await-cargo.rs:11:7
|
LL | x.await;
| ^^^^^ unknown field
|
= note: to `.await` a `Future`, switch to Rust 2018 or later
= help: set `edition = "2021"` in `Cargo.toml`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0609]: no field `await` on type `await_on_struct_similar::S`
--> $DIR/suggest-switching-edition-on-await-cargo.rs:24:7
|
LL | x.await;
| ^^^^^ help: a field with a similar name exists: `awai`
|
= note: to `.await` a `Future`, switch to Rust 2018 or later
= help: set `edition = "2021"` in `Cargo.toml`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0609]: no field `await` on type `Pin<&mut dyn Future<Output = ()>>`
--> $DIR/suggest-switching-edition-on-await-cargo.rs:33:7
|
LL | x.await;
| ^^^^^ unknown field
|
= note: to `.await` a `Future`, switch to Rust 2018 or later
= help: set `edition = "2021"` in `Cargo.toml`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error[E0609]: no field `await` on type `impl Future<Output = ()>`
--> $DIR/suggest-switching-edition-on-await-cargo.rs:42:7
|
LL | x.await;
| ^^^^^
|
= note: to `.await` a `Future`, switch to Rust 2018 or later
= help: set `edition = "2021"` in `Cargo.toml`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0609`.
8 changes: 4 additions & 4 deletions src/test/ui/async-await/suggest-switching-edition-on-await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn await_on_struct_missing() {
//~^ ERROR no field `await` on type
//~| NOTE unknown field
//~| NOTE to `.await` a `Future`, switch to Rust 2018
//~| HELP set `edition = "2021"` in `Cargo.toml`
//~| HELP pass `--edition 2021` to `rustc`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
}

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

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

fn await_on_apit(x: impl Future<Output = ()>) {
x.await;
//~^ ERROR no field `await` on type
//~| NOTE to `.await` a `Future`, switch to Rust 2018
//~| HELP set `edition = "2021"` in `Cargo.toml`
//~| HELP pass `--edition 2021` to `rustc`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | x.await;
| ^^^^^ unknown field
|
= note: to `.await` a `Future`, switch to Rust 2018 or later
= help: set `edition = "2021"` in `Cargo.toml`
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

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

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

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

error: aborting due to 4 previous errors
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/editions/async-block-2015.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
async fn foo() {
//~^ ERROR `async fn` is not permitted in Rust 2015
//~| NOTE to use `async fn`, switch to Rust 2018 or later
//~| HELP set `edition = "2021"` in `Cargo.toml`
//~| HELP pass `--edition 2021` to `rustc`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide

let x = async {};
Expand All @@ -11,15 +11,15 @@ async fn foo() {
let x = 42;
//~^ ERROR expected identifier, found keyword `let`
//~| NOTE expected identifier, found keyword
//~| HELP set `edition = "2021"` in `Cargo.toml`
//~| HELP pass `--edition 2021` to `rustc`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
42
};
let z = async { //~ NOTE `async` blocks are only allowed in Rust 2018 or later
42
//~^ ERROR expected identifier, found `42`
//~| NOTE expected identifier
//~| HELP set `edition = "2021"` in `Cargo.toml`
//~| HELP pass `--edition 2021` to `rustc`
//~| NOTE for more on editions, read https://doc.rust-lang.org/edition-guide
};
y.await;
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/editions/async-block-2015.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0670]: `async fn` is not permitted in Rust 2015
LL | async fn foo() {
| ^^^^^ to use `async fn`, switch to Rust 2018 or later
|
= help: set `edition = "2021"` in `Cargo.toml`
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

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

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

error[E0422]: cannot find struct, variant or union type `async` in this scope
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/impl-trait/issues/issue-79099.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LL | let f: impl core::future::Future<Output = u8> = async { 1 };
| |
| `async` blocks are only allowed in Rust 2018 or later
|
= help: set `edition = "2021"` in `Cargo.toml`
= help: pass `--edition 2021` to `rustc`
= note: for more on editions, read https://doc.rust-lang.org/edition-guide

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

0 comments on commit 98d027c

Please sign in to comment.