From b58347a9c607e493ff947ff470492c38f7819c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9D=B0=E5=8F=8B=20Jieyou=20Xu=20=28Joe=29?= Date: Thu, 9 Feb 2023 00:49:43 +0800 Subject: [PATCH 1/8] Don't expose type parameters and implementation details from macro expansion --- .../infer/error_reporting/need_type_info.rs | 23 +++++++++++-------- ...-107745-avoid-expr-from-macro-expansion.rs | 19 +++++++++++++++ ...745-avoid-expr-from-macro-expansion.stderr | 11 +++++++++ tests/ui/issues/issue-16966.stderr | 6 ++--- ...ing-closing-angle-bracket-eq-constraint.rs | 2 +- ...closing-angle-bracket-eq-constraint.stderr | 12 +++++----- .../cannot_infer_local_or_vec.stderr | 6 ++--- ...cannot_infer_local_or_vec_in_tuples.stderr | 6 ++--- 8 files changed, 59 insertions(+), 26 deletions(-) create mode 100644 tests/ui/inference/need_type_info/issue-107745-avoid-expr-from-macro-expansion.rs create mode 100644 tests/ui/inference/need_type_info/issue-107745-avoid-expr-from-macro-expansion.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs index b8c843a8a5a22..c092efbb557cf 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs @@ -158,8 +158,12 @@ fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinte if infcx.probe_ty_var(ty_vid).is_ok() { warn!("resolved ty var in error message"); } - if let TypeVariableOriginKind::TypeParameterDefinition(name, _) = - infcx.inner.borrow_mut().type_variables().var_origin(ty_vid).kind + + let mut infcx_inner = infcx.inner.borrow_mut(); + let ty_vars = infcx_inner.type_variables(); + let var_origin = ty_vars.var_origin(ty_vid); + if let TypeVariableOriginKind::TypeParameterDefinition(name, _) = var_origin.kind + && !var_origin.span.from_expansion() { Some(name) } else { @@ -254,7 +258,7 @@ impl<'tcx> InferCtxt<'tcx> { if let TypeVariableOriginKind::TypeParameterDefinition(name, def_id) = var_origin.kind { - if name != kw::SelfUpper { + if name != kw::SelfUpper && !var_origin.span.from_expansion() { return InferenceDiagnosticsData { name: name.to_string(), span: Some(var_origin.span), @@ -780,7 +784,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { // The sources are listed in order of preference here. let tcx = self.infcx.tcx; let ctx = CostCtxt { tcx }; - let base_cost = match source.kind { + match source.kind { InferSourceKind::LetBinding { ty, .. } => ctx.ty_cost(ty), InferSourceKind::ClosureArg { ty, .. } => ctx.ty_cost(ty), InferSourceKind::GenericArg { def_id, generic_args, .. } => { @@ -797,17 +801,17 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { InferSourceKind::ClosureReturn { ty, should_wrap_expr, .. } => { 30 + ctx.ty_cost(ty) + if should_wrap_expr.is_some() { 10 } else { 0 } } - }; - - let suggestion_may_apply = if source.from_expansion() { 10000 } else { 0 }; - - base_cost + suggestion_may_apply + } } /// Uses `fn source_cost` to determine whether this inference source is preferable to /// previous sources. We generally prefer earlier sources. #[instrument(level = "debug", skip(self))] fn update_infer_source(&mut self, mut new_source: InferSource<'tcx>) { + if new_source.from_expansion() { + return; + } + let cost = self.source_cost(&new_source) + self.attempt; debug!(?cost); self.attempt += 1; @@ -819,6 +823,7 @@ impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> { // `let x: _ = iter.collect();`, as this is a very common case. *def_id = Some(did); } + if cost < self.infer_source_cost { self.infer_source_cost = cost; self.infer_source = Some(new_source); diff --git a/tests/ui/inference/need_type_info/issue-107745-avoid-expr-from-macro-expansion.rs b/tests/ui/inference/need_type_info/issue-107745-avoid-expr-from-macro-expansion.rs new file mode 100644 index 0000000000000..7f6758f47f8fe --- /dev/null +++ b/tests/ui/inference/need_type_info/issue-107745-avoid-expr-from-macro-expansion.rs @@ -0,0 +1,19 @@ +// ignore-tidy-linelength + +// Regression test for #107745. +// Previously need_type_info::update_infer_source will consider expressions originating from +// macro expressions as candiate "previous sources". This unfortunately can mean that +// for macros expansions such as `format!()` internal implementation details can leak, such as: +// +// ``` +// error[E0282]: type annotations needed +// --> src/main.rs:2:22 +// | +//2 | println!("{:?}", []); +// | ^^ cannot infer type of the type parameter `T` declared on the associated function `new_debug` +// ``` + +fn main() { + println!("{:?}", []); + //~^ ERROR type annotations needed +} diff --git a/tests/ui/inference/need_type_info/issue-107745-avoid-expr-from-macro-expansion.stderr b/tests/ui/inference/need_type_info/issue-107745-avoid-expr-from-macro-expansion.stderr new file mode 100644 index 0000000000000..464655bbcf451 --- /dev/null +++ b/tests/ui/inference/need_type_info/issue-107745-avoid-expr-from-macro-expansion.stderr @@ -0,0 +1,11 @@ +error[E0282]: type annotations needed + --> $DIR/issue-107745-avoid-expr-from-macro-expansion.rs:17:22 + | +LL | println!("{:?}", []); + | ^^ cannot infer type + | + = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0282`. diff --git a/tests/ui/issues/issue-16966.stderr b/tests/ui/issues/issue-16966.stderr index 60f5190dbd0d6..8c92505b5eb28 100644 --- a/tests/ui/issues/issue-16966.stderr +++ b/tests/ui/issues/issue-16966.stderr @@ -1,10 +1,8 @@ error[E0282]: type annotations needed - --> $DIR/issue-16966.rs:2:5 + --> $DIR/issue-16966.rs:2:12 | LL | panic!(std::default::Default::default()); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `M` declared on the function `begin_panic` - | - = note: this error originates in the macro `$crate::panic::panic_2015` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info) + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type error: aborting due to previous error diff --git a/tests/ui/parser/missing-closing-angle-bracket-eq-constraint.rs b/tests/ui/parser/missing-closing-angle-bracket-eq-constraint.rs index da95c1bfa2709..a56cd17773dc4 100644 --- a/tests/ui/parser/missing-closing-angle-bracket-eq-constraint.rs +++ b/tests/ui/parser/missing-closing-angle-bracket-eq-constraint.rs @@ -17,7 +17,7 @@ fn test2(arg1 : T1, arg2 : T2) { fn test3<'a>(arg : &'a u32) { let v : Vec<'a = vec![]; //~^ ERROR: expected one of - //~| ERROR: type annotations needed for `Vec` + //~| ERROR: type annotations needed for `Vec<_>` } fn main() {} diff --git a/tests/ui/parser/missing-closing-angle-bracket-eq-constraint.stderr b/tests/ui/parser/missing-closing-angle-bracket-eq-constraint.stderr index bad241634cbe6..b2448774ae9d3 100644 --- a/tests/ui/parser/missing-closing-angle-bracket-eq-constraint.stderr +++ b/tests/ui/parser/missing-closing-angle-bracket-eq-constraint.stderr @@ -39,26 +39,26 @@ help: you might have meant to end the type parameters here LL | let v : Vec<'a> = vec![]; | + -error[E0282]: type annotations needed for `Vec` +error[E0282]: type annotations needed for `Vec<_>` --> $DIR/missing-closing-angle-bracket-eq-constraint.rs:7:7 | LL | let v : Vec<(u32,_) = vec![]; | ^ | -help: consider giving `v` an explicit type, where the type for type parameter `T` is specified +help: consider giving `v` an explicit type, where the placeholders `_` are specified | -LL | let v: Vec : Vec<(u32,_) = vec![]; +LL | let v: Vec<_> : Vec<(u32,_) = vec![]; | ++++++++ -error[E0282]: type annotations needed for `Vec` +error[E0282]: type annotations needed for `Vec<_>` --> $DIR/missing-closing-angle-bracket-eq-constraint.rs:18:7 | LL | let v : Vec<'a = vec![]; | ^ | -help: consider giving `v` an explicit type, where the type for type parameter `T` is specified +help: consider giving `v` an explicit type, where the placeholders `_` are specified | -LL | let v: Vec : Vec<'a = vec![]; +LL | let v: Vec<_> : Vec<'a = vec![]; | ++++++++ error: aborting due to 5 previous errors diff --git a/tests/ui/type/type-check/cannot_infer_local_or_vec.stderr b/tests/ui/type/type-check/cannot_infer_local_or_vec.stderr index b63d2a3b61c24..09c4b2053b27e 100644 --- a/tests/ui/type/type-check/cannot_infer_local_or_vec.stderr +++ b/tests/ui/type/type-check/cannot_infer_local_or_vec.stderr @@ -1,12 +1,12 @@ -error[E0282]: type annotations needed for `Vec` +error[E0282]: type annotations needed for `Vec<_>` --> $DIR/cannot_infer_local_or_vec.rs:2:9 | LL | let x = vec![]; | ^ | -help: consider giving `x` an explicit type, where the type for type parameter `T` is specified +help: consider giving `x` an explicit type, where the placeholders `_` are specified | -LL | let x: Vec = vec![]; +LL | let x: Vec<_> = vec![]; | ++++++++ error: aborting due to previous error diff --git a/tests/ui/type/type-check/cannot_infer_local_or_vec_in_tuples.stderr b/tests/ui/type/type-check/cannot_infer_local_or_vec_in_tuples.stderr index e544b36951548..1fa253052e649 100644 --- a/tests/ui/type/type-check/cannot_infer_local_or_vec_in_tuples.stderr +++ b/tests/ui/type/type-check/cannot_infer_local_or_vec_in_tuples.stderr @@ -1,12 +1,12 @@ -error[E0282]: type annotations needed for `(Vec,)` +error[E0282]: type annotations needed for `(Vec<_>,)` --> $DIR/cannot_infer_local_or_vec_in_tuples.rs:2:9 | LL | let (x, ) = (vec![], ); | ^^^^^ ---------- type must be known at this point | -help: consider giving this pattern a type, where the type for type parameter `T` is specified +help: consider giving this pattern a type, where the placeholders `_` are specified | -LL | let (x, ): (Vec,) = (vec![], ); +LL | let (x, ): (Vec<_>,) = (vec![], ); | +++++++++++ error: aborting due to previous error From b940f5088e725026820a9f9a47bdd40c01786233 Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 9 Feb 2023 09:11:25 +0000 Subject: [PATCH 2/8] fix #107822, handle properly when there is no crate attrs --- compiler/rustc_driver_impl/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index a392d70f100a4..5e403d846e1ef 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -628,7 +628,7 @@ fn print_crate_info( println!("{}", serde_json::to_string_pretty(&sess.target.to_json()).unwrap()); } FileNames | CrateName => { - let attrs = attrs.as_ref().unwrap(); + let Some(attrs) = attrs.as_ref() else { return Compilation::Continue; }; let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess); let id = rustc_session::output::find_crate_name(sess, attrs); if *req == PrintRequest::CrateName { From 569c2fe54ba676e6aaeece218e5564c4508d7c73 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 9 Feb 2023 11:18:21 +0100 Subject: [PATCH 3/8] avoid duplicating the RUSTC_LOG env var name --- compiler/rustc_log/src/lib.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/compiler/rustc_log/src/lib.rs b/compiler/rustc_log/src/lib.rs index 019fdc30dcec5..e3d850e6a3b02 100644 --- a/compiler/rustc_log/src/lib.rs +++ b/compiler/rustc_log/src/lib.rs @@ -53,12 +53,6 @@ use tracing_subscriber::fmt::{ }; use tracing_subscriber::layer::SubscriberExt; -pub fn init_rustc_env_logger() -> Result<(), Error> { - init_env_logger("RUSTC_LOG") -} - -/// In contrast to `init_rustc_env_logger` this allows you to choose an env var -/// other than `RUSTC_LOG`. pub fn init_env_logger(env: &str) -> Result<(), Error> { let filter = match env::var(env) { Ok(env) => EnvFilter::new(env), From 4b1157509f96e4fffe7ef40ab8cbaa08704d36cc Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 9 Feb 2023 19:01:47 -0800 Subject: [PATCH 4/8] Allow wasi-libc to initialize its environment variables lazily. Use `__wasilibc_get_environ()` to read the environment variable list from wasi-libc instead of using `environ`. `environ` is a global variable which effectively requires wasi-libc to initialize the environment variables eagerly, and `__wasilibc_get_environ()` is specifically designed to be an alternative that lets wasi-libc intiailize its environment variables lazily. This should have the side effect of fixing at least some of the cases of #107635. --- library/std/src/sys/wasi/os.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys/wasi/os.rs b/library/std/src/sys/wasi/os.rs index f5513e9996d40..9919dc7087ec1 100644 --- a/library/std/src/sys/wasi/os.rs +++ b/library/std/src/sys/wasi/os.rs @@ -21,6 +21,7 @@ mod libc { extern "C" { pub fn getcwd(buf: *mut c_char, size: size_t) -> *mut c_char; pub fn chdir(dir: *const c_char) -> c_int; + pub fn __wasilibc_get_environ() -> *mut *mut c_char; } } @@ -161,7 +162,12 @@ impl Iterator for Env { pub fn env() -> Env { unsafe { let _guard = env_read_lock(); - let mut environ = libc::environ; + + // Use `__wasilibc_get_environ` instead of `environ` here so that we + // don't require wasi-libc to eagerly initialize the environment + // variables. + let mut environ = libc::__wasilibc_get_environ(); + let mut result = Vec::new(); if !environ.is_null() { while !(*environ).is_null() { From d560574c4bd621cfabaa413c79e76b1da1abf279 Mon Sep 17 00:00:00 2001 From: Zephaniah Ong Date: Fri, 10 Feb 2023 15:25:01 +0800 Subject: [PATCH 5/8] create symlink only for non-windows operating systems --- src/bootstrap/download.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/bootstrap/download.rs b/src/bootstrap/download.rs index 5c863015adb27..7c5e7fb6ebc92 100644 --- a/src/bootstrap/download.rs +++ b/src/bootstrap/download.rs @@ -340,9 +340,12 @@ impl Config { let rustfmt_path = bin_root.join("bin").join(exe("rustfmt", host)); let rustfmt_stamp = bin_root.join(".rustfmt-stamp"); - let legacy_rustfmt = self.initial_rustc.with_file_name(exe("rustfmt", host)); - if !legacy_rustfmt.exists() { - t!(self.symlink_file(&rustfmt_path, &legacy_rustfmt)); + #[cfg(not(windows))] + { + let legacy_rustfmt = self.initial_rustc.with_file_name(exe("rustfmt", host)); + if !legacy_rustfmt.exists() { + t!(self.symlink_file(&rustfmt_path, &legacy_rustfmt)); + } } if rustfmt_path.exists() && !program_out_of_date(&rustfmt_stamp, &channel) { From 257389882de113945d3f9f1abb9d0a431ca3cf92 Mon Sep 17 00:00:00 2001 From: yukang Date: Fri, 10 Feb 2023 08:01:50 +0000 Subject: [PATCH 6/8] add test for no input file --- compiler/rustc_driver_impl/src/lib.rs | 5 ++++- tests/run-make/no-input-file/Makefile | 4 ++++ tests/run-make/no-input-file/no-input-file.stderr | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/run-make/no-input-file/Makefile create mode 100644 tests/run-make/no-input-file/no-input-file.stderr diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 5e403d846e1ef..e5104e2d3f9d1 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -628,7 +628,10 @@ fn print_crate_info( println!("{}", serde_json::to_string_pretty(&sess.target.to_json()).unwrap()); } FileNames | CrateName => { - let Some(attrs) = attrs.as_ref() else { return Compilation::Continue; }; + let Some(attrs) = attrs.as_ref() else { + // no crate attributes, print out an error and exit + return Compilation::Continue; + }; let t_outputs = rustc_interface::util::build_output_filenames(attrs, sess); let id = rustc_session::output::find_crate_name(sess, attrs); if *req == PrintRequest::CrateName { diff --git a/tests/run-make/no-input-file/Makefile b/tests/run-make/no-input-file/Makefile new file mode 100644 index 0000000000000..2f02159229d3c --- /dev/null +++ b/tests/run-make/no-input-file/Makefile @@ -0,0 +1,4 @@ +include ../../run-make-fulldeps/tools.mk + +all: + $(RUSTC) --print crate-name 2>&1 | diff - no-input-file.stderr diff --git a/tests/run-make/no-input-file/no-input-file.stderr b/tests/run-make/no-input-file/no-input-file.stderr new file mode 100644 index 0000000000000..b843eb524f3d2 --- /dev/null +++ b/tests/run-make/no-input-file/no-input-file.stderr @@ -0,0 +1,2 @@ +error: no input filename given + From 1f76cea515cf8155a880951e99797c55289ccd97 Mon Sep 17 00:00:00 2001 From: Jubilee <46493976+workingjubilee@users.noreply.github.com> Date: Fri, 10 Feb 2023 01:48:45 -0800 Subject: [PATCH 7/8] Cleanup typos in en_US/borrowck.ftl --- compiler/rustc_error_messages/locales/en-US/borrowck.ftl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl index fe77cf23e8f94..a3b6b5e8138b6 100644 --- a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl @@ -18,7 +18,7 @@ borrowck_generic_does_not_live_long_enough = `{$kind}` does not live long enough borrowck_move_borrowed = - cannot move out of `{$desc}` beacause it is borrowed + cannot move out of `{$desc}` because it is borrowed borrowck_var_does_not_need_mut = variable does not need to be mutable @@ -87,10 +87,10 @@ borrowck_use_due_to_use_closure = use occurs due to use in closure borrowck_assign_due_to_use_closure = - assign occurs due to use in closure + assignment occurs due to use in closure borrowck_assign_part_due_to_use_closure = - assign to part occurs due to use in closure + assignment to part occurs due to use in closure borrowck_capture_immute = capture is immutable because of use here From 623ed8e481204b2ed3876084f765ac4fb59798b5 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 10 Feb 2023 15:07:55 +0100 Subject: [PATCH 8/8] fix rustc_log doctest --- compiler/rustc_log/src/lib.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_log/src/lib.rs b/compiler/rustc_log/src/lib.rs index e3d850e6a3b02..de26fd61e4d60 100644 --- a/compiler/rustc_log/src/lib.rs +++ b/compiler/rustc_log/src/lib.rs @@ -14,7 +14,7 @@ //! //! ``` //! fn main() { -//! rustc_log::init_rustc_env_logger().unwrap(); +//! rustc_log::init_env_logger("LOG").unwrap(); //! //! let edition = rustc_span::edition::Edition::Edition2021; //! rustc_span::create_session_globals_then(edition, || { @@ -23,9 +23,9 @@ //! } //! ``` //! -//! Now `RUSTC_LOG=debug cargo run` will run your minimal main.rs and show +//! Now `LOG=debug cargo run` will run your minimal main.rs and show //! rustc's debug logging. In a workflow like this, one might also add -//! `std::env::set_var("RUSTC_LOG", "debug")` to the top of main so that `cargo +//! `std::env::set_var("LOG", "debug")` to the top of main so that `cargo //! run` by itself is sufficient to get logs. //! //! The reason rustc_log is a tiny separate crate, as opposed to exposing the