Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
6 changes: 3 additions & 3 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
HandledOptions::HelpOnly(matches) => (matches, true),
};

let sopts = config::build_session_options(&mut default_early_dcx, &matches);
let input = make_input(&default_early_dcx, &matches.free);
let has_input = input.is_some();
let sopts = config::build_session_options(&mut default_early_dcx, &matches, has_input);
// fully initialize ice path static once unstable options are available as context
let ice_file = ice_path_with_config(Some(&sopts.unstable_opts)).clone();

Expand All @@ -198,8 +200,6 @@ pub fn run_compiler(at_args: &[String], callbacks: &mut (dyn Callbacks + Send))
return;
}

let input = make_input(&default_early_dcx, &matches.free);
let has_input = input.is_some();
let (odir, ofile) = make_output(&matches);

drop(default_early_dcx);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
{
let mut early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
let matches = optgroups().parse(args).unwrap();
let sessopts = build_session_options(&mut early_dcx, &matches);
let sessopts = build_session_options(&mut early_dcx, &matches, true);
let target = rustc_session::config::build_target_config(
&early_dcx,
&sessopts.target_triple,
Expand Down Expand Up @@ -932,6 +932,6 @@ fn test_edition_parsing() {
let mut early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());

let matches = optgroups().parse(&["--edition=2018".to_string()]).unwrap();
let sessopts = build_session_options(&mut early_dcx, &matches);
let sessopts = build_session_options(&mut early_dcx, &matches, true);
assert!(sessopts.edition == Edition::Edition2018)
}
30 changes: 23 additions & 7 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2169,22 +2169,34 @@ pub fn parse_error_format(
error_format
}

pub fn parse_crate_edition(early_dcx: &EarlyDiagCtxt, matches: &getopts::Matches) -> Edition {
pub fn parse_crate_edition(
early_dcx: &EarlyDiagCtxt,
matches: &getopts::Matches,
has_input: bool,
) -> Edition {
let edition = match matches.opt_str("edition") {
Some(arg) => Edition::from_str(&arg).unwrap_or_else(|_| {
early_dcx.early_fatal(format!(
"argument for `--edition` must be one of: \
{EDITION_NAME_LIST}. (instead was `{arg}`)"
"argument for `--edition` must be one of: {EDITION_NAME_LIST} (instead was `{arg}`)"
))
}),
None => DEFAULT_EDITION,
None => {
if has_input {
early_dcx.early_note(format!(
"it is advisable to explicitly specify the `--edition` argument (the default \
implies `2015`); it must be one of: {EDITION_NAME_LIST}"
));
}
DEFAULT_EDITION
}
};

if !edition.is_stable() && !nightly_options::is_unstable_enabled(matches) {
let is_nightly = nightly_options::match_is_nightly_build(matches);
let msg = if !is_nightly {
format!(
"the crate requires edition {edition}, but the latest edition supported by this Rust version is {LATEST_STABLE_EDITION}"
"the crate requires edition {edition}, but the latest edition supported by this \
Rust version is {LATEST_STABLE_EDITION}"
)
} else {
format!("edition {edition} is unstable and only available with -Z unstable-options")
Expand Down Expand Up @@ -2498,10 +2510,14 @@ fn parse_logical_env(

// JUSTIFICATION: before wrapper fn is available
#[allow(rustc::bad_opt_access)]
pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::Matches) -> Options {
pub fn build_session_options(
early_dcx: &mut EarlyDiagCtxt,
matches: &getopts::Matches,
has_input: bool,
) -> Options {
let color = parse_color(early_dcx, matches);

let edition = parse_crate_edition(early_dcx, matches);
let edition = parse_crate_edition(early_dcx, matches, has_input);

let crate_name = matches.opt_str("crate-name");
let unstable_features = UnstableFeatures::from_environment(crate_name.as_deref());
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ impl Options {
}
}

let edition = config::parse_crate_edition(early_dcx, matches);
let edition = config::parse_crate_edition(early_dcx, matches, true);

let mut id_map = html::markdown::IdMap::new();
let Some(external_html) = ExternalHtml::load(
Expand Down
9 changes: 4 additions & 5 deletions src/tools/compiletest/src/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,10 @@ impl TestProps {
}
}

if let Some(edition) = self.edition.or(config.edition) {
// The edition is added at the start, since flags from //@compile-flags must be passed
// to rustc last.
self.compile_flags.insert(0, format!("--edition={edition}"));
}
let edition = self.edition.or(config.edition).unwrap_or(Edition::Year(2015));
// The edition is added at the start, since flags from //@compile-flags must be passed
// to rustc last.
self.compile_flags.insert(0, format!("--edition={edition}"));
}

fn update_pass_fail_mode(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
Expand Down
2 changes: 1 addition & 1 deletion tests/run-make/broken-pipe-no-ice/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn check_broken_pipe_handled_gracefully(bin: Binary, mut cmd: Command) {

fn main() {
let mut rustc = bare_rustc();
rustc.arg("--print=sysroot");
rustc.arg("--print=sysroot").edition("2015");
let rustc = rustc.into_raw_command();
check_broken_pipe_handled_gracefully(Binary::Rustc, rustc);

Expand Down
1 change: 1 addition & 0 deletions tests/run-make/compressed-debuginfo/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use run_make_support::{assert_contains, llvm_readobj, run_in_tmpdir, rustc};
fn check_compression(compression: &str, to_find: &str) {
run_in_tmpdir(|| {
let out = rustc()
.edition("2015")
.crate_name("foo")
.crate_type("lib")
.emit("obj")
Expand Down
4 changes: 4 additions & 0 deletions tests/run-make/const-trait-stable-toolchain/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use run_make_support::{diff, rustc};

fn main() {
let out = rustc()
.edition("2015")
.input("const-super-trait.rs")
.env("RUSTC_BOOTSTRAP", "-1")
.cfg("feature_enabled")
Expand All @@ -22,6 +23,7 @@ fn main() {
.actual_text("(rustc)", &out)
.run();
let out = rustc()
.edition("2015")
.input("const-super-trait.rs")
.cfg("feature_enabled")
.ui_testing()
Expand All @@ -34,6 +36,7 @@ fn main() {
.actual_text("(rustc)", &out)
.run();
let out = rustc()
.edition("2015")
.input("const-super-trait.rs")
.env("RUSTC_BOOTSTRAP", "-1")
.run_fail()
Expand All @@ -45,6 +48,7 @@ fn main() {
.actual_text("(rustc)", &out)
.run();
let out = rustc()
.edition("2015")
.input("const-super-trait.rs")
.ui_testing()
.run_fail()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
use run_make_support::{diff, rust_lib_name, rustc};

fn main() {
rustc().input("foo-prev.rs").run();
rustc().edition("2015").input("foo-prev.rs").run();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: we could consider default setting edition but allow overriding, but explicit is fine


let out = rustc()
.edition("2015")
.extra_filename("current")
.metadata("current")
.input("foo-current.rs")
Expand Down
15 changes: 13 additions & 2 deletions tests/run-make/crate-loading-multiple-candidates/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ use run_make_support::{bare_rustc, diff, rfs, rustc};
fn main() {
// Check that relative paths are preserved in the diagnostic
rfs::create_dir("mylibs");
rustc().input("crateresolve1-1.rs").out_dir("mylibs").extra_filename("-1").run();
rustc().input("crateresolve1-2.rs").out_dir("mylibs").extra_filename("-2").run();
rustc()
.edition("2015")
.input("crateresolve1-1.rs")
.out_dir("mylibs")
.extra_filename("-1")
.run();
rustc()
.edition("2015")
.input("crateresolve1-2.rs")
.out_dir("mylibs")
.extra_filename("-2")
.run();
check("./mylibs");

// Check that symlinks aren't followed when printing the diagnostic
Expand All @@ -21,6 +31,7 @@ fn main() {

fn check(library_path: &str) {
let out = rustc()
.edition("2015")
.input("multiple-candidates.rs")
.library_search_path(library_path)
.ui_testing()
Expand Down
11 changes: 8 additions & 3 deletions tests/run-make/crate-loading/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
use run_make_support::{diff, rust_lib_name, rustc};

fn main() {
rustc().input("dependency-1.rs").run();
rustc().input("dependency-2.rs").extra_filename("2").metadata("2").run();
rustc().input("dep-2-reexport.rs").extern_("dependency", rust_lib_name("dependency2")).run();
rustc().edition("2015").input("dependency-1.rs").run();
rustc().edition("2015").input("dependency-2.rs").extra_filename("2").metadata("2").run();
rustc()
.edition("2015")
.input("dep-2-reexport.rs")
.extern_("dependency", rust_lib_name("dependency2"))
.run();

let out = rustc()
.edition("2015")
.input("multiple-dep-versions.rs")
.extern_("dependency", rust_lib_name("dependency"))
.extern_("dep_2_reexport", rust_lib_name("foo"))
Expand Down
22 changes: 18 additions & 4 deletions tests/run-make/emit-to-stdout/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use run_make_support::{diff, run_in_tmpdir, rustc};

// Test emitting text outputs to stdout works correctly
fn run_diff(name: &str, file_args: &[&str]) {
rustc().emit(format!("{name}={name}")).input("test.rs").args(file_args).run();
let out = rustc().emit(format!("{name}=-")).input("test.rs").run().stdout_utf8();
rustc().edition("2015").emit(format!("{name}={name}")).input("test.rs").args(file_args).run();
let out =
rustc().edition("2015").emit(format!("{name}=-")).input("test.rs").run().stdout_utf8();
diff().expected_file(name).actual_text("stdout", &out).run();
}

Expand All @@ -29,7 +30,13 @@ fn run_terminal_err_diff(name: &str) {
let terminal = File::options().read(true).write(true).open(r"\\.\CONOUT$").unwrap();

let err = File::create(name).unwrap();
rustc().emit(format!("{name}=-")).input("test.rs").stdout(terminal).stderr(err).run_fail();
rustc()
.edition("2015")
.emit(format!("{name}=-"))
.input("test.rs")
.stdout(terminal)
.stderr(err)
.run_fail();
diff().expected_file(format!("emit-{name}.stderr")).actual_file(name).run();
}

Expand All @@ -47,6 +54,7 @@ fn main() {

// Test error for emitting multiple types to stdout
rustc()
.edition("2015")
.input("test.rs")
.emit("asm=-")
.emit("llvm-ir=-")
Expand All @@ -58,6 +66,7 @@ fn main() {

// Same as above, but using `-o`
rustc()
.edition("2015")
.input("test.rs")
.output("-")
.emit("asm,llvm-ir,dep-info,mir")
Expand All @@ -69,6 +78,11 @@ fn main() {
.run();

// Test that `-o -` redirected to a file works correctly (#26719)
rustc().input("test.rs").output("-").stdout(File::create("out-stdout").unwrap()).run();
rustc()
.edition("2015")
.input("test.rs")
.output("-")
.stdout(File::create("out-stdout").unwrap())
.run();
});
}
2 changes: 2 additions & 0 deletions tests/run-make/jobserver-error/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ use run_make_support::{diff, rustc};

fn main() {
let out = rustc()
.edition("2015")
.stdin_buf(("fn main() {}").as_bytes())
.env("MAKEFLAGS", "--jobserver-auth=1000,1000")
.run_fail()
.stderr_utf8();
diff().expected_file("cannot_open_fd.stderr").actual_text("actual", out).run();

let out = rustc()
.edition("2015")
.stdin_buf(("fn main() {}").as_bytes())
.input("-")
.env("MAKEFLAGS", "--jobserver-auth=3,3")
Expand Down
10 changes: 7 additions & 3 deletions tests/run-make/linker-warning/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use run_make_support::{Rustc, diff, regex, rustc};
fn run_rustc() -> Rustc {
let mut rustc = rustc();
rustc
.edition("2015")
.arg("main.rs")
// NOTE: `link-self-contained` can vary depending on bootstrap.toml.
// Make sure we use a consistent value.
Expand All @@ -23,9 +24,9 @@ fn run_rustc() -> Rustc {

fn main() {
// first, compile our linker and our dependencies
rustc().arg("fake-linker.rs").output("fake-linker").run();
rustc().arg("foo.rs").crate_type("rlib").run();
rustc().arg("bar.rs").crate_type("rlib").run();
rustc().edition("2015").arg("fake-linker.rs").output("fake-linker").run();
rustc().edition("2015").arg("foo.rs").crate_type("rlib").run();
rustc().edition("2015").arg("bar.rs").crate_type("rlib").run();

// Run rustc with our fake linker, and make sure it shows warnings
let warnings = run_rustc().link_arg("run_make_warn").run();
Expand Down Expand Up @@ -92,12 +93,14 @@ fn main() {

// Make sure we show linker warnings even across `-Z no-link`
rustc()
.edition("2015")
.arg("-Zno-link")
.input("-")
.stdin_buf("#![deny(linker_messages)] \n fn main() {}")
.run()
.assert_stderr_equals("");
rustc()
.edition("2015")
.arg("-Zlink-only")
.arg("rust_out.rlink")
.linker("./fake-linker")
Expand All @@ -111,6 +114,7 @@ fn main() {

// Same thing, but with json output.
rustc()
.edition("2015")
.error_format("json")
.arg("-Zlink-only")
.arg("rust_out.rlink")
Expand Down
1 change: 1 addition & 0 deletions tests/run-make/missing-unstable-trait-bound/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use run_make_support::{diff, rustc};

fn main() {
let out = rustc()
.edition("2015")
.env("RUSTC_BOOTSTRAP", "-1")
.input("missing-bound.rs")
.run_fail()
Expand Down
3 changes: 2 additions & 1 deletion tests/run-make/multiline-args-value/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use run_make_support::{cwd, diff, rustc};
fn test_and_compare(test_name: &str, flag: &str, val: &str) {
let mut cmd = rustc();

let output = cmd.input("").arg("--crate-type=lib").arg(flag).arg(val).run_fail();
let output =
cmd.edition("2015").input("").arg("--crate-type=lib").arg(flag).arg(val).run_fail();

assert_eq!(output.stdout_utf8(), "");
diff()
Expand Down
6 changes: 5 additions & 1 deletion tests/run-make/non-unicode-env/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ fn main() {
let non_unicode: &std::ffi::OsStr = std::os::unix::ffi::OsStrExt::from_bytes(&[0xFF]);
#[cfg(windows)]
let non_unicode: std::ffi::OsString = std::os::windows::ffi::OsStringExt::from_wide(&[0xD800]);
let output = rustc().input("non_unicode_env.rs").env("NON_UNICODE_VAR", non_unicode).run_fail();
let output = rustc()
.edition("2015")
.input("non_unicode_env.rs")
.env("NON_UNICODE_VAR", non_unicode)
.run_fail();
let expected = rfs::read_to_string("non_unicode_env.stderr");
output.assert_stderr_equals(expected);
}
Loading