Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Comment thread
jieyouxu marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,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 @@ -200,8 +202,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
10 changes: 5 additions & 5 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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 @@ -941,7 +941,7 @@ 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, false);
assert!(sessopts.edition == Edition::Edition2018)
}

Expand All @@ -952,7 +952,7 @@ fn test_assumptions_on_binders_enables_next_solver_globally() {

// `-Zassumptions-on-binders` alone enables the next solver globally.
let matches = optgroups().parse(&["-Zassumptions-on-binders".to_string()]).unwrap();
let opts = build_session_options(&mut early_dcx, &matches);
let opts = build_session_options(&mut early_dcx, &matches, false);
assert!(opts.unstable_opts.assumptions_on_binders);
assert_eq!(opts.unstable_opts.next_solver, globally);

Expand All @@ -963,7 +963,7 @@ fn test_assumptions_on_binders_enables_next_solver_globally() {
["-Znext-solver=coherence".to_string(), "-Zassumptions-on-binders".to_string()],
] {
let matches = optgroups().parse(&args).unwrap();
let opts = build_session_options(&mut early_dcx, &matches);
let opts = build_session_options(&mut early_dcx, &matches, false);
assert!(opts.unstable_opts.assumptions_on_binders);
assert_eq!(opts.unstable_opts.next_solver, globally);
}
Expand All @@ -976,7 +976,7 @@ fn test_assumptions_on_binders_enables_next_solver_globally() {
["-Znext-solver=no".to_string(), "-Zassumptions-on-binders".to_string()],
] {
let matches = optgroups().parse(&args).unwrap();
let opts = build_session_options(&mut early_dcx, &matches);
let opts = build_session_options(&mut early_dcx, &matches, false);
assert!(opts.unstable_opts.assumptions_on_binders);
assert_eq!(opts.unstable_opts.next_solver, globally);
}
Expand Down
37 changes: 28 additions & 9 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use rustc_errors::{ColorConfig, DiagCtxtFlags};
use rustc_feature::UnstableFeatures;
use rustc_hashes::Hash64;
use rustc_macros::{BlobDecodable, Decodable, Encodable, StableHash};
use rustc_span::edition::{DEFAULT_EDITION, EDITION_NAME_LIST, Edition, LATEST_STABLE_EDITION};
use rustc_span::edition::{
DEFAULT_EDITION, EDITION_NAME_LIST, EDITION_NAME_LIST_STABLE, Edition, LATEST_STABLE_EDITION,
};
use rustc_span::source_map::FilePathMapping;
use rustc_span::{
FileName, RealFileName, RemapPathScopeComponents, SourceFileHashAlgorithm, Symbol, sym,
Expand Down Expand Up @@ -2365,22 +2367,35 @@ 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 is_nightly = nightly_options::match_is_nightly_build(matches);
let edition_list = if is_nightly { EDITION_NAME_LIST } else { EDITION_NAME_LIST_STABLE };
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_list} (instead was `{arg}`)",
))
}),
None => DEFAULT_EDITION,
None => {
if has_input {
eprintln!(
"`--edition` is unspecified, defaulting to `{DEFAULT_EDITION}` while the \
latest is `{LATEST_STABLE_EDITION}`; it must be one of: {edition_list}\n",
);
}
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 @@ -2677,10 +2692,14 @@ fn parse_remap_path_prefix(

// 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
21 changes: 14 additions & 7 deletions compiler/rustc_span/src/edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,18 @@ pub const ALL_EDITIONS: &[Edition] = &[
Edition::EditionFuture,
];

/// All the valid editions that `--edition` will accept on nightly.
pub const EDITION_NAME_LIST: &str = "<2015|2018|2021|2024|future>";
/// All the valid editions on stable, which doesn't include `future`.
pub const EDITION_NAME_LIST_STABLE: &str = "<2015|2018|2021|2024>";

pub const DEFAULT_EDITION: Edition = Edition::Edition2015;

pub const LATEST_STABLE_EDITION: Edition = Edition::Edition2024;

impl fmt::Display for Edition {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = match *self {
Edition::Edition2015 => "2015",
Edition::Edition2018 => "2018",
Edition::Edition2021 => "2021",
Edition::Edition2024 => "2024",
Edition::EditionFuture => "future",
};
let s = self.as_str();
write!(f, "{s}")
}
}
Expand All @@ -75,6 +72,16 @@ impl Edition {
}
}

pub fn as_str(&self) -> &'static str {
match *self {
Edition::Edition2015 => "2015",
Edition::Edition2018 => "2018",
Edition::Edition2021 => "2021",
Edition::Edition2024 => "2024",
Edition::EditionFuture => "future",
}
}

pub fn is_stable(self) -> bool {
match self {
Edition::Edition2015 => true,
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,9 @@ impl Options {
}
}

let edition = config::parse_crate_edition(early_dcx, matches);
// We don't want rustdoc invocations to complain about the lack of `--edition`.
let has_input = false;
let edition = config::parse_crate_edition(early_dcx, matches, has_input);

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 @@ -405,11 +405,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
5 changes: 4 additions & 1 deletion src/tools/compiletest/src/rustdoc_gui_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ impl RustdocGuiTestProps {

let props = TestProps::from_file(test_file_path, None, &config);

let TestProps { compile_flags, run_flags, .. } = props;
let TestProps { mut compile_flags, run_flags, .. } = props;
// We don't want to pass `--edition=2015` in, which is being set by default by
// `TestProps::from_file`.
compile_flags.remove(0);
Comment on lines +36 to +39

@jieyouxu jieyouxu Sep 16, 2026

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.

Instead of adding then removing, can we stop presetting --edition=2015 in TestProps::from_file for rustdoc-gui test mode? Actually I'll look at this later, this has some funky behavior in general.

Self { compile_flags, run_flags }
}
}
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 @@ -12,6 +12,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
2 changes: 2 additions & 0 deletions tests/run-make/const-destruct-stable-toolchain/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ use run_make_support::{diff, rustc};
fn main() {
let out = rustc()
.input("const-drop.rs")
.edition("2015")
.env("RUSTC_BOOTSTRAP", "-1")
.run_fail()
.assert_stderr_not_contains("consider restricting type parameter `T`")
.stderr_utf8();
diff().expected_file("const-drop-stable.stderr").actual_text("(rustc)", &out).run();
let out = rustc()
.input("const-drop.rs")
.edition("2015")
.ui_testing()
.run_fail()
.assert_stderr_contains(
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 @@ -9,6 +9,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 @@ -24,6 +25,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 @@ -36,6 +38,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 @@ -47,6 +50,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();
});
}
Loading
Loading