Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 6 pull requests #121254

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
d907a6b
rustdoc: Correctly handle long crate names on mobile
GuillaumeGomez Jan 31, 2024
5d29f5a
Add regression test for #120471 to ensure that long crate name are ha…
GuillaumeGomez Jan 31, 2024
d51e703
As Windows 10 requires certain features like CMPXCHG16B and a few oth…
CKingX Feb 9, 2024
d6766e2
Update x86_64_pc_windows_msvc.rs
CKingX Feb 9, 2024
fcb06f7
Update x86_64_pc_windows_msvc.rs
CKingX Feb 9, 2024
abeac8f
Update x86_64_uwp_windows_gnu.rs
CKingX Feb 9, 2024
1c6dda7
Possibly removed merge policy
CKingX Feb 9, 2024
376c7b9
Added sahf feature to windows targets
CKingX Feb 13, 2024
131cc37
Initiate the inner usage of cfg_match (Library)
c410-f3r Feb 16, 2024
9b3fcf9
Detect when method call on argument could be removed to fulfill faile…
estebank Feb 14, 2024
3a917cd
make "invalid fragment specifier" translatable
tshepang Feb 14, 2024
e3859d2
add test to guard against inaccurate diagnostic
tshepang Feb 17, 2024
408eeae
Improve wording of static_mut_ref
obeis Feb 17, 2024
2a438a6
Rollup merge of #116342 - c410-f3r:t3st3ss, r=Mark-Simulacrum
CKingX Feb 18, 2024
12af8a9
Rollup merge of #120526 - GuillaumeGomez:mobile-long-crate-name, r=no…
CKingX Feb 18, 2024
8aea4c3
Rollup merge of #120820 - CKingX:cpu-base-minimum, r=petrochenkov
CKingX Feb 18, 2024
c465cfa
Rollup merge of #121034 - obeis:improve-static-mut-ref, r=RalfJung
CKingX Feb 18, 2024
3aff0b0
Rollup merge of #121067 - tshepang:make-expand-translatable, r=fmease
CKingX Feb 18, 2024
72997ad
Rollup merge of #121100 - estebank:issue-71252, r=compiler-errors
CKingX Feb 18, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5999,7 +5999,6 @@ dependencies = [
name = "unwind"
version = "0.0.0"
dependencies = [
"cfg-if",
"compiler_builtins",
"core",
"libc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ fn start<T: Termination + 'static>(

static mut NUM: u8 = 6 * 7;

// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
#[allow(static_mut_ref)]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[allow(static_mut_refs)]
static NUM_REF: &'static u8 = unsafe { &NUM };

unsafe fn zeroed<T>() -> T {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ fn start<T: Termination + 'static>(

static mut NUM: u8 = 6 * 7;

// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
#[allow(static_mut_ref)]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[allow(static_mut_refs)]
static NUM_REF: &'static u8 = unsafe { &NUM };

macro_rules! assert {
Expand Down
26 changes: 15 additions & 11 deletions compiler/rustc_error_codes/src/error_codes/E0796.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
Reference of mutable static.
You have created a reference to a mutable static.

Erroneous code example:

```compile_fail,edition2024,E0796
static mut X: i32 = 23;
static mut Y: i32 = 24;

unsafe {
let y = &X;
let ref x = X;
let (x, y) = (&X, &Y);
foo(&X);
fn work() {
let _val = unsafe { X };
}

fn foo<'a>(_x: &'a i32) {}
let x_ref = unsafe { &mut X };
work();
// The next line has Undefined Behavior!
// `x_ref` is a mutable reference and allows no aliases,
// but `work` has been reading the reference between
// the moment `x_ref` was created and when it was used.
// This violates the uniqueness of `x_ref`.
*x_ref = 42;
```

Mutable statics can be written to by multiple threads: aliasing violations or
data races will cause undefined behavior.
A reference to a mutable static has lifetime `'static`. This is very dangerous
as it is easy to accidentally overlap the lifetime of that reference with
other, conflicting accesses to the same static.

Reference of mutable static is a hard error from 2024 edition.
References to mutable statics are a hard error in the 2024 edition.
5 changes: 5 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ expand_invalid_cfg_multiple_predicates = multiple `cfg` predicates are specified
expand_invalid_cfg_no_parens = `cfg` is not followed by parentheses
expand_invalid_cfg_no_predicate = `cfg` predicate is not specified
expand_invalid_cfg_predicate_literal = `cfg` predicate key cannot be a literal

expand_invalid_fragment_specifier =
invalid fragment specifier `{$fragment}`
.help = {$help}

expand_macro_body_stability =
macros cannot have body stability attributes
.label = invalid body stability attribute
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,13 @@ pub struct DuplicateMatcherBinding {
#[label(expand_label2)]
pub prev: Span,
}

#[derive(Diagnostic)]
#[diag(expand_invalid_fragment_specifier)]
#[help]
pub struct InvalidFragmentSpecifier {
#[primary_span]
pub span: Span,
pub fragment: Ident,
pub help: String,
}
18 changes: 9 additions & 9 deletions compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::errors;
use crate::mbe::macro_parser::count_metavar_decls;
use crate::mbe::{Delimited, KleeneOp, KleeneToken, MetaVarExpr, SequenceRepetition, TokenTree};

Expand Down Expand Up @@ -60,11 +61,11 @@ pub(super) fn parse(
Some(&tokenstream::TokenTree::Token(Token { kind: token::Colon, span }, _)) => {
match trees.next() {
Some(tokenstream::TokenTree::Token(token, _)) => match token.ident() {
Some((frag, _)) => {
Some((fragment, _)) => {
let span = token.span.with_lo(start_sp.lo());

let kind =
token::NonterminalKind::from_symbol(frag.name, || {
token::NonterminalKind::from_symbol(fragment.name, || {
// FIXME(#85708) - once we properly decode a foreign
// crate's `SyntaxContext::root`, then we can replace
// this with just `span.edition()`. A
Expand All @@ -81,14 +82,13 @@ pub(super) fn parse(
})
.unwrap_or_else(
|| {
let msg = format!(
"invalid fragment specifier `{}`",
frag.name
sess.dcx().emit_err(
errors::InvalidFragmentSpecifier {
span,
fragment,
help: VALID_FRAGMENT_NAMES_MSG.into(),
},
);
sess.dcx()
.struct_span_err(span, msg)
.with_help(VALID_FRAGMENT_NAMES_MSG)
.emit();
token::NonterminalKind::Ident
},
);
Expand Down
31 changes: 18 additions & 13 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,24 @@ hir_analysis_start_not_target_feature = `#[start]` function is not allowed to ha
hir_analysis_start_not_track_caller = `#[start]` function is not allowed to be `#[track_caller]`
.label = `#[start]` function is not allowed to be `#[track_caller]`

hir_analysis_static_mut_ref = reference of mutable static is disallowed
.label = reference of mutable static
.note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
.suggestion = shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
.suggestion_mut = mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer

hir_analysis_static_mut_ref_lint = {$shared}reference of mutable static is discouraged
.label = shared reference of mutable static
.label_mut = mutable reference of mutable static
.suggestion = shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
.suggestion_mut = mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
.note = reference of mutable static is a hard error from 2024 edition
.why_note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
hir_analysis_static_mut_ref = creating a {$shared} reference to a mutable static
.label = {$shared} reference to mutable static
.note = {$shared ->
[shared] this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior
*[mutable] this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
}
.suggestion = use `addr_of!` instead to create a raw pointer
.suggestion_mut = use `addr_of_mut!` instead to create a raw pointer

hir_analysis_static_mut_refs_lint = creating a {$shared} reference to mutable static is discouraged
.label = {$shared} reference to mutable static
.suggestion = use `addr_of!` instead to create a raw pointer
.suggestion_mut = use `addr_of_mut!` instead to create a raw pointer
.note = this will be a hard error in the 2024 edition
.why_note = {$shared ->
[shared] this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior
*[mutable] this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
}

hir_analysis_static_specialize = cannot specialize on `'static` lifetime

Expand Down
28 changes: 10 additions & 18 deletions compiler/rustc_hir_analysis/src/check/errs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_hir as hir;
use rustc_hir_pretty::qpath_to_string;
use rustc_lint_defs::builtin::STATIC_MUT_REF;
use rustc_lint_defs::builtin::STATIC_MUT_REFS;
use rustc_middle::ty::TyCtxt;
use rustc_span::Span;
use rustc_type_ir::Mutability;
Expand Down Expand Up @@ -66,32 +66,24 @@ fn handle_static_mut_ref(
hir_id: hir::HirId,
) {
if e2024 {
let sugg = if mutable {
errors::StaticMutRefSugg::Mut { span, var }
let (sugg, shared) = if mutable {
(errors::StaticMutRefSugg::Mut { span, var }, "mutable")
} else {
errors::StaticMutRefSugg::Shared { span, var }
(errors::StaticMutRefSugg::Shared { span, var }, "shared")
};
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg });
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg, shared });
return;
}

let (label, sugg, shared) = if mutable {
(
errors::RefOfMutStaticLabel::Mut { span },
errors::RefOfMutStaticSugg::Mut { span, var },
"mutable ",
)
let (sugg, shared) = if mutable {
(errors::RefOfMutStaticSugg::Mut { span, var }, "mutable")
} else {
(
errors::RefOfMutStaticLabel::Shared { span },
errors::RefOfMutStaticSugg::Shared { span, var },
"shared ",
)
(errors::RefOfMutStaticSugg::Shared { span, var }, "shared")
};
tcx.emit_node_span_lint(
STATIC_MUT_REF,
STATIC_MUT_REFS,
hir_id,
span,
errors::RefOfMutStatic { shared, why_note: (), label, sugg },
errors::RefOfMutStatic { span, sugg, shared },
);
}
28 changes: 7 additions & 21 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1455,12 +1455,13 @@ pub struct OnlyCurrentTraitsPointerSugg<'a> {
#[derive(Diagnostic)]
#[diag(hir_analysis_static_mut_ref, code = E0796)]
#[note]
pub struct StaticMutRef {
pub struct StaticMutRef<'a> {
#[primary_span]
#[label]
pub span: Span,
#[subdiagnostic]
pub sugg: StaticMutRefSugg,
pub shared: &'a str,
}

#[derive(Subdiagnostic)]
Expand Down Expand Up @@ -1491,30 +1492,15 @@ pub enum StaticMutRefSugg {

// STATIC_MUT_REF lint
#[derive(LintDiagnostic)]
#[diag(hir_analysis_static_mut_ref_lint)]
#[diag(hir_analysis_static_mut_refs_lint)]
#[note]
#[note(hir_analysis_why_note)]
pub struct RefOfMutStatic<'a> {
pub shared: &'a str,
#[note(hir_analysis_why_note)]
pub why_note: (),
#[subdiagnostic]
pub label: RefOfMutStaticLabel,
#[label]
pub span: Span,
#[subdiagnostic]
pub sugg: RefOfMutStaticSugg,
}

#[derive(Subdiagnostic)]
pub enum RefOfMutStaticLabel {
#[label(hir_analysis_label)]
Shared {
#[primary_span]
span: Span,
},
#[label(hir_analysis_label_mut)]
Mut {
#[primary_span]
span: Span,
},
pub shared: &'a str,
}

#[derive(Subdiagnostic)]
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ fn register_builtins(store: &mut LintStore) {
store.register_renamed("or_patterns_back_compat", "rust_2021_incompatible_or_patterns");
store.register_renamed("non_fmt_panic", "non_fmt_panics");
store.register_renamed("unused_tuple_struct_fields", "dead_code");
store.register_renamed("static_mut_ref", "static_mut_refs");

// These were moved to tool lints, but rustc still sees them when compiling normally, before
// tool lints are registered, so `check_tool_name_for_backwards_compat` doesn't work. Use
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ declare_lint_pass! {
SINGLE_USE_LIFETIMES,
SOFT_UNSTABLE,
STABLE_FEATURES,
STATIC_MUT_REF,
STATIC_MUT_REFS,
SUSPICIOUS_AUTO_TRAIT_IMPLS,
TEST_UNSTABLE_LINT,
TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
Expand Down Expand Up @@ -1769,7 +1769,7 @@ declare_lint! {
}

declare_lint! {
/// The `static_mut_ref` lint checks for shared or mutable references
/// The `static_mut_refs` lint checks for shared or mutable references
/// of mutable static inside `unsafe` blocks and `unsafe` functions.
///
/// ### Example
Expand Down Expand Up @@ -1807,9 +1807,9 @@ declare_lint! {
/// Shared or mutable references of mutable static are almost always a mistake and
/// can lead to undefined behavior and various other problems in your code.
///
/// This lint is "warn" by default on editions up to 2021, from 2024 there is
/// This lint is "warn" by default on editions up to 2021, in 2024 there is
/// a hard error instead.
pub STATIC_MUT_REF,
pub STATIC_MUT_REFS,
Warn,
"shared references or mutable references of mutable static is discouraged",
@future_incompatible = FutureIncompatibleInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use crate::spec::{base, Cc, LinkerFlavor, Lld, Target};
pub fn target() -> Target {
let mut base = base::windows_gnu::opts();
base.cpu = "x86-64".into();
base.features = "+cx16,+sse3,+sahf".into();
base.plt_by_default = false;
// Use high-entropy 64 bit address space for ASLR
base.add_pre_link_args(
LinkerFlavor::Gnu(Cc::No, Lld::No),
&["-m", "i386pep", "--high-entropy-va"],
);
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m64", "-Wl,--high-entropy-va"]);
base.max_atomic_width = Some(64);
base.max_atomic_width = Some(128);
base.linker = Some("x86_64-w64-mingw32-gcc".into());

Target {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use crate::spec::{base, Cc, LinkerFlavor, Lld, Target};
pub fn target() -> Target {
let mut base = base::windows_gnullvm::opts();
base.cpu = "x86-64".into();
base.features = "+cx16,+sse3,+sahf".into();
base.plt_by_default = false;
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m64"]);
base.max_atomic_width = Some(64);
base.max_atomic_width = Some(128);
base.linker = Some("x86_64-w64-mingw32-clang".into());

Target {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::spec::{base, SanitizerSet, Target};
pub fn target() -> Target {
let mut base = base::windows_msvc::opts();
base.cpu = "x86-64".into();
base.features = "+cx16,+sse3,+sahf".into();
base.plt_by_default = false;
base.max_atomic_width = Some(64);
base.max_atomic_width = Some(128);
base.supported_sanitizers = SanitizerSet::ADDRESS;

Target {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use crate::spec::{base, Cc, LinkerFlavor, Lld, Target};
pub fn target() -> Target {
let mut base = base::windows_uwp_gnu::opts();
base.cpu = "x86-64".into();
base.features = "+cx16,+sse3,+sahf".into();
base.plt_by_default = false;
// Use high-entropy 64 bit address space for ASLR
base.add_pre_link_args(
LinkerFlavor::Gnu(Cc::No, Lld::No),
&["-m", "i386pep", "--high-entropy-va"],
);
base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m64", "-Wl,--high-entropy-va"]);
base.max_atomic_width = Some(64);
base.max_atomic_width = Some(128);

Target {
llvm_target: "x86_64-pc-windows-gnu".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crate::spec::{base, Target};
pub fn target() -> Target {
let mut base = base::windows_uwp_msvc::opts();
base.cpu = "x86-64".into();
base.features = "+cx16,+sse3,+sahf".into();
base.plt_by_default = false;
base.max_atomic_width = Some(64);
base.max_atomic_width = Some(128);

Target {
llvm_target: "x86_64-pc-windows-msvc".into(),
Expand Down
Loading
Loading