From 44c166e9ace21835b2794005573c2b1964e29acc Mon Sep 17 00:00:00 2001 From: Urgau Date: Tue, 19 Nov 2024 20:32:48 +0100 Subject: [PATCH 01/11] Add Cargo revision and values to the check-cfg macro test --- tests/ui/check-cfg/auxiliary/cfg_macro.rs | 16 ++++++++ .../report-in-external-macros.cargo.stderr | 39 +++++++++++++++++++ .../ui/check-cfg/report-in-external-macros.rs | 11 +++++- .../report-in-external-macros.rustc.stderr | 35 +++++++++++++++++ .../report-in-external-macros.stderr | 14 ------- 5 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 tests/ui/check-cfg/report-in-external-macros.cargo.stderr create mode 100644 tests/ui/check-cfg/report-in-external-macros.rustc.stderr delete mode 100644 tests/ui/check-cfg/report-in-external-macros.stderr diff --git a/tests/ui/check-cfg/auxiliary/cfg_macro.rs b/tests/ui/check-cfg/auxiliary/cfg_macro.rs index d68accd9202f7..1293fd6975e67 100644 --- a/tests/ui/check-cfg/auxiliary/cfg_macro.rs +++ b/tests/ui/check-cfg/auxiliary/cfg_macro.rs @@ -9,3 +9,19 @@ macro_rules! my_lib_macro { $crate::my_lib_func() }; } + +#[macro_export] +macro_rules! my_lib_macro_value { + () => { + #[cfg(panic = "UNEXPECTED_VALUE")] + $crate::my_lib_func() + }; +} + +#[macro_export] +macro_rules! my_lib_macro_feature { + () => { + #[cfg(feature = "UNEXPECTED_FEATURE")] + $crate::my_lib_func() + }; +} diff --git a/tests/ui/check-cfg/report-in-external-macros.cargo.stderr b/tests/ui/check-cfg/report-in-external-macros.cargo.stderr new file mode 100644 index 0000000000000..98c7a06834713 --- /dev/null +++ b/tests/ui/check-cfg/report-in-external-macros.cargo.stderr @@ -0,0 +1,39 @@ +warning: unexpected `cfg` condition name: `my_lib_cfg` + --> $DIR/report-in-external-macros.rs:13:5 + | +LL | cfg_macro::my_lib_macro!(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `feature`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows` + = help: consider using a Cargo feature instead + = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: + [lints.rust] + unexpected_cfgs = { level = "warn", check-cfg = ['cfg(my_lib_cfg)'] } + = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(my_lib_cfg)");` to the top of the `build.rs` + = note: see for more information about checking conditional configuration + = note: `#[warn(unexpected_cfgs)]` on by default + = note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: unexpected `cfg` condition value: `UNEXPECTED_VALUE` + --> $DIR/report-in-external-macros.rs:16:5 + | +LL | cfg_macro::my_lib_macro_value!(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: expected values for `panic` are: `abort` and `unwind` + = note: see for more information about checking conditional configuration + = note: this warning originates in the macro `cfg_macro::my_lib_macro_value` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: unexpected `cfg` condition value: `UNEXPECTED_FEATURE` + --> $DIR/report-in-external-macros.rs:19:5 + | +LL | cfg_macro::my_lib_macro_feature!(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: no expected values for `feature` + = help: consider adding `UNEXPECTED_FEATURE` as a feature in `Cargo.toml` + = note: see for more information about checking conditional configuration + = note: this warning originates in the macro `cfg_macro::my_lib_macro_feature` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: 3 warnings emitted + diff --git a/tests/ui/check-cfg/report-in-external-macros.rs b/tests/ui/check-cfg/report-in-external-macros.rs index 56550b04af305..72c2f6f6618fb 100644 --- a/tests/ui/check-cfg/report-in-external-macros.rs +++ b/tests/ui/check-cfg/report-in-external-macros.rs @@ -3,10 +3,19 @@ //@ check-pass //@ no-auto-check-cfg +//@ revisions: cargo rustc +//@ [rustc]unset-rustc-env:CARGO_CRATE_NAME +//@ [cargo]rustc-env:CARGO_CRATE_NAME=foo //@ aux-crate: cfg_macro=cfg_macro.rs -//@ compile-flags: --check-cfg=cfg() +//@ compile-flags: --check-cfg=cfg(feature,values()) fn main() { cfg_macro::my_lib_macro!(); //~^ WARNING unexpected `cfg` condition name + + cfg_macro::my_lib_macro_value!(); + //~^ WARNING unexpected `cfg` condition value + + cfg_macro::my_lib_macro_feature!(); + //~^ WARNING unexpected `cfg` condition value } diff --git a/tests/ui/check-cfg/report-in-external-macros.rustc.stderr b/tests/ui/check-cfg/report-in-external-macros.rustc.stderr new file mode 100644 index 0000000000000..15d12c064554b --- /dev/null +++ b/tests/ui/check-cfg/report-in-external-macros.rustc.stderr @@ -0,0 +1,35 @@ +warning: unexpected `cfg` condition name: `my_lib_cfg` + --> $DIR/report-in-external-macros.rs:13:5 + | +LL | cfg_macro::my_lib_macro!(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `feature`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows` + = help: to expect this configuration use `--check-cfg=cfg(my_lib_cfg)` + = note: see for more information about checking conditional configuration + = note: `#[warn(unexpected_cfgs)]` on by default + = note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: unexpected `cfg` condition value: `UNEXPECTED_VALUE` + --> $DIR/report-in-external-macros.rs:16:5 + | +LL | cfg_macro::my_lib_macro_value!(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: expected values for `panic` are: `abort` and `unwind` + = note: see for more information about checking conditional configuration + = note: this warning originates in the macro `cfg_macro::my_lib_macro_value` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: unexpected `cfg` condition value: `UNEXPECTED_FEATURE` + --> $DIR/report-in-external-macros.rs:19:5 + | +LL | cfg_macro::my_lib_macro_feature!(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: no expected values for `feature` + = help: to expect this configuration use `--check-cfg=cfg(feature, values("UNEXPECTED_FEATURE"))` + = note: see for more information about checking conditional configuration + = note: this warning originates in the macro `cfg_macro::my_lib_macro_feature` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: 3 warnings emitted + diff --git a/tests/ui/check-cfg/report-in-external-macros.stderr b/tests/ui/check-cfg/report-in-external-macros.stderr deleted file mode 100644 index 11300a4e40272..0000000000000 --- a/tests/ui/check-cfg/report-in-external-macros.stderr +++ /dev/null @@ -1,14 +0,0 @@ -warning: unexpected `cfg` condition name: `my_lib_cfg` - --> $DIR/report-in-external-macros.rs:10:5 - | -LL | cfg_macro::my_lib_macro!(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows` - = help: to expect this configuration use `--check-cfg=cfg(my_lib_cfg)` - = note: see for more information about checking conditional configuration - = note: `#[warn(unexpected_cfgs)]` on by default - = note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info) - -warning: 1 warning emitted - From 0ab3ae81a59aba3fed45d5894e931d4250c18aa8 Mon Sep 17 00:00:00 2001 From: Urgau Date: Tue, 19 Nov 2024 21:23:54 +0100 Subject: [PATCH 02/11] Disable most Cargo check-cfg help in external macros --- .../rustc_lint/src/context/diagnostics/check_cfg.rs | 13 ++++++++++--- .../report-in-external-macros.cargo.stderr | 6 ------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs index 16994846545df..4ab30ce32ee4f 100644 --- a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs +++ b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs @@ -85,6 +85,7 @@ pub(super) fn unexpected_cfg_name( }; let is_from_cargo = rustc_session::utils::was_invoked_from_cargo(); + let is_from_external_macro = rustc_middle::lint::in_external_macro(sess, name_span); let mut is_feature_cfg = name == sym::feature; let code_sugg = if is_feature_cfg && is_from_cargo { @@ -185,7 +186,11 @@ pub(super) fn unexpected_cfg_name( }; let invocation_help = if is_from_cargo { - let sub = if !is_feature_cfg { Some(cargo_help_sub(sess, &inst)) } else { None }; + let sub = if !is_feature_cfg && !is_from_external_macro { + Some(cargo_help_sub(sess, &inst)) + } else { + None + }; lints::unexpected_cfg_name::InvocationHelp::Cargo { sub } } else { lints::unexpected_cfg_name::InvocationHelp::Rustc(lints::UnexpectedCfgRustcHelp::new( @@ -216,7 +221,9 @@ pub(super) fn unexpected_cfg_value( .copied() .flatten() .collect(); + let is_from_cargo = rustc_session::utils::was_invoked_from_cargo(); + let is_from_external_macro = rustc_middle::lint::in_external_macro(sess, name_span); // Show the full list if all possible values for a given name, but don't do it // for names as the possibilities could be very long @@ -284,13 +291,13 @@ pub(super) fn unexpected_cfg_value( }; let invocation_help = if is_from_cargo { - let help = if name == sym::feature { + let help = if name == sym::feature && !is_from_external_macro { if let Some((value, _value_span)) = value { Some(lints::unexpected_cfg_value::CargoHelp::AddFeature { value }) } else { Some(lints::unexpected_cfg_value::CargoHelp::DefineFeatures) } - } else if can_suggest_adding_value { + } else if can_suggest_adding_value && !is_from_external_macro { Some(lints::unexpected_cfg_value::CargoHelp::Other(cargo_help_sub(sess, &inst))) } else { None diff --git a/tests/ui/check-cfg/report-in-external-macros.cargo.stderr b/tests/ui/check-cfg/report-in-external-macros.cargo.stderr index 98c7a06834713..0858b01e309c9 100644 --- a/tests/ui/check-cfg/report-in-external-macros.cargo.stderr +++ b/tests/ui/check-cfg/report-in-external-macros.cargo.stderr @@ -5,11 +5,6 @@ LL | cfg_macro::my_lib_macro!(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `feature`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows` - = help: consider using a Cargo feature instead - = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint: - [lints.rust] - unexpected_cfgs = { level = "warn", check-cfg = ['cfg(my_lib_cfg)'] } - = help: or consider adding `println!("cargo::rustc-check-cfg=cfg(my_lib_cfg)");` to the top of the `build.rs` = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default = note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -31,7 +26,6 @@ LL | cfg_macro::my_lib_macro_feature!(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: no expected values for `feature` - = help: consider adding `UNEXPECTED_FEATURE` as a feature in `Cargo.toml` = note: see for more information about checking conditional configuration = note: this warning originates in the macro `cfg_macro::my_lib_macro_feature` (in Nightly builds, run with -Z macro-backtrace for more info) From e2fbeec150b1bf4408cc7c30f4fa06e224186e71 Mon Sep 17 00:00:00 2001 From: Urgau Date: Tue, 19 Nov 2024 22:46:11 +0100 Subject: [PATCH 03/11] Add external macro specific diagnostic to check-cfg --- compiler/rustc_lint/messages.ftl | 4 ++ .../src/context/diagnostics/check_cfg.rs | 57 ++++++++++++++++--- compiler/rustc_lint/src/lints.rs | 44 ++++++++++++-- .../report-in-external-macros.cargo.stderr | 9 +++ .../report-in-external-macros.rustc.stderr | 6 ++ 5 files changed, 108 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 6e35d89b48800..436e14e93d29a 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -803,10 +803,14 @@ lint_unexpected_cfg_add_build_rs_println = or consider adding `{$build_rs_printl lint_unexpected_cfg_add_cargo_feature = consider using a Cargo feature instead lint_unexpected_cfg_add_cargo_toml_lint_cfg = or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:{$cargo_toml_lint_cfg} lint_unexpected_cfg_add_cmdline_arg = to expect this configuration use `{$cmdline_arg}` +lint_unexpected_cfg_cargo_update = the {$macro_kind} `{$macro_name}` may come from an old version of it's defining crate, try updating your dependencies with `cargo update` + lint_unexpected_cfg_define_features = consider defining some features in `Cargo.toml` lint_unexpected_cfg_doc_cargo = see for more information about checking conditional configuration lint_unexpected_cfg_doc_rustc = see for more information about checking conditional configuration +lint_unexpected_cfg_from_external_macro_origin = using a cfg inside a {$macro_kind} will use the cfgs from the destination crate and not the ones from the defining crate +lint_unexpected_cfg_from_external_macro_refer = try refering to `{$macro_name}` crate for guidance on how handle this unexpected cfg lint_unexpected_cfg_name = unexpected `cfg` condition name: `{$name}` lint_unexpected_cfg_name_expected_names = expected names are: {$possibilities}{$and_more -> [0] {""} diff --git a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs index 4ab30ce32ee4f..63a722f606762 100644 --- a/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs +++ b/compiler/rustc_lint/src/context/diagnostics/check_cfg.rs @@ -1,9 +1,10 @@ +use rustc_hir::def_id::LOCAL_CRATE; use rustc_middle::bug; use rustc_session::Session; use rustc_session::config::ExpectedValues; use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::symbol::Ident; -use rustc_span::{Span, Symbol, sym}; +use rustc_span::{ExpnKind, Span, Symbol, sym}; use crate::lints; @@ -60,6 +61,35 @@ fn cargo_help_sub( } } +fn rustc_macro_help(span: Span) -> Option { + let oexpn = span.ctxt().outer_expn_data(); + if let Some(def_id) = oexpn.macro_def_id + && let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind + && def_id.krate != LOCAL_CRATE + { + Some(lints::UnexpectedCfgRustcMacroHelp { macro_kind: macro_kind.descr(), macro_name }) + } else { + None + } +} + +fn cargo_macro_help(span: Span) -> Option { + let oexpn = span.ctxt().outer_expn_data(); + if let Some(def_id) = oexpn.macro_def_id + && let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind + && def_id.krate != LOCAL_CRATE + { + Some(lints::UnexpectedCfgCargoMacroHelp { + macro_kind: macro_kind.descr(), + macro_name, + // FIXME: Get access to a `TyCtxt` from an `EarlyContext` + // crate_name: cx.tcx.crate_name(def_id.krate), + }) + } else { + None + } +} + pub(super) fn unexpected_cfg_name( sess: &Session, (name, name_span): (Symbol, Span), @@ -186,16 +216,21 @@ pub(super) fn unexpected_cfg_name( }; let invocation_help = if is_from_cargo { - let sub = if !is_feature_cfg && !is_from_external_macro { + let help = if !is_feature_cfg && !is_from_external_macro { Some(cargo_help_sub(sess, &inst)) } else { None }; - lints::unexpected_cfg_name::InvocationHelp::Cargo { sub } + lints::unexpected_cfg_name::InvocationHelp::Cargo { + help, + macro_help: cargo_macro_help(name_span), + } } else { - lints::unexpected_cfg_name::InvocationHelp::Rustc(lints::UnexpectedCfgRustcHelp::new( - &inst(EscapeQuotes::No), - )) + let help = lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No)); + lints::unexpected_cfg_name::InvocationHelp::Rustc { + help, + macro_help: rustc_macro_help(name_span), + } }; lints::UnexpectedCfgName { code_sugg, invocation_help, name } @@ -302,14 +337,20 @@ pub(super) fn unexpected_cfg_value( } else { None }; - lints::unexpected_cfg_value::InvocationHelp::Cargo(help) + lints::unexpected_cfg_value::InvocationHelp::Cargo { + help, + macro_help: cargo_macro_help(name_span), + } } else { let help = if can_suggest_adding_value { Some(lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No))) } else { None }; - lints::unexpected_cfg_value::InvocationHelp::Rustc(help) + lints::unexpected_cfg_value::InvocationHelp::Rustc { + help, + macro_help: rustc_macro_help(name_span), + } }; lints::UnexpectedCfgValue { diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 352155729e51c..3bf9bf0700560 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -2131,6 +2131,25 @@ impl UnexpectedCfgRustcHelp { } } +#[derive(Subdiagnostic)] +#[note(lint_unexpected_cfg_from_external_macro_origin)] +#[help(lint_unexpected_cfg_from_external_macro_refer)] +pub(crate) struct UnexpectedCfgRustcMacroHelp { + pub macro_kind: &'static str, + pub macro_name: Symbol, +} + +#[derive(Subdiagnostic)] +#[note(lint_unexpected_cfg_from_external_macro_origin)] +#[help(lint_unexpected_cfg_from_external_macro_refer)] +#[help(lint_unexpected_cfg_cargo_update)] +pub(crate) struct UnexpectedCfgCargoMacroHelp { + pub macro_kind: &'static str, + pub macro_name: Symbol, + // FIXME: Figure out a way to get the crate name + // crate_name: String, +} + #[derive(LintDiagnostic)] #[diag(lint_unexpected_cfg_name)] pub(crate) struct UnexpectedCfgName { @@ -2235,10 +2254,17 @@ pub(crate) mod unexpected_cfg_name { #[note(lint_unexpected_cfg_doc_cargo)] Cargo { #[subdiagnostic] - sub: Option, + macro_help: Option, + #[subdiagnostic] + help: Option, }, #[note(lint_unexpected_cfg_doc_rustc)] - Rustc(#[subdiagnostic] super::UnexpectedCfgRustcHelp), + Rustc { + #[subdiagnostic] + macro_help: Option, + #[subdiagnostic] + help: super::UnexpectedCfgRustcHelp, + }, } } @@ -2341,9 +2367,19 @@ pub(crate) mod unexpected_cfg_value { #[derive(Subdiagnostic)] pub(crate) enum InvocationHelp { #[note(lint_unexpected_cfg_doc_cargo)] - Cargo(#[subdiagnostic] Option), + Cargo { + #[subdiagnostic] + help: Option, + #[subdiagnostic] + macro_help: Option, + }, #[note(lint_unexpected_cfg_doc_rustc)] - Rustc(#[subdiagnostic] Option), + Rustc { + #[subdiagnostic] + help: Option, + #[subdiagnostic] + macro_help: Option, + }, } #[derive(Subdiagnostic)] diff --git a/tests/ui/check-cfg/report-in-external-macros.cargo.stderr b/tests/ui/check-cfg/report-in-external-macros.cargo.stderr index 0858b01e309c9..6fb397b5529b5 100644 --- a/tests/ui/check-cfg/report-in-external-macros.cargo.stderr +++ b/tests/ui/check-cfg/report-in-external-macros.cargo.stderr @@ -5,6 +5,9 @@ LL | cfg_macro::my_lib_macro!(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `feature`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows` + = note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate + = help: try refering to `cfg_macro::my_lib_macro` crate for guidance on how handle this unexpected cfg + = help: the macro `cfg_macro::my_lib_macro` may come from an old version of it's defining crate, try updating your dependencies with `cargo update` = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default = note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -16,6 +19,9 @@ LL | cfg_macro::my_lib_macro_value!(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: expected values for `panic` are: `abort` and `unwind` + = note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate + = help: try refering to `cfg_macro::my_lib_macro_value` crate for guidance on how handle this unexpected cfg + = help: the macro `cfg_macro::my_lib_macro_value` may come from an old version of it's defining crate, try updating your dependencies with `cargo update` = note: see for more information about checking conditional configuration = note: this warning originates in the macro `cfg_macro::my_lib_macro_value` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -26,6 +32,9 @@ LL | cfg_macro::my_lib_macro_feature!(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: no expected values for `feature` + = note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate + = help: try refering to `cfg_macro::my_lib_macro_feature` crate for guidance on how handle this unexpected cfg + = help: the macro `cfg_macro::my_lib_macro_feature` may come from an old version of it's defining crate, try updating your dependencies with `cargo update` = note: see for more information about checking conditional configuration = note: this warning originates in the macro `cfg_macro::my_lib_macro_feature` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/check-cfg/report-in-external-macros.rustc.stderr b/tests/ui/check-cfg/report-in-external-macros.rustc.stderr index 15d12c064554b..1a03184ee814b 100644 --- a/tests/ui/check-cfg/report-in-external-macros.rustc.stderr +++ b/tests/ui/check-cfg/report-in-external-macros.rustc.stderr @@ -5,6 +5,8 @@ LL | cfg_macro::my_lib_macro!(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `feature`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows` + = note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate + = help: try refering to `cfg_macro::my_lib_macro` crate for guidance on how handle this unexpected cfg = help: to expect this configuration use `--check-cfg=cfg(my_lib_cfg)` = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default @@ -17,6 +19,8 @@ LL | cfg_macro::my_lib_macro_value!(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: expected values for `panic` are: `abort` and `unwind` + = note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate + = help: try refering to `cfg_macro::my_lib_macro_value` crate for guidance on how handle this unexpected cfg = note: see for more information about checking conditional configuration = note: this warning originates in the macro `cfg_macro::my_lib_macro_value` (in Nightly builds, run with -Z macro-backtrace for more info) @@ -28,6 +32,8 @@ LL | cfg_macro::my_lib_macro_feature!(); | = note: no expected values for `feature` = help: to expect this configuration use `--check-cfg=cfg(feature, values("UNEXPECTED_FEATURE"))` + = note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate + = help: try refering to `cfg_macro::my_lib_macro_feature` crate for guidance on how handle this unexpected cfg = note: see for more information about checking conditional configuration = note: this warning originates in the macro `cfg_macro::my_lib_macro_feature` (in Nightly builds, run with -Z macro-backtrace for more info) From 9c37c14aa2456315bf7b3fa7b3e089bc13a43ee9 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Sat, 23 Nov 2024 10:24:35 -0600 Subject: [PATCH 04/11] Update linux_musl base to dynamically link the crt by default However, don't change the behavior of any existing targets at this time. For targets that used the old default, explicitly set `crt_static_default = true`. This makes it easier for new targets to use the correct defaults while leaving the changing of individual targets to future PRs. Related to /~https://github.com/rust-lang/compiler-team/issues/422 --- compiler/rustc_target/src/spec/base/linux_musl.rs | 3 --- .../src/spec/targets/aarch64_unknown_linux_musl.rs | 3 +++ .../src/spec/targets/arm_unknown_linux_musleabi.rs | 2 ++ .../src/spec/targets/arm_unknown_linux_musleabihf.rs | 2 ++ .../src/spec/targets/armv5te_unknown_linux_musleabi.rs | 2 ++ .../src/spec/targets/armv7_unknown_linux_musleabi.rs | 2 ++ .../src/spec/targets/armv7_unknown_linux_musleabihf.rs | 2 ++ .../rustc_target/src/spec/targets/i586_unknown_linux_musl.rs | 2 ++ .../rustc_target/src/spec/targets/i686_unknown_linux_musl.rs | 2 ++ .../rustc_target/src/spec/targets/mips64_openwrt_linux_musl.rs | 1 - .../src/spec/targets/mips64_unknown_linux_muslabi64.rs | 2 ++ .../src/spec/targets/mips64el_unknown_linux_muslabi64.rs | 2 ++ .../rustc_target/src/spec/targets/mips_unknown_linux_musl.rs | 1 - .../rustc_target/src/spec/targets/mipsel_unknown_linux_musl.rs | 1 - .../src/spec/targets/powerpc64_unknown_linux_musl.rs | 2 ++ .../src/spec/targets/powerpc64le_unknown_linux_musl.rs | 2 ++ .../src/spec/targets/powerpc_unknown_linux_musl.rs | 2 ++ .../src/spec/targets/powerpc_unknown_linux_muslspe.rs | 2 ++ .../src/spec/targets/riscv32gc_unknown_linux_musl.rs | 2 ++ .../src/spec/targets/riscv64gc_unknown_linux_musl.rs | 1 - .../rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs | 2 ++ .../src/spec/targets/thumbv7neon_unknown_linux_musleabihf.rs | 2 ++ .../rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs | 2 ++ 23 files changed, 37 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_target/src/spec/base/linux_musl.rs b/compiler/rustc_target/src/spec/base/linux_musl.rs index e020bb8523804..1a854fe362d50 100644 --- a/compiler/rustc_target/src/spec/base/linux_musl.rs +++ b/compiler/rustc_target/src/spec/base/linux_musl.rs @@ -8,8 +8,5 @@ pub(crate) fn opts() -> TargetOptions { base.post_link_objects_self_contained = crt_objects::post_musl_self_contained(); base.link_self_contained = LinkSelfContainedDefault::InferredForMusl; - // These targets statically link libc by default - base.crt_static_default = true; - base } diff --git a/compiler/rustc_target/src/spec/targets/aarch64_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/aarch64_unknown_linux_musl.rs index bb65048a56d66..4fefdfa5c5e15 100644 --- a/compiler/rustc_target/src/spec/targets/aarch64_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/aarch64_unknown_linux_musl.rs @@ -12,6 +12,9 @@ pub(crate) fn target() -> Target { | SanitizerSet::MEMORY | SanitizerSet::THREAD; + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + base.crt_static_default = true; + Target { llvm_target: "aarch64-unknown-linux-musl".into(), metadata: crate::spec::TargetMetadata { diff --git a/compiler/rustc_target/src/spec/targets/arm_unknown_linux_musleabi.rs b/compiler/rustc_target/src/spec/targets/arm_unknown_linux_musleabi.rs index b96d8455a5b19..416bb5432fd1c 100644 --- a/compiler/rustc_target/src/spec/targets/arm_unknown_linux_musleabi.rs +++ b/compiler/rustc_target/src/spec/targets/arm_unknown_linux_musleabi.rs @@ -22,6 +22,8 @@ pub(crate) fn target() -> Target { features: "+strict-align,+v6".into(), max_atomic_width: Some(64), mcount: "\u{1}mcount".into(), + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + crt_static_default: true, ..base::linux_musl::opts() }, } diff --git a/compiler/rustc_target/src/spec/targets/arm_unknown_linux_musleabihf.rs b/compiler/rustc_target/src/spec/targets/arm_unknown_linux_musleabihf.rs index 3418a7090d3a5..909eb78f6983d 100644 --- a/compiler/rustc_target/src/spec/targets/arm_unknown_linux_musleabihf.rs +++ b/compiler/rustc_target/src/spec/targets/arm_unknown_linux_musleabihf.rs @@ -22,6 +22,8 @@ pub(crate) fn target() -> Target { features: "+strict-align,+v6,+vfp2,-d32".into(), max_atomic_width: Some(64), mcount: "\u{1}mcount".into(), + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + crt_static_default: true, ..base::linux_musl::opts() }, } diff --git a/compiler/rustc_target/src/spec/targets/armv5te_unknown_linux_musleabi.rs b/compiler/rustc_target/src/spec/targets/armv5te_unknown_linux_musleabi.rs index 1bcd090b9f2a5..5e3ad42e5a41e 100644 --- a/compiler/rustc_target/src/spec/targets/armv5te_unknown_linux_musleabi.rs +++ b/compiler/rustc_target/src/spec/targets/armv5te_unknown_linux_musleabi.rs @@ -23,6 +23,8 @@ pub(crate) fn target() -> Target { max_atomic_width: Some(32), mcount: "\u{1}mcount".into(), has_thumb_interworking: true, + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + crt_static_default: true, ..base::linux_musl::opts() }, } diff --git a/compiler/rustc_target/src/spec/targets/armv7_unknown_linux_musleabi.rs b/compiler/rustc_target/src/spec/targets/armv7_unknown_linux_musleabi.rs index 169234973259c..843adcfc71116 100644 --- a/compiler/rustc_target/src/spec/targets/armv7_unknown_linux_musleabi.rs +++ b/compiler/rustc_target/src/spec/targets/armv7_unknown_linux_musleabi.rs @@ -26,6 +26,8 @@ pub(crate) fn target() -> Target { features: "+v7,+thumb2,+soft-float,-neon".into(), max_atomic_width: Some(64), mcount: "\u{1}mcount".into(), + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + crt_static_default: true, ..base::linux_musl::opts() }, } diff --git a/compiler/rustc_target/src/spec/targets/armv7_unknown_linux_musleabihf.rs b/compiler/rustc_target/src/spec/targets/armv7_unknown_linux_musleabihf.rs index 5adfa0bc2f864..e0630817bc3ae 100644 --- a/compiler/rustc_target/src/spec/targets/armv7_unknown_linux_musleabihf.rs +++ b/compiler/rustc_target/src/spec/targets/armv7_unknown_linux_musleabihf.rs @@ -25,6 +25,8 @@ pub(crate) fn target() -> Target { features: "+v7,+vfp3,-d32,+thumb2,-neon".into(), max_atomic_width: Some(64), mcount: "\u{1}mcount".into(), + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + crt_static_default: true, ..base::linux_musl::opts() }, } diff --git a/compiler/rustc_target/src/spec/targets/i586_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/i586_unknown_linux_musl.rs index 623422a89ea62..8ad93496f3a8c 100644 --- a/compiler/rustc_target/src/spec/targets/i586_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/i586_unknown_linux_musl.rs @@ -4,5 +4,7 @@ pub(crate) fn target() -> Target { let mut base = super::i686_unknown_linux_musl::target(); base.cpu = "pentium".into(); base.llvm_target = "i586-unknown-linux-musl".into(); + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + base.crt_static_default = true; base } diff --git a/compiler/rustc_target/src/spec/targets/i686_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/i686_unknown_linux_musl.rs index b805b80b85b42..6ba87c732b72d 100644 --- a/compiler/rustc_target/src/spec/targets/i686_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/i686_unknown_linux_musl.rs @@ -6,6 +6,8 @@ pub(crate) fn target() -> Target { base.max_atomic_width = Some(64); base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m32", "-Wl,-melf_i386"]); base.stack_probes = StackProbeType::Inline; + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + base.crt_static_default = true; // The unwinder used by i686-unknown-linux-musl, the LLVM libunwind // implementation, apparently relies on frame pointers existing... somehow. diff --git a/compiler/rustc_target/src/spec/targets/mips64_openwrt_linux_musl.rs b/compiler/rustc_target/src/spec/targets/mips64_openwrt_linux_musl.rs index b7415bf683de2..1a64c11f4ca63 100644 --- a/compiler/rustc_target/src/spec/targets/mips64_openwrt_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/mips64_openwrt_linux_musl.rs @@ -8,7 +8,6 @@ pub(crate) fn target() -> Target { base.cpu = "mips64r2".into(); base.features = "+mips64r2,+soft-float".into(); base.max_atomic_width = Some(64); - base.crt_static_default = false; Target { // LLVM doesn't recognize "muslabi64" yet. diff --git a/compiler/rustc_target/src/spec/targets/mips64_unknown_linux_muslabi64.rs b/compiler/rustc_target/src/spec/targets/mips64_unknown_linux_muslabi64.rs index 69af2da11001b..32f5c79d653d2 100644 --- a/compiler/rustc_target/src/spec/targets/mips64_unknown_linux_muslabi64.rs +++ b/compiler/rustc_target/src/spec/targets/mips64_unknown_linux_muslabi64.rs @@ -22,6 +22,8 @@ pub(crate) fn target() -> Target { abi: "abi64".into(), endian: Endian::Big, mcount: "_mcount".into(), + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + crt_static_default: true, ..base }, } diff --git a/compiler/rustc_target/src/spec/targets/mips64el_unknown_linux_muslabi64.rs b/compiler/rustc_target/src/spec/targets/mips64el_unknown_linux_muslabi64.rs index 4f50e8b7033aa..5e7c37fd46c32 100644 --- a/compiler/rustc_target/src/spec/targets/mips64el_unknown_linux_muslabi64.rs +++ b/compiler/rustc_target/src/spec/targets/mips64el_unknown_linux_muslabi64.rs @@ -5,6 +5,8 @@ pub(crate) fn target() -> Target { base.cpu = "mips64r2".into(); base.features = "+mips64r2".into(); base.max_atomic_width = Some(64); + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + base.crt_static_default = true; Target { // LLVM doesn't recognize "muslabi64" yet. llvm_target: "mips64el-unknown-linux-musl".into(), diff --git a/compiler/rustc_target/src/spec/targets/mips_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/mips_unknown_linux_musl.rs index b283b3b1ef79d..5076ae345a97e 100644 --- a/compiler/rustc_target/src/spec/targets/mips_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/mips_unknown_linux_musl.rs @@ -6,7 +6,6 @@ pub(crate) fn target() -> Target { base.cpu = "mips32r2".into(); base.features = "+mips32r2,+soft-float".into(); base.max_atomic_width = Some(32); - base.crt_static_default = false; Target { llvm_target: "mips-unknown-linux-musl".into(), metadata: crate::spec::TargetMetadata { diff --git a/compiler/rustc_target/src/spec/targets/mipsel_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/mipsel_unknown_linux_musl.rs index 744396aa1babd..339b32b63399b 100644 --- a/compiler/rustc_target/src/spec/targets/mipsel_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/mipsel_unknown_linux_musl.rs @@ -5,7 +5,6 @@ pub(crate) fn target() -> Target { base.cpu = "mips32r2".into(); base.features = "+mips32r2,+soft-float".into(); base.max_atomic_width = Some(32); - base.crt_static_default = false; Target { llvm_target: "mipsel-unknown-linux-musl".into(), metadata: crate::spec::TargetMetadata { diff --git a/compiler/rustc_target/src/spec/targets/powerpc64_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/powerpc64_unknown_linux_musl.rs index 5da891cc13f6d..b2de77c9006aa 100644 --- a/compiler/rustc_target/src/spec/targets/powerpc64_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/powerpc64_unknown_linux_musl.rs @@ -7,6 +7,8 @@ pub(crate) fn target() -> Target { base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m64"]); base.max_atomic_width = Some(64); base.stack_probes = StackProbeType::Inline; + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + base.crt_static_default = true; Target { llvm_target: "powerpc64-unknown-linux-musl".into(), diff --git a/compiler/rustc_target/src/spec/targets/powerpc64le_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/powerpc64le_unknown_linux_musl.rs index e2921aa17fe64..c5782b3c88dbd 100644 --- a/compiler/rustc_target/src/spec/targets/powerpc64le_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/powerpc64le_unknown_linux_musl.rs @@ -6,6 +6,8 @@ pub(crate) fn target() -> Target { base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m64"]); base.max_atomic_width = Some(64); base.stack_probes = StackProbeType::Inline; + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + base.crt_static_default = true; Target { llvm_target: "powerpc64le-unknown-linux-musl".into(), diff --git a/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_musl.rs index 9859e8ced4f46..341903e5a9489 100644 --- a/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_musl.rs @@ -6,6 +6,8 @@ pub(crate) fn target() -> Target { base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m32"]); base.max_atomic_width = Some(32); base.stack_probes = StackProbeType::Inline; + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + base.crt_static_default = true; Target { llvm_target: "powerpc-unknown-linux-musl".into(), diff --git a/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_muslspe.rs b/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_muslspe.rs index 2305db81c5edf..b86c3c2e8e042 100644 --- a/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_muslspe.rs +++ b/compiler/rustc_target/src/spec/targets/powerpc_unknown_linux_muslspe.rs @@ -6,6 +6,8 @@ pub(crate) fn target() -> Target { base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-mspe"]); base.max_atomic_width = Some(32); base.stack_probes = StackProbeType::Inline; + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + base.crt_static_default = true; Target { llvm_target: "powerpc-unknown-linux-muslspe".into(), diff --git a/compiler/rustc_target/src/spec/targets/riscv32gc_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/riscv32gc_unknown_linux_musl.rs index 212de791e499a..f9d49cbaba179 100644 --- a/compiler/rustc_target/src/spec/targets/riscv32gc_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/riscv32gc_unknown_linux_musl.rs @@ -23,6 +23,8 @@ pub(crate) fn target() -> Target { llvm_abiname: "ilp32d".into(), max_atomic_width: Some(32), supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Off]), + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + crt_static_default: true, ..base::linux_musl::opts() }, } diff --git a/compiler/rustc_target/src/spec/targets/riscv64gc_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/riscv64gc_unknown_linux_musl.rs index 2e6fce91d4c75..e012bc196da5b 100644 --- a/compiler/rustc_target/src/spec/targets/riscv64gc_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/riscv64gc_unknown_linux_musl.rs @@ -21,7 +21,6 @@ pub(crate) fn target() -> Target { llvm_abiname: "lp64d".into(), max_atomic_width: Some(64), supported_split_debuginfo: Cow::Borrowed(&[SplitDebuginfo::Off]), - crt_static_default: false, ..base::linux_musl::opts() }, } diff --git a/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs index 65b5c1167bdd8..5d211e4037481 100644 --- a/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/s390x_unknown_linux_musl.rs @@ -15,6 +15,8 @@ pub(crate) fn target() -> Target { base.stack_probes = StackProbeType::Inline; base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::LEAK | SanitizerSet::MEMORY | SanitizerSet::THREAD; + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + base.crt_static_default = true; Target { llvm_target: "s390x-unknown-linux-musl".into(), diff --git a/compiler/rustc_target/src/spec/targets/thumbv7neon_unknown_linux_musleabihf.rs b/compiler/rustc_target/src/spec/targets/thumbv7neon_unknown_linux_musleabihf.rs index 5c43ca69bd852..5d262aaace9b6 100644 --- a/compiler/rustc_target/src/spec/targets/thumbv7neon_unknown_linux_musleabihf.rs +++ b/compiler/rustc_target/src/spec/targets/thumbv7neon_unknown_linux_musleabihf.rs @@ -29,6 +29,8 @@ pub(crate) fn target() -> Target { features: "+v7,+thumb-mode,+thumb2,+vfp3,+neon".into(), max_atomic_width: Some(64), mcount: "\u{1}mcount".into(), + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + crt_static_default: true, ..base::linux_musl::opts() }, } diff --git a/compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs b/compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs index 8be0f335db9a4..8dcdc5be8a958 100644 --- a/compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs +++ b/compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_musl.rs @@ -14,6 +14,8 @@ pub(crate) fn target() -> Target { | SanitizerSet::MEMORY | SanitizerSet::THREAD; base.supports_xray = true; + // FIXME(compiler-team#422): musl targets should be dynamically linked by default. + base.crt_static_default = true; Target { llvm_target: "x86_64-unknown-linux-musl".into(), From 3f3ee2db1a3162c86f18b5a6273c573f429580e1 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Tue, 10 Dec 2024 20:12:58 -0600 Subject: [PATCH 05/11] Add test for musl dynamically linking --- tests/run-make/musl-default-linking/rmake.rs | 62 ++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/run-make/musl-default-linking/rmake.rs diff --git a/tests/run-make/musl-default-linking/rmake.rs b/tests/run-make/musl-default-linking/rmake.rs new file mode 100644 index 0000000000000..b6d428d3f2716 --- /dev/null +++ b/tests/run-make/musl-default-linking/rmake.rs @@ -0,0 +1,62 @@ +use run_make_support::{rustc, serde_json}; + +// Please do NOT add more targets to this list! +// Per /~https://github.com/rust-lang/compiler-team/issues/422, +// we should be trying to move these targets to dynamically link +// musl libc by default. +static LEGACY_STATIC_LINKING_TARGETS: &[&'static str] = &[ + "aarch64-unknown-linux-musl", + "arm-unknown-linux-musleabi", + "arm-unknown-linux-musleabihf", + "armv5te-unknown-linux-musleabi", + "armv7-unknown-linux-musleabi", + "armv7-unknown-linux-musleabihf", + "i586-unknown-linux-musl", + "i686-unknown-linux-musl", + "mips64-unknown-linux-musl", + "mips64-unknown-linux-muslabi64", + "mips64el-unknown-linux-muslabi64", + "powerpc-unknown-linux-musl", + "powerpc-unknown-linux-muslspe", + "powerpc64-unknown-linux-musl", + "powerpc64le-unknown-linux-musl", + "riscv32gc-unknown-linux-musl", + "s390x-unknown-linux-musl", + "thumbv7neon-unknown-linux-musleabihf", + "x86_64-unknown-linux-musl", +]; + +fn main() { + let targets = rustc().print("target-list").run().stdout_utf8(); + + for target in targets.lines() { + let abi = target.split('-').last().unwrap(); + + if !abi.starts_with("musl") { + continue; + } + + let target_spec_json = rustc() + .print("target-spec-json") + .target(target) + .arg("-Zunstable-options") + .run() + .stdout_utf8(); + + let target_spec: serde_json::Value = + serde_json::from_str(&target_spec_json).expect("failed to parse target-spec-json"); + let default = &target_spec["crt-static-default"]; + + // If the value is `null`, then the default to dynamically link from + // musl_base was not overriden. + if default.is_null() { + continue; + } + + if default.as_bool().expect("wasn't a boolean") + && !LEGACY_STATIC_LINKING_TARGETS.contains(&target) + { + panic!("{target} statically links musl libc when it should dynamically link it"); + } + } +} From eb10db0a76fdacdbe6747431aea90f17ce4ec7c1 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Wed, 11 Dec 2024 16:40:54 -0800 Subject: [PATCH 06/11] Make some types and methods related to Polonius + Miri public. --- compiler/rustc_borrowck/src/borrow_set.rs | 24 +++++++++---------- compiler/rustc_borrowck/src/consumers.rs | 4 ++-- .../rustc_const_eval/src/interpret/operand.rs | 24 +++++++++++++------ .../rustc_const_eval/src/interpret/stack.rs | 4 +++- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_borrowck/src/borrow_set.rs b/compiler/rustc_borrowck/src/borrow_set.rs index 16b3d901956cc..d66f613c71e0b 100644 --- a/compiler/rustc_borrowck/src/borrow_set.rs +++ b/compiler/rustc_borrowck/src/borrow_set.rs @@ -20,18 +20,18 @@ pub struct BorrowSet<'tcx> { /// by the `Location` of the assignment statement in which it /// appears on the right hand side. Thus the location is the map /// key, and its position in the map corresponds to `BorrowIndex`. - pub(crate) location_map: FxIndexMap>, + pub location_map: FxIndexMap>, /// Locations which activate borrows. /// NOTE: a given location may activate more than one borrow in the future /// when more general two-phase borrow support is introduced, but for now we /// only need to store one borrow index. - pub(crate) activation_map: FxIndexMap>, + pub activation_map: FxIndexMap>, /// Map from local to all the borrows on that local. - pub(crate) local_map: FxIndexMap>, + pub local_map: FxIndexMap>, - pub(crate) locals_state_at_exit: LocalsStateAtExit, + pub locals_state_at_exit: LocalsStateAtExit, } impl<'tcx> Index for BorrowSet<'tcx> { @@ -45,7 +45,7 @@ impl<'tcx> Index for BorrowSet<'tcx> { /// Location where a two-phase borrow is activated, if a borrow /// is in fact a two-phase borrow. #[derive(Copy, Clone, PartialEq, Eq, Debug)] -pub(crate) enum TwoPhaseActivation { +pub enum TwoPhaseActivation { NotTwoPhase, NotActivated, ActivatedAt(Location), @@ -55,17 +55,17 @@ pub(crate) enum TwoPhaseActivation { pub struct BorrowData<'tcx> { /// Location where the borrow reservation starts. /// In many cases, this will be equal to the activation location but not always. - pub(crate) reserve_location: Location, + pub reserve_location: Location, /// Location where the borrow is activated. - pub(crate) activation_location: TwoPhaseActivation, + pub activation_location: TwoPhaseActivation, /// What kind of borrow this is - pub(crate) kind: mir::BorrowKind, + pub kind: mir::BorrowKind, /// The region for which this borrow is live - pub(crate) region: RegionVid, + pub region: RegionVid, /// Place from which we are borrowing - pub(crate) borrowed_place: mir::Place<'tcx>, + pub borrowed_place: mir::Place<'tcx>, /// Place to which the borrow was stored - pub(crate) assigned_place: mir::Place<'tcx>, + pub assigned_place: mir::Place<'tcx>, } impl<'tcx> fmt::Display for BorrowData<'tcx> { @@ -120,7 +120,7 @@ impl LocalsStateAtExit { } impl<'tcx> BorrowSet<'tcx> { - pub(crate) fn build( + pub fn build( tcx: TyCtxt<'tcx>, body: &Body<'tcx>, locals_are_invalidated_at_exit: bool, diff --git a/compiler/rustc_borrowck/src/consumers.rs b/compiler/rustc_borrowck/src/consumers.rs index 7ace38c3e85ff..74de766ba2317 100644 --- a/compiler/rustc_borrowck/src/consumers.rs +++ b/compiler/rustc_borrowck/src/consumers.rs @@ -5,15 +5,15 @@ use rustc_index::{IndexSlice, IndexVec}; use rustc_middle::mir::{Body, Promoted}; use rustc_middle::ty::TyCtxt; +pub use super::borrow_set::{BorrowData, BorrowSet, TwoPhaseActivation}; pub use super::constraints::OutlivesConstraint; pub use super::dataflow::{BorrowIndex, Borrows, calculate_borrows_out_of_scope_at_location}; -pub use super::facts::{AllFacts as PoloniusInput, RustcFacts}; +pub use super::facts::{AllFacts as PoloniusInput, PoloniusRegionVid, RustcFacts}; pub use super::location::{LocationTable, RichLocation}; pub use super::nll::PoloniusOutput; pub use super::place_ext::PlaceExt; pub use super::places_conflict::{PlaceConflictBias, places_conflict}; pub use super::region_infer::RegionInferenceContext; -use crate::borrow_set::BorrowSet; /// Options determining the output behavior of [`get_body_with_borrowck_facts`]. /// diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index 0157e6c2125e5..bd33e8d20c09b 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -15,9 +15,9 @@ use rustc_middle::{bug, mir, span_bug, ty}; use tracing::trace; use super::{ - CtfeProvenance, InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, OffsetMode, - PlaceTy, Pointer, Projectable, Provenance, Scalar, alloc_range, err_ub, from_known_layout, - interp_ok, mir_assign_valid_types, throw_ub, + CtfeProvenance, Frame, InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, + OffsetMode, PlaceTy, Pointer, Projectable, Provenance, Scalar, alloc_range, err_ub, + from_known_layout, interp_ok, mir_assign_valid_types, throw_ub, }; /// An `Immediate` represents a single immediate self-contained Rust value. @@ -706,17 +706,27 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { interp_ok(str) } - /// Read from a local of the current frame. + /// Read from a local of the current frame. Convenience method for [`InterpCx::local_at_frame_to_op`]. + pub fn local_to_op( + &self, + local: mir::Local, + layout: Option>, + ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> { + self.local_at_frame_to_op(self.frame(), local, layout) + } + + /// Read from a local of a given frame. /// Will not access memory, instead an indirect `Operand` is returned. /// - /// This is public because it is used by [priroda](/~https://github.com/oli-obk/priroda) to get an + /// This is public because it is used by [priroda](/~https://github.com/oli-obk/priroda) and + /// [Aquascope](/~https://github.com/cognitive-engineering-lab/aquascope/) to get an /// OpTy from a local. - pub fn local_to_op( + pub fn local_at_frame_to_op( &self, + frame: &Frame<'tcx, M::Provenance, M::FrameExtra>, local: mir::Local, layout: Option>, ) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> { - let frame = self.frame(); let layout = self.layout_of_local(frame, local, layout)?; let op = *frame.locals[local].access()?; if matches!(op, Operand::Immediate(_)) { diff --git a/compiler/rustc_const_eval/src/interpret/stack.rs b/compiler/rustc_const_eval/src/interpret/stack.rs index a9ebf38661703..6512675530a45 100644 --- a/compiler/rustc_const_eval/src/interpret/stack.rs +++ b/compiler/rustc_const_eval/src/interpret/stack.rs @@ -584,8 +584,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { interp_ok(()) } + /// This is public because it is used by [Aquascope](/~https://github.com/cognitive-engineering-lab/aquascope/) + /// to analyze all the locals in a stack frame. #[inline(always)] - pub(super) fn layout_of_local( + pub fn layout_of_local( &self, frame: &Frame<'tcx, M::Provenance, M::FrameExtra>, local: mir::Local, From 6b93fac9ff967aa6bb6119307e13b07fe5750888 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 12 Dec 2024 11:00:02 -0800 Subject: [PATCH 07/11] Update wasi-sdk used to build WASI targets Bump to the latest wasi-sdk-25 release which brings in various wasi-libc updates as well as LLVM 19 as the version used to compile wasi-libc. --- src/ci/docker/host-x86_64/dist-various-2/Dockerfile | 4 ++-- src/ci/docker/host-x86_64/test-various/Dockerfile | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ci/docker/host-x86_64/dist-various-2/Dockerfile b/src/ci/docker/host-x86_64/dist-various-2/Dockerfile index c40de76abbfee..03ec77f507e75 100644 --- a/src/ci/docker/host-x86_64/dist-various-2/Dockerfile +++ b/src/ci/docker/host-x86_64/dist-various-2/Dockerfile @@ -90,9 +90,9 @@ RUN /tmp/build-solaris-toolchain.sh sparcv9 sparcv9 solaris-sparc sun COPY host-x86_64/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh /tmp/ RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh -RUN curl -L /~https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-23/wasi-sdk-23.0-x86_64-linux.tar.gz | \ +RUN curl -L /~https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/wasi-sdk-25.0-x86_64-linux.tar.gz | \ tar -xz -ENV WASI_SDK_PATH=/tmp/wasi-sdk-23.0-x86_64-linux +ENV WASI_SDK_PATH=/tmp/wasi-sdk-25.0-x86_64-linux COPY scripts/freebsd-toolchain.sh /tmp/ RUN /tmp/freebsd-toolchain.sh i686 diff --git a/src/ci/docker/host-x86_64/test-various/Dockerfile b/src/ci/docker/host-x86_64/test-various/Dockerfile index c2f5a87b1234f..8d2e45ae497ef 100644 --- a/src/ci/docker/host-x86_64/test-various/Dockerfile +++ b/src/ci/docker/host-x86_64/test-various/Dockerfile @@ -40,9 +40,9 @@ WORKDIR / COPY scripts/sccache.sh /scripts/ RUN sh /scripts/sccache.sh -RUN curl -L /~https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-23/wasi-sdk-23.0-x86_64-linux.tar.gz | \ +RUN curl -L /~https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-25/wasi-sdk-25.0-x86_64-linux.tar.gz | \ tar -xz -ENV WASI_SDK_PATH=/wasi-sdk-23.0-x86_64-linux +ENV WASI_SDK_PATH=/wasi-sdk-25.0-x86_64-linux ENV RUST_CONFIGURE_ARGS \ --musl-root-x86_64=/usr/local/x86_64-linux-musl \ From 4d5d4700f3c7fa5151560d27f389c15a7a3047a0 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Thu, 12 Dec 2024 12:34:43 -0800 Subject: [PATCH 08/11] Make BorrowSet/BorrowData fields accessible via public getters --- compiler/rustc_borrowck/src/borrow_set.rs | 66 ++++++++++++++++--- .../rustc_const_eval/src/interpret/machine.rs | 8 ++- .../rustc_const_eval/src/interpret/operand.rs | 7 +- src/tools/miri/src/machine.rs | 8 ++- 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/compiler/rustc_borrowck/src/borrow_set.rs b/compiler/rustc_borrowck/src/borrow_set.rs index d66f613c71e0b..ff838fbbb8868 100644 --- a/compiler/rustc_borrowck/src/borrow_set.rs +++ b/compiler/rustc_borrowck/src/borrow_set.rs @@ -20,18 +20,37 @@ pub struct BorrowSet<'tcx> { /// by the `Location` of the assignment statement in which it /// appears on the right hand side. Thus the location is the map /// key, and its position in the map corresponds to `BorrowIndex`. - pub location_map: FxIndexMap>, + pub(crate) location_map: FxIndexMap>, /// Locations which activate borrows. /// NOTE: a given location may activate more than one borrow in the future /// when more general two-phase borrow support is introduced, but for now we /// only need to store one borrow index. - pub activation_map: FxIndexMap>, + pub(crate) activation_map: FxIndexMap>, /// Map from local to all the borrows on that local. - pub local_map: FxIndexMap>, + pub(crate) local_map: FxIndexMap>, - pub locals_state_at_exit: LocalsStateAtExit, + pub(crate) locals_state_at_exit: LocalsStateAtExit, +} + +// These methods are public to support borrowck consumers. +impl<'tcx> BorrowSet<'tcx> { + pub fn location_map(&self) -> &FxIndexMap> { + &self.location_map + } + + pub fn activation_map(&self) -> &FxIndexMap> { + &self.activation_map + } + + pub fn local_map(&self) -> &FxIndexMap> { + &self.local_map + } + + pub fn locals_state_at_exit(&self) -> &LocalsStateAtExit { + &self.locals_state_at_exit + } } impl<'tcx> Index for BorrowSet<'tcx> { @@ -55,17 +74,44 @@ pub enum TwoPhaseActivation { pub struct BorrowData<'tcx> { /// Location where the borrow reservation starts. /// In many cases, this will be equal to the activation location but not always. - pub reserve_location: Location, + pub(crate) reserve_location: Location, /// Location where the borrow is activated. - pub activation_location: TwoPhaseActivation, + pub(crate) activation_location: TwoPhaseActivation, /// What kind of borrow this is - pub kind: mir::BorrowKind, + pub(crate) kind: mir::BorrowKind, /// The region for which this borrow is live - pub region: RegionVid, + pub(crate) region: RegionVid, /// Place from which we are borrowing - pub borrowed_place: mir::Place<'tcx>, + pub(crate) borrowed_place: mir::Place<'tcx>, /// Place to which the borrow was stored - pub assigned_place: mir::Place<'tcx>, + pub(crate) assigned_place: mir::Place<'tcx>, +} + +// These methods are public to support borrowck consumers. +impl<'tcx> BorrowData<'tcx> { + pub fn reserve_location(&self) -> Location { + self.reserve_location + } + + pub fn activation_location(&self) -> TwoPhaseActivation { + self.activation_location + } + + pub fn kind(&self) -> mir::BorrowKind { + self.kind + } + + pub fn region(&self) -> RegionVid { + self.region + } + + pub fn borrowed_place(&self) -> mir::Place<'tcx> { + self.borrowed_place + } + + pub fn assigned_place(&self) -> mir::Place<'tcx> { + self.assigned_place + } } impl<'tcx> fmt::Display for BorrowData<'tcx> { diff --git a/compiler/rustc_const_eval/src/interpret/machine.rs b/compiler/rustc_const_eval/src/interpret/machine.rs index a180d5da9418b..9ac2a024ccf37 100644 --- a/compiler/rustc_const_eval/src/interpret/machine.rs +++ b/compiler/rustc_const_eval/src/interpret/machine.rs @@ -540,10 +540,14 @@ pub trait Machine<'tcx>: Sized { interp_ok(ReturnAction::Normal) } - /// Called immediately after an "immediate" local variable is read + /// Called immediately after an "immediate" local variable is read in a given frame /// (i.e., this is called for reads that do not end up accessing addressable memory). #[inline(always)] - fn after_local_read(_ecx: &InterpCx<'tcx, Self>, _local: mir::Local) -> InterpResult<'tcx> { + fn after_local_read( + _ecx: &InterpCx<'tcx, Self>, + _frame: &Frame<'tcx, Self::Provenance, Self::FrameExtra>, + _local: mir::Local, + ) -> InterpResult<'tcx> { interp_ok(()) } diff --git a/compiler/rustc_const_eval/src/interpret/operand.rs b/compiler/rustc_const_eval/src/interpret/operand.rs index bd33e8d20c09b..9d2a7125f5105 100644 --- a/compiler/rustc_const_eval/src/interpret/operand.rs +++ b/compiler/rustc_const_eval/src/interpret/operand.rs @@ -718,9 +718,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { /// Read from a local of a given frame. /// Will not access memory, instead an indirect `Operand` is returned. /// - /// This is public because it is used by [priroda](/~https://github.com/oli-obk/priroda) and - /// [Aquascope](/~https://github.com/cognitive-engineering-lab/aquascope/) to get an - /// OpTy from a local. + /// This is public because it is used by [Aquascope](/~https://github.com/cognitive-engineering-lab/aquascope/) + /// to get an OpTy from a local. pub fn local_at_frame_to_op( &self, frame: &Frame<'tcx, M::Provenance, M::FrameExtra>, @@ -732,7 +731,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { if matches!(op, Operand::Immediate(_)) { assert!(!layout.is_unsized()); } - M::after_local_read(self, local)?; + M::after_local_read(self, frame, local)?; interp_ok(OpTy { op, layout }) } diff --git a/src/tools/miri/src/machine.rs b/src/tools/miri/src/machine.rs index 7cc22f83a24a6..ac26feb345c18 100644 --- a/src/tools/miri/src/machine.rs +++ b/src/tools/miri/src/machine.rs @@ -1571,8 +1571,12 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> { res } - fn after_local_read(ecx: &InterpCx<'tcx, Self>, local: mir::Local) -> InterpResult<'tcx> { - if let Some(data_race) = &ecx.frame().extra.data_race { + fn after_local_read( + ecx: &InterpCx<'tcx, Self>, + frame: &Frame<'tcx, Provenance, FrameExtra<'tcx>>, + local: mir::Local, + ) -> InterpResult<'tcx> { + if let Some(data_race) = &frame.extra.data_race { data_race.local_read(local, &ecx.machine); } interp_ok(()) From 91e74edca01dbbbb444117f9e85ed76164390314 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 14 Dec 2024 03:45:29 +0000 Subject: [PATCH 09/11] Encode coroutine-closures in SMIR --- .../rustc_smir/src/rustc_internal/internal.rs | 4 + compiler/rustc_smir/src/rustc_internal/mod.rs | 4 + .../rustc_smir/src/rustc_smir/convert/mir.rs | 7 +- compiler/stable_mir/src/mir/body.rs | 8 +- compiler/stable_mir/src/mir/pretty.rs | 4 + compiler/stable_mir/src/ty.rs | 11 +++ compiler/stable_mir/src/visitor.rs | 1 + tests/ui/stable-mir-print/async-closure.rs | 12 +++ .../ui/stable-mir-print/async-closure.stdout | 90 +++++++++++++++++++ 9 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 tests/ui/stable-mir-print/async-closure.rs create mode 100644 tests/ui/stable-mir-print/async-closure.stdout diff --git a/compiler/rustc_smir/src/rustc_internal/internal.rs b/compiler/rustc_smir/src/rustc_internal/internal.rs index dec2a77619bad..c465367b6b9e0 100644 --- a/compiler/rustc_smir/src/rustc_internal/internal.rs +++ b/compiler/rustc_smir/src/rustc_internal/internal.rs @@ -141,6 +141,10 @@ impl RustcInternal for RigidTy { RigidTy::Coroutine(def, args, _mov) => { rustc_ty::TyKind::Coroutine(def.0.internal(tables, tcx), args.internal(tables, tcx)) } + RigidTy::CoroutineClosure(def, args) => rustc_ty::TyKind::CoroutineClosure( + def.0.internal(tables, tcx), + args.internal(tables, tcx), + ), RigidTy::CoroutineWitness(def, args) => rustc_ty::TyKind::CoroutineWitness( def.0.internal(tables, tcx), args.internal(tables, tcx), diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index 614c9169d660b..64d241067a845 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -107,6 +107,10 @@ impl<'tcx> Tables<'tcx> { stable_mir::ty::CoroutineDef(self.create_def_id(did)) } + pub fn coroutine_closure_def(&mut self, did: DefId) -> stable_mir::ty::CoroutineClosureDef { + stable_mir::ty::CoroutineClosureDef(self.create_def_id(did)) + } + pub fn alias_def(&mut self, did: DefId) -> stable_mir::ty::AliasDef { stable_mir::ty::AliasDef(self.create_def_id(did)) } diff --git a/compiler/rustc_smir/src/rustc_smir/convert/mir.rs b/compiler/rustc_smir/src/rustc_smir/convert/mir.rs index fcdf8703b14da..a5a17b4b5730e 100644 --- a/compiler/rustc_smir/src/rustc_smir/convert/mir.rs +++ b/compiler/rustc_smir/src/rustc_smir/convert/mir.rs @@ -565,8 +565,11 @@ impl<'tcx> Stable<'tcx> for mir::AggregateKind<'tcx> { tables.tcx.coroutine_movability(*def_id).stable(tables), ) } - mir::AggregateKind::CoroutineClosure(..) => { - todo!("FIXME(async_closures): Lower these to SMIR") + mir::AggregateKind::CoroutineClosure(def_id, generic_args) => { + stable_mir::mir::AggregateKind::CoroutineClosure( + tables.coroutine_closure_def(*def_id), + generic_args.stable(tables), + ) } mir::AggregateKind::RawPtr(ty, mutability) => { stable_mir::mir::AggregateKind::RawPtr(ty.stable(tables), mutability.stable(tables)) diff --git a/compiler/stable_mir/src/mir/body.rs b/compiler/stable_mir/src/mir/body.rs index f96487cc53c9f..dfd090b39563f 100644 --- a/compiler/stable_mir/src/mir/body.rs +++ b/compiler/stable_mir/src/mir/body.rs @@ -5,8 +5,8 @@ use serde::Serialize; use crate::compiler_interface::with; use crate::mir::pretty::function_body; use crate::ty::{ - AdtDef, ClosureDef, CoroutineDef, GenericArgs, MirConst, Movability, Region, RigidTy, Ty, - TyConst, TyKind, VariantIdx, + AdtDef, ClosureDef, CoroutineClosureDef, CoroutineDef, GenericArgs, MirConst, Movability, + Region, RigidTy, Ty, TyConst, TyKind, VariantIdx, }; use crate::{Error, Opaque, Span, Symbol}; @@ -617,6 +617,9 @@ impl Rvalue { AggregateKind::Coroutine(def, ref args, mov) => { Ok(Ty::new_coroutine(def, args.clone(), mov)) } + AggregateKind::CoroutineClosure(def, ref args) => { + Ok(Ty::new_coroutine_closure(def, args.clone())) + } AggregateKind::RawPtr(ty, mutability) => Ok(Ty::new_ptr(ty, mutability)), }, Rvalue::ShallowInitBox(_, ty) => Ok(Ty::new_box(*ty)), @@ -633,6 +636,7 @@ pub enum AggregateKind { Closure(ClosureDef, GenericArgs), // FIXME(stable_mir): Movability here is redundant Coroutine(CoroutineDef, GenericArgs, Movability), + CoroutineClosure(CoroutineClosureDef, GenericArgs), RawPtr(Ty, Mutability), } diff --git a/compiler/stable_mir/src/mir/pretty.rs b/compiler/stable_mir/src/mir/pretty.rs index 01a50d46b2d9c..93ed32e258a8b 100644 --- a/compiler/stable_mir/src/mir/pretty.rs +++ b/compiler/stable_mir/src/mir/pretty.rs @@ -410,6 +410,10 @@ fn pretty_aggregate( write!(writer, "{{coroutine@{}}}(", def.span().diagnostic())?; ")" } + AggregateKind::CoroutineClosure(def, _) => { + write!(writer, "{{coroutine-closure@{}}}(", def.span().diagnostic())?; + ")" + } AggregateKind::RawPtr(ty, mutability) => { write!( writer, diff --git a/compiler/stable_mir/src/ty.rs b/compiler/stable_mir/src/ty.rs index 9ce72f155f95c..d7eb435e13f40 100644 --- a/compiler/stable_mir/src/ty.rs +++ b/compiler/stable_mir/src/ty.rs @@ -63,6 +63,11 @@ impl Ty { Ty::from_rigid_kind(RigidTy::Coroutine(def, args, mov)) } + /// Create a new closure type. + pub fn new_coroutine_closure(def: CoroutineClosureDef, args: GenericArgs) -> Ty { + Ty::from_rigid_kind(RigidTy::CoroutineClosure(def, args)) + } + /// Create a new box type that represents `Box`, for the given inner type `T`. pub fn new_box(inner_ty: Ty) -> Ty { with(|cx| cx.new_box_ty(inner_ty)) @@ -550,6 +555,7 @@ pub enum RigidTy { Closure(ClosureDef, GenericArgs), // FIXME(stable_mir): Movability here is redundant Coroutine(CoroutineDef, GenericArgs, Movability), + CoroutineClosure(CoroutineClosureDef, GenericArgs), Dynamic(Vec>, Region, DynKind), Never, Tuple(Vec), @@ -740,6 +746,11 @@ crate_def! { pub CoroutineDef; } +crate_def! { + #[derive(Serialize)] + pub CoroutineClosureDef; +} + crate_def! { #[derive(Serialize)] pub ParamDef; diff --git a/compiler/stable_mir/src/visitor.rs b/compiler/stable_mir/src/visitor.rs index 48260285408c7..3533ed2e8511d 100644 --- a/compiler/stable_mir/src/visitor.rs +++ b/compiler/stable_mir/src/visitor.rs @@ -168,6 +168,7 @@ impl Visitable for RigidTy { | RigidTy::Closure(_, args) | RigidTy::Coroutine(_, args, _) | RigidTy::CoroutineWitness(_, args) + | RigidTy::CoroutineClosure(_, args) | RigidTy::FnDef(_, args) => args.visit(visitor), RigidTy::FnPtr(sig) => sig.visit(visitor), RigidTy::Dynamic(pred, r, _) => { diff --git a/tests/ui/stable-mir-print/async-closure.rs b/tests/ui/stable-mir-print/async-closure.rs new file mode 100644 index 0000000000000..7da532a359f94 --- /dev/null +++ b/tests/ui/stable-mir-print/async-closure.rs @@ -0,0 +1,12 @@ +//@ compile-flags: -Z unpretty=stable-mir --crate-type lib -C panic=abort +//@ check-pass +//@ only-x86_64 +//@ edition: 2024 +//@ needs-unwind unwind edges are different with panic=abort + +pub fn foo() { + let y = 0; + let x = async || { + let y = y; + }; +} diff --git a/tests/ui/stable-mir-print/async-closure.stdout b/tests/ui/stable-mir-print/async-closure.stdout new file mode 100644 index 0000000000000..21df1fd395403 --- /dev/null +++ b/tests/ui/stable-mir-print/async-closure.stdout @@ -0,0 +1,90 @@ +// WARNING: This is highly experimental output it's intended for stable-mir developers only. +// If you find a bug or want to improve the output open a issue at /~https://github.com/rust-lang/project-stable-mir. +fn foo() -> () { + let mut _0: (); + let _1: i32; + let _2: {async closure@$DIR/async-closure.rs:9:13: 9:21}; + let mut _3: &i32; + debug y => _1; + debug x => _2; + bb0: { + _1 = 0_i32; + _3 = &_1; + _2 = {coroutine-closure@$DIR/async-closure.rs:9:13: 9:21}(move _3); + return; + } +} +fn foo::{closure#0}(_1: &{async closure@$DIR/async-closure.rs:9:13: 9:21}) -> {async closure body@$DIR/async-closure.rs:9:22: 11:6} { + let mut _0: {async closure body@$DIR/async-closure.rs:9:22: 11:6}; + let mut _2: &i32; + debug y => (*((*_1).0: &i32)); + bb0: { + _2 = CopyForDeref(((*_1).0: &i32)); + _0 = {coroutine@$DIR/async-closure.rs:9:22: 11:6}(_2); + return; + } +} +fn foo::{closure#0}::{closure#0}(_1: Pin<&mut {async closure body@$DIR/async-closure.rs:9:22: 11:6}>, _2: &mut Context<'_>) -> Poll<()> { + let mut _0: Poll<()>; + let _3: i32; + let mut _4: &i32; + let mut _5: u32; + let mut _6: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6}; + let mut _7: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6}; + let mut _8: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6}; + debug _task_context => _2; + debug y => (*((*(_1.0: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6})).0: &i32)); + debug y => _3; + bb0: { + _6 = CopyForDeref((_1.0: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6})); + _5 = discriminant((*_6)); + switchInt(move _5) -> [0: bb1, 1: bb2, otherwise: bb3]; + } + bb1: { + _7 = CopyForDeref((_1.0: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6})); + _4 = CopyForDeref(((*_7).0: &i32)); + _3 = (*_4); + _0 = std::task::Poll::Ready(()); + _8 = CopyForDeref((_1.0: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6})); + discriminant((*_8) = 1; + return; + } + bb2: { + assert(false, `async fn` resumed after completion) -> [success: bb2, unwind unreachable]; + } + bb3: { + unreachable; + } +} +fn foo::{closure#0}::{closure#1}(_1: Pin<&mut {async closure body@$DIR/async-closure.rs:9:22: 11:6}>, _2: &mut Context<'_>) -> Poll<()> { + let mut _0: Poll<()>; + let _3: i32; + let mut _4: &i32; + let mut _5: u32; + let mut _6: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6}; + let mut _7: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6}; + let mut _8: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6}; + debug _task_context => _2; + debug y => (*((*(_1.0: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6})).0: &i32)); + debug y => _3; + bb0: { + _6 = CopyForDeref((_1.0: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6})); + _5 = discriminant((*_6)); + switchInt(move _5) -> [0: bb1, 1: bb2, otherwise: bb3]; + } + bb1: { + _7 = CopyForDeref((_1.0: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6})); + _4 = CopyForDeref(((*_7).0: &i32)); + _3 = (*_4); + _0 = std::task::Poll::Ready(()); + _8 = CopyForDeref((_1.0: &mut {async closure body@$DIR/async-closure.rs:9:22: 11:6})); + discriminant((*_8) = 1; + return; + } + bb2: { + assert(false, `async fn` resumed after completion) -> [success: bb2, unwind unreachable]; + } + bb3: { + unreachable; + } +} From 0b0744ae8093eeeb9cf9627320ca0d4649c9ffaf 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?= <39484203+jieyouxu@users.noreply.github.com> Date: Sat, 14 Dec 2024 02:51:15 +0800 Subject: [PATCH 10/11] Add a regression test for #134162 --- tests/crashes/134162.rs | 8 -------- .../ui/typeck/rhs-ty-hint-134162.e2018.stderr | 11 +++++++++++ .../ui/typeck/rhs-ty-hint-134162.e2021.stderr | 11 +++++++++++ .../ui/typeck/rhs-ty-hint-134162.e2024.stderr | 11 +++++++++++ tests/ui/typeck/rhs-ty-hint-134162.rs | 18 ++++++++++++++++++ 5 files changed, 51 insertions(+), 8 deletions(-) delete mode 100644 tests/crashes/134162.rs create mode 100644 tests/ui/typeck/rhs-ty-hint-134162.e2018.stderr create mode 100644 tests/ui/typeck/rhs-ty-hint-134162.e2021.stderr create mode 100644 tests/ui/typeck/rhs-ty-hint-134162.e2024.stderr create mode 100644 tests/ui/typeck/rhs-ty-hint-134162.rs diff --git a/tests/crashes/134162.rs b/tests/crashes/134162.rs deleted file mode 100644 index 9e5a4a1cb0bf3..0000000000000 --- a/tests/crashes/134162.rs +++ /dev/null @@ -1,8 +0,0 @@ -//@ known-bug: #134162 - -fn main() { - struct X; - - let xs = [X, X, X]; - let eq = xs == [panic!("panic evaluated"); 2]; -} diff --git a/tests/ui/typeck/rhs-ty-hint-134162.e2018.stderr b/tests/ui/typeck/rhs-ty-hint-134162.e2018.stderr new file mode 100644 index 0000000000000..ff189e36ba04f --- /dev/null +++ b/tests/ui/typeck/rhs-ty-hint-134162.e2018.stderr @@ -0,0 +1,11 @@ +error[E0369]: binary operation `==` cannot be applied to type `[X; 1]` + --> $DIR/rhs-ty-hint-134162.rs:16:17 + | +LL | let _ = [X] == [panic!(); 2]; + | --- ^^ ------------- [_; 2] + | | + | [X; 1] + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0369`. diff --git a/tests/ui/typeck/rhs-ty-hint-134162.e2021.stderr b/tests/ui/typeck/rhs-ty-hint-134162.e2021.stderr new file mode 100644 index 0000000000000..ff189e36ba04f --- /dev/null +++ b/tests/ui/typeck/rhs-ty-hint-134162.e2021.stderr @@ -0,0 +1,11 @@ +error[E0369]: binary operation `==` cannot be applied to type `[X; 1]` + --> $DIR/rhs-ty-hint-134162.rs:16:17 + | +LL | let _ = [X] == [panic!(); 2]; + | --- ^^ ------------- [_; 2] + | | + | [X; 1] + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0369`. diff --git a/tests/ui/typeck/rhs-ty-hint-134162.e2024.stderr b/tests/ui/typeck/rhs-ty-hint-134162.e2024.stderr new file mode 100644 index 0000000000000..ff189e36ba04f --- /dev/null +++ b/tests/ui/typeck/rhs-ty-hint-134162.e2024.stderr @@ -0,0 +1,11 @@ +error[E0369]: binary operation `==` cannot be applied to type `[X; 1]` + --> $DIR/rhs-ty-hint-134162.rs:16:17 + | +LL | let _ = [X] == [panic!(); 2]; + | --- ^^ ------------- [_; 2] + | | + | [X; 1] + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0369`. diff --git a/tests/ui/typeck/rhs-ty-hint-134162.rs b/tests/ui/typeck/rhs-ty-hint-134162.rs new file mode 100644 index 0000000000000..a14b4bcfd26a5 --- /dev/null +++ b/tests/ui/typeck/rhs-ty-hint-134162.rs @@ -0,0 +1,18 @@ +//! Regression test for . +//! +//! introduced RHS type hints for when a ty doesn't +//! support a bin op. In the suggestion path, there was a `delay_bug`. +//! converted this `delay_bug` to `bug`, which did +//! not trigger any test failures as we did not have test coverage for this particular case. This +//! manifested in an ICE as reported in . + +//@ revisions: e2018 e2021 e2024 +//@[e2018] edition: 2018 +//@[e2021] edition: 2021 +//@[e2024] edition: 2024 + +fn main() { + struct X; + let _ = [X] == [panic!(); 2]; + //[e2018,e2021,e2024]~^ ERROR binary operation `==` cannot be applied to type `[X; 1]` +} From d15315cf9d2fbe3c2d348267567feacaddf89c50 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?= <39484203+jieyouxu@users.noreply.github.com> Date: Sat, 14 Dec 2024 02:52:29 +0800 Subject: [PATCH 11/11] Return adjustment target if adjust kind is never-to-any Without doing so, we'll run into a series of delayed bugs then find that we have a `TyKind::Error` constructed yet fail to emit an error. This partially reverts a change in related to never type adjustments in expr typecheck errors. --- compiler/rustc_hir_typeck/src/expr.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 65345048bfc2e..66978399efb53 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -72,12 +72,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if self.try_structurally_resolve_type(expr.span, ty).is_never() && self.expr_guaranteed_to_constitute_read_for_never(expr) { - if let Some(_) = self.typeck_results.borrow().adjustments().get(expr.hir_id) { + if let Some(adjustments) = self.typeck_results.borrow().adjustments().get(expr.hir_id) { let reported = self.dcx().span_delayed_bug( expr.span, "expression with never type wound up being adjusted", ); - return Ty::new_error(self.tcx(), reported); + + return if let [Adjustment { kind: Adjust::NeverToAny, target }] = &adjustments[..] { + target.to_owned() + } else { + Ty::new_error(self.tcx(), reported) + }; } let adj_ty = self.next_ty_var(expr.span);