From 45ede927fb01ea7928267cc4caa2db75fd3be318 Mon Sep 17 00:00:00 2001 From: Luca Barbieri Date: Fri, 10 Apr 2020 22:42:19 +0200 Subject: [PATCH 01/10] Depend on libc from crates.io --- Cargo.lock | 4 ++++ src/librustc_data_structures/Cargo.toml | 1 + src/librustc_data_structures/lib.rs | 2 -- src/librustc_driver/Cargo.toml | 1 + src/librustc_driver/lib.rs | 2 -- src/librustc_interface/Cargo.toml | 1 + src/librustc_interface/lib.rs | 3 --- src/librustc_metadata/Cargo.toml | 1 + src/librustc_metadata/lib.rs | 1 - 9 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 908bfa355571b..e8a7413ff353f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3694,6 +3694,7 @@ dependencies = [ "indexmap", "jobserver", "lazy_static 1.4.0", + "libc", "log", "measureme", "parking_lot 0.10.0", @@ -3713,6 +3714,7 @@ version = "0.0.0" dependencies = [ "env_logger 0.7.1", "lazy_static 1.4.0", + "libc", "log", "rustc_ast", "rustc_ast_pretty", @@ -3867,6 +3869,7 @@ dependencies = [ name = "rustc_interface" version = "0.0.0" dependencies = [ + "libc", "log", "once_cell", "rustc-rayon", @@ -3960,6 +3963,7 @@ name = "rustc_metadata" version = "0.0.0" dependencies = [ "flate2", + "libc", "log", "memmap", "rustc_ast", diff --git a/src/librustc_data_structures/Cargo.toml b/src/librustc_data_structures/Cargo.toml index c5707bd24f7e7..6d7022acc7863 100644 --- a/src/librustc_data_structures/Cargo.toml +++ b/src/librustc_data_structures/Cargo.toml @@ -27,6 +27,7 @@ smallvec = { version = "1.0", features = ["union", "may_dangle"] } rustc_index = { path = "../librustc_index", package = "rustc_index" } bitflags = "1.2.1" measureme = "0.7.1" +libc = "0.2" [dependencies.parking_lot] version = "0.10" diff --git a/src/librustc_data_structures/lib.rs b/src/librustc_data_structures/lib.rs index ba82e58d7a92e..d0180911567c7 100644 --- a/src/librustc_data_structures/lib.rs +++ b/src/librustc_data_structures/lib.rs @@ -26,8 +26,6 @@ #[macro_use] extern crate log; -#[cfg(unix)] -extern crate libc; #[macro_use] extern crate cfg_if; diff --git a/src/librustc_driver/Cargo.toml b/src/librustc_driver/Cargo.toml index 652f16c2e0828..cfd103aed3240 100644 --- a/src/librustc_driver/Cargo.toml +++ b/src/librustc_driver/Cargo.toml @@ -11,6 +11,7 @@ crate-type = ["dylib"] [dependencies] lazy_static = "1.0" +libc = "0.2" log = "0.4" env_logger = { version = "0.7", default-features = false } rustc_middle = { path = "../librustc_middle" } diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index acf8f1adbc2e8..ad2d7b12d4aad 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -9,8 +9,6 @@ #![recursion_limit = "256"] pub extern crate getopts; -#[cfg(unix)] -extern crate libc; #[macro_use] extern crate log; #[macro_use] diff --git a/src/librustc_interface/Cargo.toml b/src/librustc_interface/Cargo.toml index c9d81e51641a8..8ea866d7cab5c 100644 --- a/src/librustc_interface/Cargo.toml +++ b/src/librustc_interface/Cargo.toml @@ -10,6 +10,7 @@ path = "lib.rs" doctest = false [dependencies] +libc = "0.2" log = "0.4" rayon = { version = "0.3.0", package = "rustc-rayon" } smallvec = { version = "1.0", features = ["union", "may_dangle"] } diff --git a/src/librustc_interface/lib.rs b/src/librustc_interface/lib.rs index ba1e2216ca805..0650d09003486 100644 --- a/src/librustc_interface/lib.rs +++ b/src/librustc_interface/lib.rs @@ -6,9 +6,6 @@ #![feature(generators)] #![recursion_limit = "256"] -#[cfg(unix)] -extern crate libc; - mod callbacks; pub mod interface; mod passes; diff --git a/src/librustc_metadata/Cargo.toml b/src/librustc_metadata/Cargo.toml index a88ccd52e9111..b03e884cdaf7a 100644 --- a/src/librustc_metadata/Cargo.toml +++ b/src/librustc_metadata/Cargo.toml @@ -11,6 +11,7 @@ doctest = false [dependencies] flate2 = "1.0" +libc = "0.2" log = "0.4" memmap = "0.7" smallvec = { version = "1.0", features = ["union", "may_dangle"] } diff --git a/src/librustc_metadata/lib.rs b/src/librustc_metadata/lib.rs index 22a2bf280415c..4659be8c195ad 100644 --- a/src/librustc_metadata/lib.rs +++ b/src/librustc_metadata/lib.rs @@ -10,7 +10,6 @@ #![feature(stmt_expr_attributes)] #![recursion_limit = "256"] -extern crate libc; extern crate proc_macro; #[macro_use] From e2e41c9527e64120b5dedfd1f0fdb29d03112d6d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 11 Apr 2020 18:58:03 +0200 Subject: [PATCH 02/10] Clean up E0515 explanation --- src/librustc_error_codes/error_codes/E0515.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0515.md b/src/librustc_error_codes/error_codes/E0515.md index 9580b6f92acd1..0f4fbf672239c 100644 --- a/src/librustc_error_codes/error_codes/E0515.md +++ b/src/librustc_error_codes/error_codes/E0515.md @@ -1,7 +1,4 @@ -Cannot return value that references local variable - -Local variables, function parameters and temporaries are all dropped before the -end of the function body. So a reference to them cannot be returned. +A reference to a local variable was returned. Erroneous code example: @@ -20,6 +17,9 @@ fn get_dangling_iterator<'a>() -> Iter<'a, i32> { } ``` +Local variables, function parameters and temporaries are all dropped before the +end of the function body. So a reference to them cannot be returned. + Consider returning an owned value instead: ``` From 6b2830427b6bb2bf436439cb06ef60d4392d768f Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sun, 12 Apr 2020 05:02:35 +0900 Subject: [PATCH 03/10] Update links of `rustc guide` --- src/librustc_infer/traits/mod.rs | 4 ++-- src/librustc_trait_selection/lib.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc_infer/traits/mod.rs b/src/librustc_infer/traits/mod.rs index 758a0b39d43ee..2210c663d1469 100644 --- a/src/librustc_infer/traits/mod.rs +++ b/src/librustc_infer/traits/mod.rs @@ -1,6 +1,6 @@ -//! Trait Resolution. See the [rustc guide] for more information on how this works. +//! Trait Resolution. See the [rustc-dev-guide] for more information on how this works. //! -//! [rustc guide]: https://rust-lang.github.io/rustc-guide/traits/resolution.html +//! [rustc-dev-guide]: https://rustc-dev-guide.rust-lang.org/traits/resolution.html mod engine; pub mod error_reporting; diff --git a/src/librustc_trait_selection/lib.rs b/src/librustc_trait_selection/lib.rs index 21315cc89ca5c..ea886cd1f9e9b 100644 --- a/src/librustc_trait_selection/lib.rs +++ b/src/librustc_trait_selection/lib.rs @@ -2,9 +2,9 @@ //! //! - **Traits.** Trait resolution is implemented in the `traits` module. //! -//! For more information about how rustc works, see the [rustc guide]. +//! For more information about how rustc works, see the [rustc-dev-guide]. //! -//! [rustc guide]: https://rust-lang.github.io/rustc-guide/ +//! [rustc-dev-guide]: https://rustc-dev-guide.rust-lang.org/ //! //! # Note //! From ac2b84f9628c46c9c7e6c4870054f372962079ed Mon Sep 17 00:00:00 2001 From: Luca Barbieri Date: Fri, 10 Apr 2020 22:42:19 +0200 Subject: [PATCH 04/10] Depend on getopts from crates.io rustc_session exports it for other crates to avoid mismatching crate versions. --- Cargo.lock | 1 + src/librustc_driver/lib.rs | 2 +- src/librustc_interface/tests.rs | 3 +-- src/librustc_session/Cargo.toml | 1 + src/librustc_session/lib.rs | 8 ++------ src/librustdoc/config.rs | 1 + src/librustdoc/lib.rs | 2 +- 7 files changed, 8 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e8a7413ff353f..73d276d177648 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4201,6 +4201,7 @@ dependencies = [ name = "rustc_session" version = "0.0.0" dependencies = [ + "getopts", "log", "num_cpus", "rustc_ast", diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index ad2d7b12d4aad..0e3199975f9be 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -8,7 +8,6 @@ #![feature(nll)] #![recursion_limit = "256"] -pub extern crate getopts; #[macro_use] extern crate log; #[macro_use] @@ -35,6 +34,7 @@ use rustc_save_analysis::DumpHandler; use rustc_serialize::json::{self, ToJson}; use rustc_session::config::nightly_options; use rustc_session::config::{ErrorOutputType, Input, OutputType, PrintRequest}; +use rustc_session::getopts; use rustc_session::lint::{Lint, LintId}; use rustc_session::{config, DiagnosticOutput, Session}; use rustc_session::{early_error, early_warn}; diff --git a/src/librustc_interface/tests.rs b/src/librustc_interface/tests.rs index b452ccfe33a52..c75f3b279a258 100644 --- a/src/librustc_interface/tests.rs +++ b/src/librustc_interface/tests.rs @@ -1,5 +1,3 @@ -extern crate getopts; - use crate::interface::parse_cfgspecs; use rustc_data_structures::fx::FxHashSet; @@ -9,6 +7,7 @@ use rustc_session::config::{build_configuration, build_session_options, to_crate use rustc_session::config::{rustc_optgroups, ErrorOutputType, ExternLocation, Options, Passes}; use rustc_session::config::{ExternEntry, LinkerPluginLto, LtoCli, SwitchWithOptPath}; use rustc_session::config::{Externs, OutputType, OutputTypes, SymbolManglingVersion}; +use rustc_session::getopts; use rustc_session::lint::Level; use rustc_session::search_paths::SearchPath; use rustc_session::{build_session, Session}; diff --git a/src/librustc_session/Cargo.toml b/src/librustc_session/Cargo.toml index 3895d0f8061c0..814073bb4f7be 100644 --- a/src/librustc_session/Cargo.toml +++ b/src/librustc_session/Cargo.toml @@ -9,6 +9,7 @@ name = "rustc_session" path = "lib.rs" [dependencies] +getopts = "0.2" log = "0.4" rustc_errors = { path = "../librustc_errors" } rustc_feature = { path = "../librustc_feature" } diff --git a/src/librustc_session/lib.rs b/src/librustc_session/lib.rs index cc4d525d62887..2ff92c46d1251 100644 --- a/src/librustc_session/lib.rs +++ b/src/librustc_session/lib.rs @@ -1,10 +1,4 @@ #![feature(crate_visibility_modifier)] -#![feature(test)] - -// Use the test crate here so we depend on getopts through it. This allow tools to link to both -// librustc_session and libtest. -extern crate getopts; -extern crate test as _; pub mod cgu_reuse_tracker; pub mod utils; @@ -23,3 +17,5 @@ mod session; pub use session::*; pub mod output; + +pub use getopts; diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 179c5bfacf32e..a5a1e20396caf 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -10,6 +10,7 @@ use rustc_session::config::{ nightly_options, }; use rustc_session::config::{CodegenOptions, DebuggingOptions, ErrorOutputType, Externs}; +use rustc_session::getopts; use rustc_session::lint::Level; use rustc_session::search_paths::SearchPath; use rustc_span::edition::{Edition, DEFAULT_EDITION}; diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 99860a103d78f..b0d5a8e58e120 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -15,7 +15,6 @@ #![recursion_limit = "256"] extern crate env_logger; -extern crate getopts; extern crate rustc_ast; extern crate rustc_ast_pretty; extern crate rustc_attr; @@ -51,6 +50,7 @@ use std::panic; use std::process; use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup}; +use rustc_session::getopts; use rustc_session::{early_error, early_warn}; #[macro_use] From 53d58dbf5f8a65189e5f97ef46da6593484a9e79 Mon Sep 17 00:00:00 2001 From: Luca Barbieri Date: Fri, 10 Apr 2020 22:42:19 +0200 Subject: [PATCH 05/10] Require compiler-rt root at ../src/llvm-project/compiler-rt --- src/bootstrap/compile.rs | 2 ++ src/libprofiler_builtins/build.rs | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index 32ce170a5a1fe..f44096af6dd53 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -186,6 +186,8 @@ pub fn std_cargo(builder: &Builder<'_>, target: Interned, cargo: &mut Ca // `compiler-rt` is located. let compiler_builtins_root = builder.src.join("src/llvm-project/compiler-rt"); let compiler_builtins_c_feature = if compiler_builtins_root.exists() { + // Note that `libprofiler_builtins/build.rs` also computes this so if + // you're changing something here please also change that. cargo.env("RUST_COMPILER_RT_ROOT", &compiler_builtins_root); " compiler-builtins-c".to_string() } else { diff --git a/src/libprofiler_builtins/build.rs b/src/libprofiler_builtins/build.rs index c990b28933504..e23e2f2c1306f 100644 --- a/src/libprofiler_builtins/build.rs +++ b/src/libprofiler_builtins/build.rs @@ -63,8 +63,9 @@ fn main() { cfg.define("COMPILER_RT_HAS_ATOMICS", Some("1")); } - let root = env::var_os("RUST_COMPILER_RT_ROOT").unwrap(); - let root = Path::new(&root); + // Note that this should exist if we're going to run (otherwise we just + // don't build profiler builtins at all). + let root = Path::new("../llvm-project/compiler-rt"); let src_root = root.join("lib").join("profile"); for src in profile_sources { From 3dd500de373757fd2b118db0c999e48dd01ff894 Mon Sep 17 00:00:00 2001 From: Luca Barbieri Date: Fri, 10 Apr 2020 22:42:19 +0200 Subject: [PATCH 06/10] Don't emit rerun-if-changed on llvm-config if using system LLVM The code was broken because it printed "llvm-config" instead of the absolute path to the llvm-config executable, causing Cargo to always rebuild librustc_llvm if using system LLVM. Also, it's not the build system's job to rebuild when a system library changes, so we simply don't emit "rerun-if-changed" if a path to LLVM was not explicitly provided. --- src/librustc_llvm/build.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/librustc_llvm/build.rs b/src/librustc_llvm/build.rs index fcaeaf2e4b07d..f14fc9fc2eba8 100644 --- a/src/librustc_llvm/build.rs +++ b/src/librustc_llvm/build.rs @@ -24,18 +24,28 @@ fn main() { build_helper::restore_library_path(); let target = env::var("TARGET").expect("TARGET was not set"); - let llvm_config = env::var_os("LLVM_CONFIG").map(PathBuf::from).unwrap_or_else(|| { - if let Some(dir) = env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) { - let to_test = - dir.parent().unwrap().parent().unwrap().join(&target).join("llvm/bin/llvm-config"); - if Command::new(&to_test).output().is_ok() { - return to_test; + let llvm_config = + env::var_os("LLVM_CONFIG").map(|x| Some(PathBuf::from(x))).unwrap_or_else(|| { + if let Some(dir) = env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) { + let to_test = dir + .parent() + .unwrap() + .parent() + .unwrap() + .join(&target) + .join("llvm/bin/llvm-config"); + if Command::new(&to_test).output().is_ok() { + return Some(to_test); + } } - } - PathBuf::from("llvm-config") - }); + None + }); + + if let Some(llvm_config) = &llvm_config { + println!("cargo:rerun-if-changed={}", llvm_config.display()); + } + let llvm_config = llvm_config.unwrap_or_else(|| PathBuf::from("llvm-config")); - println!("cargo:rerun-if-changed={}", llvm_config.display()); println!("cargo:rerun-if-env-changed=LLVM_CONFIG"); // Test whether we're cross-compiling LLVM. This is a pretty rare case From 1864caaaa4d283143eb5e267e5cb46fa0500434a Mon Sep 17 00:00:00 2001 From: Luca Barbieri Date: Fri, 10 Apr 2020 22:42:20 +0200 Subject: [PATCH 07/10] Make panic-unwind a default feature for libstd x.py sets it unconditionally, so want it for plain "cargo build". We need to load one of the panic runtimes that is in src (vs. pre-built in the compiler's sysroot) to ensure that we don't load libpanic_unwind from the sysroot. That would lead to a load of libcore, also from the sysroot, and create lots of errors about duplicate lang items. --- src/libstd/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/Cargo.toml b/src/libstd/Cargo.toml index 3a83f3f569a26..ceb39c01c6723 100644 --- a/src/libstd/Cargo.toml +++ b/src/libstd/Cargo.toml @@ -47,7 +47,7 @@ hermit-abi = { version = "0.1.10", features = ['rustc-dep-of-std'] } wasi = { version = "0.9.0", features = ['rustc-dep-of-std'], default-features = false } [features] -default = ["std_detect_file_io", "std_detect_dlsym_getauxval"] +default = ["std_detect_file_io", "std_detect_dlsym_getauxval", "panic-unwind"] backtrace = [ "backtrace_rs/dbghelp", # backtrace/symbolize on MSVC From f41aa161c223b6490486d81339f4da91e80b8c0a Mon Sep 17 00:00:00 2001 From: Arlo Siemsen Date: Sat, 11 Apr 2020 20:21:51 -0700 Subject: [PATCH 08/10] Normalize source when loading external foreign source into SourceMap The compiler normalizes source when reading files initially (removes BOMs, etc), but not when loading external sources. Fixes #70874 by normalizing when loading external sources too. Adds a test to verify normalization. --- src/librustc_span/lib.rs | 4 +- src/librustc_span/source_map/tests.rs | 56 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index 0d9f3f214fb21..85a870ae34c11 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -1192,8 +1192,10 @@ impl SourceFile { kind: src_kind @ ExternalSourceKind::AbsentOk, .. } = &mut *external_src { - if let Some(src) = src { + if let Some(mut src) = src { + // The src_hash needs to be computed on the pre-normalized src. if self.src_hash.matches(&src) { + normalize_src(&mut src, BytePos::from_usize(0)); *src_kind = ExternalSourceKind::Present(Lrc::new(src)); return true; } diff --git a/src/librustc_span/source_map/tests.rs b/src/librustc_span/source_map/tests.rs index 79df1884f0db0..b8459eee4ecf0 100644 --- a/src/librustc_span/source_map/tests.rs +++ b/src/librustc_span/source_map/tests.rs @@ -168,6 +168,62 @@ fn span_merging_fail() { assert!(sm.merge_spans(span1, span2).is_none()); } +/// Tests loading an external source file that requires normalization. +#[test] +fn t10() { + let sm = SourceMap::new(FilePathMapping::empty()); + let unnormalized = "first line.\r\nsecond line"; + let normalized = "first line.\nsecond line"; + + let src_file = sm.new_source_file(PathBuf::from("blork.rs").into(), unnormalized.to_string()); + + assert_eq!(src_file.src.as_ref().unwrap().as_ref(), normalized); + assert!( + src_file.src_hash.matches(unnormalized), + "src_hash should use the source before normalization" + ); + + let SourceFile { + name, + name_was_remapped, + src_hash, + start_pos, + end_pos, + lines, + multibyte_chars, + non_narrow_chars, + normalized_pos, + name_hash, + .. + } = (*src_file).clone(); + + let imported_src_file = sm.new_imported_source_file( + name, + name_was_remapped, + src_hash, + name_hash, + (end_pos - start_pos).to_usize(), + CrateNum::new(0), + lines, + multibyte_chars, + non_narrow_chars, + normalized_pos, + start_pos, + end_pos, + ); + + assert!( + imported_src_file.external_src.borrow().get_source().is_none(), + "imported source file should not have source yet" + ); + imported_src_file.add_external_src(|| Some(unnormalized.to_string())); + assert_eq!( + imported_src_file.external_src.borrow().get_source().unwrap().as_ref(), + normalized, + "imported source file should be normalized" + ); +} + /// Returns the span corresponding to the `n`th occurrence of `substring` in `source_text`. trait SourceMapExtension { fn span_substr( From ec4f7e2edfa8ee6cf9933ceb9194db44158e9222 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Sun, 12 Apr 2020 09:45:08 +0200 Subject: [PATCH 09/10] Add some basic docs to `sym` and `kw` modules I was looking into improving some Clippy documentation but was missing a place that explains the `kw` and `sym` modules from rustc. --- src/librustc_span/symbol.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/librustc_span/symbol.rs b/src/librustc_span/symbol.rs index 6845cb3b9a352..4c963ac84dc85 100644 --- a/src/librustc_span/symbol.rs +++ b/src/librustc_span/symbol.rs @@ -1153,12 +1153,20 @@ impl Interner { } // This module has a very short name because it's used a lot. +/// This module contains all the defined keyword `Symbol`s. +/// +/// Given that `kw` is imported, use them like `kw::keyword_name`. +/// For example `kw::Loop` or `kw::Break`. pub mod kw { use super::Symbol; keywords!(); } // This module has a very short name because it's used a lot. +/// This module contains all the defined non-keyword `Symbol`s. +/// +/// Given that `sym` is imported, use them like `sym::symbol_name`. +/// For example `sym::rustfmt` or `sym::u8`. #[allow(rustc::default_hash_types)] pub mod sym { use super::Symbol; From d0b23d5b8338be7c76ec2013fc19801f578d4140 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 12 Apr 2020 13:58:05 +0200 Subject: [PATCH 10/10] Clean up E0516 explanation --- src/librustc_error_codes/error_codes/E0516.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustc_error_codes/error_codes/E0516.md b/src/librustc_error_codes/error_codes/E0516.md index 815d760901300..935c31bbab98a 100644 --- a/src/librustc_error_codes/error_codes/E0516.md +++ b/src/librustc_error_codes/error_codes/E0516.md @@ -1,4 +1,5 @@ The `typeof` keyword is currently reserved but unimplemented. + Erroneous code example: ```compile_fail,E0516