From c629bfc9e177a9f6380cb6bad166e51194c18887 Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Sat, 20 Jul 2024 08:21:26 +0000 Subject: [PATCH 1/6] Inject win arm32 shims into metadata generation --- library/std/src/sys/pal/windows/c.rs | 41 ------------------- .../std/src/sys/pal/windows/c/bindings.txt | 2 + .../std/src/sys/pal/windows/c/windows_sys.rs | 24 +++++++++++ src/tools/generate-windows-sys/src/main.rs | 19 +++++++++ 4 files changed, 45 insertions(+), 41 deletions(-) diff --git a/library/std/src/sys/pal/windows/c.rs b/library/std/src/sys/pal/windows/c.rs index 296d19a926d96..9ab31bf6f7538 100644 --- a/library/std/src/sys/pal/windows/c.rs +++ b/library/std/src/sys/pal/windows/c.rs @@ -457,44 +457,3 @@ compat_fn_with_fallback! { Status as u32 } } - -// # Arm32 shim -// -// AddVectoredExceptionHandler and WSAStartup use platform-specific types. -// However, Microsoft no longer supports thumbv7a so definitions for those targets -// are not included in the win32 metadata. We work around that by defining them here. -// -// Where possible, these definitions should be kept in sync with https://docs.rs/windows-sys -cfg_if::cfg_if! { -if #[cfg(not(target_vendor = "uwp"))] { - #[link(name = "kernel32")] - extern "system" { - pub fn AddVectoredExceptionHandler( - first: u32, - handler: PVECTORED_EXCEPTION_HANDLER, - ) -> *mut c_void; - } - pub type PVECTORED_EXCEPTION_HANDLER = Option< - unsafe extern "system" fn(exceptioninfo: *mut EXCEPTION_POINTERS) -> i32, - >; - #[repr(C)] - pub struct EXCEPTION_POINTERS { - pub ExceptionRecord: *mut EXCEPTION_RECORD, - pub ContextRecord: *mut CONTEXT, - } - #[cfg(target_arch = "arm")] - pub enum CONTEXT {} -}} -// WSAStartup is only redefined here so that we can override WSADATA for Arm32 -windows_targets::link!("ws2_32.dll" "system" fn WSAStartup(wversionrequested: u16, lpwsadata: *mut WSADATA) -> i32); -#[cfg(target_arch = "arm")] -#[repr(C)] -pub struct WSADATA { - pub wVersion: u16, - pub wHighVersion: u16, - pub szDescription: [u8; 257], - pub szSystemStatus: [u8; 129], - pub iMaxSockets: u16, - pub iMaxUdpDg: u16, - pub lpVendorInfo: PSTR, -} diff --git a/library/std/src/sys/pal/windows/c/bindings.txt b/library/std/src/sys/pal/windows/c/bindings.txt index 5ad4a3731d822..258403e090aca 100644 --- a/library/std/src/sys/pal/windows/c/bindings.txt +++ b/library/std/src/sys/pal/windows/c/bindings.txt @@ -2175,6 +2175,7 @@ Windows.Win32.Networking.WinSock.WSARecv Windows.Win32.Networking.WinSock.WSASend Windows.Win32.Networking.WinSock.WSASERVICE_NOT_FOUND Windows.Win32.Networking.WinSock.WSASocketW +Windows.Win32.Networking.WinSock.WSAStartup Windows.Win32.Networking.WinSock.WSASYSCALLFAILURE Windows.Win32.Networking.WinSock.WSASYSNOTREADY Windows.Win32.Networking.WinSock.WSATRY_AGAIN @@ -2419,6 +2420,7 @@ Windows.Win32.System.Console.STD_HANDLE Windows.Win32.System.Console.STD_INPUT_HANDLE Windows.Win32.System.Console.STD_OUTPUT_HANDLE Windows.Win32.System.Console.WriteConsoleW +Windows.Win32.System.Diagnostics.Debug.AddVectoredExceptionHandler Windows.Win32.System.Diagnostics.Debug.ARM64_NT_NEON128 Windows.Win32.System.Diagnostics.Debug.CONTEXT Windows.Win32.System.Diagnostics.Debug.EXCEPTION_RECORD diff --git a/library/std/src/sys/pal/windows/c/windows_sys.rs b/library/std/src/sys/pal/windows/c/windows_sys.rs index fea00fec9ae59..1f06cc990b61a 100644 --- a/library/std/src/sys/pal/windows/c/windows_sys.rs +++ b/library/std/src/sys/pal/windows/c/windows_sys.rs @@ -5,6 +5,7 @@ windows_targets::link!("advapi32.dll" "system" fn OpenProcessToken(processhandle windows_targets::link!("advapi32.dll" "system" "SystemFunction036" fn RtlGenRandom(randombuffer : *mut core::ffi::c_void, randombufferlength : u32) -> BOOLEAN); windows_targets::link!("kernel32.dll" "system" fn AcquireSRWLockExclusive(srwlock : *mut SRWLOCK)); windows_targets::link!("kernel32.dll" "system" fn AcquireSRWLockShared(srwlock : *mut SRWLOCK)); +windows_targets::link!("kernel32.dll" "system" fn AddVectoredExceptionHandler(first : u32, handler : PVECTORED_EXCEPTION_HANDLER) -> *mut core::ffi::c_void); windows_targets::link!("kernel32.dll" "system" fn CancelIo(hfile : HANDLE) -> BOOL); windows_targets::link!("kernel32.dll" "system" fn CloseHandle(hobject : HANDLE) -> BOOL); windows_targets::link!("kernel32.dll" "system" fn CompareStringOrdinal(lpstring1 : PCWSTR, cchcount1 : i32, lpstring2 : PCWSTR, cchcount2 : i32, bignorecase : BOOL) -> COMPARESTRING_RESULT); @@ -113,6 +114,7 @@ windows_targets::link!("ws2_32.dll" "system" fn WSAGetLastError() -> WSA_ERROR); windows_targets::link!("ws2_32.dll" "system" fn WSARecv(s : SOCKET, lpbuffers : *const WSABUF, dwbuffercount : u32, lpnumberofbytesrecvd : *mut u32, lpflags : *mut u32, lpoverlapped : *mut OVERLAPPED, lpcompletionroutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE) -> i32); windows_targets::link!("ws2_32.dll" "system" fn WSASend(s : SOCKET, lpbuffers : *const WSABUF, dwbuffercount : u32, lpnumberofbytessent : *mut u32, dwflags : u32, lpoverlapped : *mut OVERLAPPED, lpcompletionroutine : LPWSAOVERLAPPED_COMPLETION_ROUTINE) -> i32); windows_targets::link!("ws2_32.dll" "system" fn WSASocketW(af : i32, r#type : i32, protocol : i32, lpprotocolinfo : *const WSAPROTOCOL_INFOW, g : u32, dwflags : u32) -> SOCKET); +windows_targets::link!("ws2_32.dll" "system" fn WSAStartup(wversionrequested : u16, lpwsadata : *mut WSADATA) -> i32); windows_targets::link!("ws2_32.dll" "system" fn accept(s : SOCKET, addr : *mut SOCKADDR, addrlen : *mut i32) -> SOCKET); windows_targets::link!("ws2_32.dll" "system" fn bind(s : SOCKET, name : *const SOCKADDR, namelen : i32) -> i32); windows_targets::link!("ws2_32.dll" "system" fn closesocket(s : SOCKET) -> i32); @@ -2283,6 +2285,12 @@ pub type EXCEPTION_DISPOSITION = i32; pub const EXCEPTION_MAXIMUM_PARAMETERS: u32 = 15u32; #[repr(C)] #[derive(Clone, Copy)] +pub struct EXCEPTION_POINTERS { + pub ExceptionRecord: *mut EXCEPTION_RECORD, + pub ContextRecord: *mut CONTEXT, +} +#[repr(C)] +#[derive(Clone, Copy)] pub struct EXCEPTION_RECORD { pub ExceptionCode: NTSTATUS, pub ExceptionFlags: u32, @@ -2859,6 +2867,8 @@ pub type PTIMERAPCROUTINE = Option< dwtimerhighvalue: u32, ), >; +pub type PVECTORED_EXCEPTION_HANDLER = + Option i32>; pub type PWSTR = *mut u16; pub const READ_CONTROL: FILE_ACCESS_RIGHTS = 131072u32; pub const REALTIME_PRIORITY_CLASS: PROCESS_CREATION_FLAGS = 256u32; @@ -3283,5 +3293,19 @@ pub struct XSAVE_FORMAT { pub XmmRegisters: [M128A; 8], pub Reserved4: [u8; 224], } + +#[cfg(target_arch = "arm")] +#[repr(C)] +pub struct WSADATA { + pub wVersion: u16, + pub wHighVersion: u16, + pub szDescription: [u8; 257], + pub szSystemStatus: [u8; 129], + pub iMaxSockets: u16, + pub iMaxUdpDg: u16, + pub lpVendorInfo: PSTR, +} +#[cfg(target_arch = "arm")] +pub enum CONTEXT {} // ignore-tidy-filelength use super::windows_targets; diff --git a/src/tools/generate-windows-sys/src/main.rs b/src/tools/generate-windows-sys/src/main.rs index fd21e7a86b002..90fce2b675ad7 100644 --- a/src/tools/generate-windows-sys/src/main.rs +++ b/src/tools/generate-windows-sys/src/main.rs @@ -4,6 +4,24 @@ use std::fs; use std::io::{Read, Seek, SeekFrom, Write}; use std::path::PathBuf; +/// 32-bit ARM is not supported by Microsoft so ARM types are not generated. +/// Therefore we need to inject a few types to make the bindings work. +const ARM32_SHIM: &str = r#" +#[cfg(target_arch = "arm")] +#[repr(C)] +pub struct WSADATA { + pub wVersion: u16, + pub wHighVersion: u16, + pub szDescription: [u8; 257], + pub szSystemStatus: [u8; 129], + pub iMaxSockets: u16, + pub iMaxUdpDg: u16, + pub lpVendorInfo: PSTR, +} +#[cfg(target_arch = "arm")] +pub enum CONTEXT {} +"#; + fn main() -> Result<(), Box> { let mut path: PathBuf = env::args_os().nth(1).expect("a path to the rust repository is required").into(); @@ -16,6 +34,7 @@ fn main() -> Result<(), Box> { println!("{info}"); let mut f = std::fs::File::options().append(true).open("windows_sys.rs")?; + f.write_all(ARM32_SHIM.as_bytes())?; writeln!(&mut f, "// ignore-tidy-filelength")?; writeln!(&mut f, "use super::windows_targets;")?; From db8cdc5d37da5c0cf306fba51d07d6af10d69bd7 Mon Sep 17 00:00:00 2001 From: GnomedDev Date: Mon, 24 Jun 2024 16:17:59 +0100 Subject: [PATCH 2/6] Use Cow<'static, str> for InlineAsmTemplatePiece::String --- compiler/rustc_ast/src/ast.rs | 3 +- compiler/rustc_builtin_macros/src/asm.rs | 6 +-- .../rustc_codegen_cranelift/src/inline_asm.rs | 4 +- .../src/intrinsics/llvm_x86.rs | 42 ++++++++++--------- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 75c656973f963..4d19be62df809 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -36,6 +36,7 @@ use rustc_macros::{Decodable, Encodable, HashStable_Generic}; use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP}; +use std::borrow::Cow; use std::cmp; use std::fmt; use std::mem; @@ -2272,7 +2273,7 @@ impl std::fmt::Debug for InlineAsmOptions { #[derive(Clone, PartialEq, Encodable, Decodable, Debug, Hash, HashStable_Generic)] pub enum InlineAsmTemplatePiece { - String(String), + String(Cow<'static, str>), Placeholder { operand_idx: usize, modifier: Option, span: Span }, } diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs index dd0f9aaf22104..b8fe6338493d0 100644 --- a/compiler/rustc_builtin_macros/src/asm.rs +++ b/compiler/rustc_builtin_macros/src/asm.rs @@ -459,7 +459,7 @@ fn expand_preparsed_asm( for (i, template_expr) in args.templates.into_iter().enumerate() { if i != 0 { - template.push(ast::InlineAsmTemplatePiece::String("\n".to_string())); + template.push(ast::InlineAsmTemplatePiece::String("\n".into())); } let msg = "asm template must be a string literal"; @@ -527,7 +527,7 @@ fn expand_preparsed_asm( // Don't treat raw asm as a format string. if args.options.contains(ast::InlineAsmOptions::RAW) { - template.push(ast::InlineAsmTemplatePiece::String(template_str.to_string())); + template.push(ast::InlineAsmTemplatePiece::String(template_str.to_string().into())); let template_num_lines = 1 + template_str.matches('\n').count(); line_spans.extend(std::iter::repeat(template_sp).take(template_num_lines)); continue; @@ -577,7 +577,7 @@ fn expand_preparsed_asm( for piece in unverified_pieces { match piece { parse::Piece::String(s) => { - template.push(ast::InlineAsmTemplatePiece::String(s.to_string())) + template.push(ast::InlineAsmTemplatePiece::String(s.to_string().into())) } parse::Piece::NextArgument(arg) => { let span = arg_spans.next().unwrap_or(template_sp); diff --git a/compiler/rustc_codegen_cranelift/src/inline_asm.rs b/compiler/rustc_codegen_cranelift/src/inline_asm.rs index c88230c936056..16edec47e1029 100644 --- a/compiler/rustc_codegen_cranelift/src/inline_asm.rs +++ b/compiler/rustc_codegen_cranelift/src/inline_asm.rs @@ -46,9 +46,7 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>( // Used by panic_abort on Windows, but uses a syntax which only happens to work with // asm!() by accident and breaks with the GNU assembler as well as global_asm!() for // the LLVM backend. - if template.len() == 1 - && template[0] == InlineAsmTemplatePiece::String("int $$0x29".to_string()) - { + if template.len() == 1 && template[0] == InlineAsmTemplatePiece::String("int $$0x29".into()) { fx.bcx.ins().trap(TrapCode::User(1)); return; } diff --git a/compiler/rustc_codegen_cranelift/src/intrinsics/llvm_x86.rs b/compiler/rustc_codegen_cranelift/src/intrinsics/llvm_x86.rs index e1896138e487b..a20faa2cad3a8 100644 --- a/compiler/rustc_codegen_cranelift/src/intrinsics/llvm_x86.rs +++ b/compiler/rustc_codegen_cranelift/src/intrinsics/llvm_x86.rs @@ -40,7 +40,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( shl rdx, 32 or rax, rdx " - .to_string(), + .into(), )], &[ CInlineAsmOperand::In { @@ -471,7 +471,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( // into 0x80000000 for which Cranelift doesn't have a native instruction. codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String(format!("cvtps2dq xmm0, xmm0"))], + &[InlineAsmTemplatePiece::String("cvtps2dq xmm0, xmm0".into())], &[CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm0)), _late: true, @@ -875,7 +875,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String(asm.to_string())], + &[InlineAsmTemplatePiece::String(asm.into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::ax)), @@ -914,7 +914,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String(format!("pcmpestri xmm0, xmm1, {imm8}"))], + &[InlineAsmTemplatePiece::String(format!("pcmpestri xmm0, xmm1, {imm8}").into())], &[ CInlineAsmOperand::In { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm0)), @@ -967,7 +967,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String(format!("pcmpestrm xmm0, xmm1, {imm8}"))], + &[InlineAsmTemplatePiece::String(format!("pcmpestrm xmm0, xmm1, {imm8}").into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm0)), @@ -1015,7 +1015,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String(format!("pclmulqdq xmm0, xmm1, {imm8}"))], + &[InlineAsmTemplatePiece::String(format!("pclmulqdq xmm0, xmm1, {imm8}").into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm0)), @@ -1052,7 +1052,9 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String(format!("aeskeygenassist xmm0, xmm0, {imm8}"))], + &[InlineAsmTemplatePiece::String( + format!("aeskeygenassist xmm0, xmm0, {imm8}").into(), + )], &[CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm0)), _late: true, @@ -1071,7 +1073,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("aesimc xmm0, xmm0".to_string())], + &[InlineAsmTemplatePiece::String("aesimc xmm0, xmm0".into())], &[CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm0)), _late: true, @@ -1091,7 +1093,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("aesenc xmm0, xmm1".to_string())], + &[InlineAsmTemplatePiece::String("aesenc xmm0, xmm1".into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm0)), @@ -1117,7 +1119,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("aesenclast xmm0, xmm1".to_string())], + &[InlineAsmTemplatePiece::String("aesenclast xmm0, xmm1".into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm0)), @@ -1143,7 +1145,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("aesdec xmm0, xmm1".to_string())], + &[InlineAsmTemplatePiece::String("aesdec xmm0, xmm1".into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm0)), @@ -1169,7 +1171,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("aesdeclast xmm0, xmm1".to_string())], + &[InlineAsmTemplatePiece::String("aesdeclast xmm0, xmm1".into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm0)), @@ -1207,7 +1209,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String(format!("sha1rnds4 xmm1, xmm2, {func}"))], + &[InlineAsmTemplatePiece::String(format!("sha1rnds4 xmm1, xmm2, {func}").into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm1)), @@ -1233,7 +1235,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("sha1msg1 xmm1, xmm2".to_string())], + &[InlineAsmTemplatePiece::String("sha1msg1 xmm1, xmm2".into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm1)), @@ -1259,7 +1261,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("sha1msg2 xmm1, xmm2".to_string())], + &[InlineAsmTemplatePiece::String("sha1msg2 xmm1, xmm2".into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm1)), @@ -1285,7 +1287,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("sha1nexte xmm1, xmm2".to_string())], + &[InlineAsmTemplatePiece::String("sha1nexte xmm1, xmm2".into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm1)), @@ -1312,7 +1314,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("sha256rnds2 xmm1, xmm2".to_string())], + &[InlineAsmTemplatePiece::String("sha256rnds2 xmm1, xmm2".into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm1)), @@ -1343,7 +1345,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("sha256msg1 xmm1, xmm2".to_string())], + &[InlineAsmTemplatePiece::String("sha256msg1 xmm1, xmm2".into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm1)), @@ -1369,7 +1371,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("sha256msg2 xmm1, xmm2".to_string())], + &[InlineAsmTemplatePiece::String("sha256msg2 xmm1, xmm2".into())], &[ CInlineAsmOperand::InOut { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::xmm1)), @@ -1435,7 +1437,7 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( let edx_place = res_place.place_field(fx, FieldIdx::new(1)); codegen_inline_asm_inner( fx, - &[InlineAsmTemplatePiece::String("rdtsc".to_string())], + &[InlineAsmTemplatePiece::String("rdtsc".into())], &[ CInlineAsmOperand::Out { reg: InlineAsmRegOrRegClass::Reg(InlineAsmReg::X86(X86InlineAsmReg::ax)), From cf9816c17e1c265af2906a8a5a7c025be3f3dccb Mon Sep 17 00:00:00 2001 From: Pavel Grigorenko Date: Wed, 24 Jul 2024 15:28:31 +0300 Subject: [PATCH 3/6] CStr: derive PartialEq, Eq; add test for Ord --- library/core/src/ffi/c_str.rs | 15 ++++----------- library/core/tests/ffi.rs | 1 + library/core/tests/ffi/cstr.rs | 15 +++++++++++++++ library/core/tests/lib.rs | 1 + 4 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 library/core/tests/ffi.rs create mode 100644 library/core/tests/ffi/cstr.rs diff --git a/library/core/src/ffi/c_str.rs b/library/core/src/ffi/c_str.rs index c9111254082da..ae42ae3baf418 100644 --- a/library/core/src/ffi/c_str.rs +++ b/library/core/src/ffi/c_str.rs @@ -94,7 +94,7 @@ use crate::str; /// ``` /// /// [str]: prim@str "str" -#[derive(Hash)] +#[derive(PartialEq, Eq, Hash)] #[stable(feature = "core_c_str", since = "1.64.0")] #[rustc_has_incoherent_inherent_impls] #[lang = "CStr"] @@ -104,7 +104,6 @@ use crate::str; // want `repr(transparent)` but we don't want it to show up in rustdoc, so we hide it under // `cfg(doc)`. This is an ad-hoc implementation of attribute privacy. #[repr(transparent)] -#[allow(clippy::derived_hash_with_manual_eq)] pub struct CStr { // FIXME: this should not be represented with a DST slice but rather with // just a raw `c_char` along with some form of marker to make @@ -678,15 +677,9 @@ impl CStr { } } -#[stable(feature = "rust1", since = "1.0.0")] -impl PartialEq for CStr { - #[inline] - fn eq(&self, other: &CStr) -> bool { - self.to_bytes().eq(other.to_bytes()) - } -} -#[stable(feature = "rust1", since = "1.0.0")] -impl Eq for CStr {} +// `.to_bytes()` representations are compared instead of the inner `[c_char]`s, +// because `c_char` is `i8` (not `u8`) on some platforms. +// That is why this is implemented manually and not derived. #[stable(feature = "rust1", since = "1.0.0")] impl PartialOrd for CStr { #[inline] diff --git a/library/core/tests/ffi.rs b/library/core/tests/ffi.rs new file mode 100644 index 0000000000000..2b33fbd95f073 --- /dev/null +++ b/library/core/tests/ffi.rs @@ -0,0 +1 @@ +mod cstr; diff --git a/library/core/tests/ffi/cstr.rs b/library/core/tests/ffi/cstr.rs new file mode 100644 index 0000000000000..9bf4c21a9ab97 --- /dev/null +++ b/library/core/tests/ffi/cstr.rs @@ -0,0 +1,15 @@ +use core::ffi::CStr; + +#[test] +fn compares_as_u8s() { + let a: &CStr = c"Hello!"; // Starts with ascii + let a_bytes: &[u8] = a.to_bytes(); + assert!((..0b1000_0000).contains(&a_bytes[0])); + + let b: &CStr = c"こんにちは!"; // Starts with non ascii + let b_bytes: &[u8] = b.to_bytes(); + assert!((0b1000_0000..).contains(&b_bytes[0])); + + assert_eq!(Ord::cmp(a, b), Ord::cmp(a_bytes, b_bytes)); + assert_eq!(PartialOrd::partial_cmp(a, b), PartialOrd::partial_cmp(a_bytes, b_bytes)); +} diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 83a615fcd8be3..04aaa3f35b7ae 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -132,6 +132,7 @@ mod clone; mod cmp; mod const_ptr; mod convert; +mod ffi; mod fmt; mod future; mod hash; From 4ac60601d32d96688494c4bb404a04205501a02b Mon Sep 17 00:00:00 2001 From: surechen Date: Thu, 25 Jul 2024 22:33:45 +0800 Subject: [PATCH 4/6] Fix a span error when parsing a wrong param of function. fixes #128042 --- compiler/rustc_parse/src/parser/item.rs | 9 +++++++- .../suggest-add-self-issue-128042.rs | 12 ++++++++++ .../suggest-add-self-issue-128042.stderr | 22 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/ui/suggestions/suggest-add-self-issue-128042.rs create mode 100644 tests/ui/suggestions/suggest-add-self-issue-128042.stderr diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index fbc5b91460034..9aaf4b99243f5 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -2773,7 +2773,14 @@ impl<'a> Parser<'a> { let snapshot = p.create_snapshot_for_diagnostic(); let param = p.parse_param_general(req_name, first_param).or_else(|e| { let guar = e.emit(); - let lo = p.prev_token.span; + // When parsing a param failed, we should check to make the span of the param + // not contain '(' before it. + // For example when parsing `*mut Self` in function `fn oof(*mut Self)`. + let lo = if let TokenKind::OpenDelim(Delimiter::Parenthesis) = p.prev_token.kind { + p.prev_token.span.shrink_to_hi() + } else { + p.prev_token.span + }; p.restore_snapshot(snapshot); // Skip every token until next possible arg or end. p.eat_to_tokens(&[&token::Comma, &token::CloseDelim(Delimiter::Parenthesis)]); diff --git a/tests/ui/suggestions/suggest-add-self-issue-128042.rs b/tests/ui/suggestions/suggest-add-self-issue-128042.rs new file mode 100644 index 0000000000000..f94d166c4967a --- /dev/null +++ b/tests/ui/suggestions/suggest-add-self-issue-128042.rs @@ -0,0 +1,12 @@ +struct Thing { + state: u8, +} + +impl Thing { + fn oof(*mut Self) { //~ ERROR expected parameter name, found `*` + self.state = 1; + //~^ ERROR expected value, found module `self` + } +} + +fn main() {} diff --git a/tests/ui/suggestions/suggest-add-self-issue-128042.stderr b/tests/ui/suggestions/suggest-add-self-issue-128042.stderr new file mode 100644 index 0000000000000..49ac15632501a --- /dev/null +++ b/tests/ui/suggestions/suggest-add-self-issue-128042.stderr @@ -0,0 +1,22 @@ +error: expected parameter name, found `*` + --> $DIR/suggest-add-self-issue-128042.rs:6:12 + | +LL | fn oof(*mut Self) { + | ^ expected parameter name + +error[E0424]: expected value, found module `self` + --> $DIR/suggest-add-self-issue-128042.rs:7:9 + | +LL | fn oof(*mut Self) { + | --- this function doesn't have a `self` parameter +LL | self.state = 1; + | ^^^^ `self` value is a keyword only available in methods with a `self` parameter + | +help: add a `self` receiver parameter to make the associated `fn` a method + | +LL | fn oof(&self, *mut Self) { + | ++++++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0424`. From f0ba8c4ffb092469fe603f16074fcc5a60dc8eb6 Mon Sep 17 00:00:00 2001 From: Urgau <3616612+Urgau@users.noreply.github.com> Date: Thu, 25 Jul 2024 16:57:57 +0200 Subject: [PATCH 5/6] Fix 1.80.0 version in RELEASES.md --- RELEASES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASES.md b/RELEASES.md index 0ecd472efb6e2..2c91ddf782674 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -1,4 +1,4 @@ -Version 1.80 (2024-07-25) +Version 1.80.0 (2024-07-25) ========================== From 0706cc6397cfe51bbffdefa134e6459a0f7c049a Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 25 Jul 2024 15:33:34 +0000 Subject: [PATCH 6/6] Turn an unreachable code path into an ICE --- compiler/rustc_mir_build/src/thir/cx/expr.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_mir_build/src/thir/cx/expr.rs b/compiler/rustc_mir_build/src/thir/cx/expr.rs index 99afb500e0b6f..5f13b329de40d 100644 --- a/compiler/rustc_mir_build/src/thir/cx/expr.rs +++ b/compiler/rustc_mir_build/src/thir/cx/expr.rs @@ -3,7 +3,6 @@ use crate::thir::cx::region::Scope; use crate::thir::cx::Cx; use crate::thir::util::UserAnnotatedTyHelpers; use itertools::Itertools; -use rustc_ast::LitKind; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_hir as hir; use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; @@ -22,8 +21,7 @@ use rustc_middle::ty::{ self, AdtKind, InlineConstArgs, InlineConstArgsParts, ScalarInt, Ty, UpvarArgs, UserType, }; use rustc_middle::{bug, span_bug}; -use rustc_span::source_map::Spanned; -use rustc_span::{sym, Span, DUMMY_SP}; +use rustc_span::{sym, Span}; use rustc_target::abi::{FieldIdx, FIRST_VARIANT}; use tracing::{debug, info, instrument, trace}; @@ -899,14 +897,10 @@ impl<'tcx> Cx<'tcx> { let hir_id = self.tcx.local_def_id_to_hir_id(def_id.expect_local()); let generics = self.tcx.generics_of(hir_id.owner); let Some(&index) = generics.param_def_id_to_index.get(&def_id) else { - let guar = self.tcx.dcx().has_errors().unwrap(); - // We already errored about a late bound const - - let lit = self - .tcx - .hir_arena - .alloc(Spanned { span: DUMMY_SP, node: LitKind::Err(guar) }); - return ExprKind::Literal { lit, neg: false }; + span_bug!( + expr.span, + "Should have already errored about late bound consts: {def_id:?}" + ); }; let name = self.tcx.hir().name(hir_id); let param = ty::ParamConst::new(index, name);