Skip to content

Commit af7bbfa

Browse files
authored
derive Copy where possible (#6844)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Chores** * Made many lightweight/internal types trivially copyable to simplify value handling across the codebase. * Added a workspace lint to surface missing copy implementations as warnings. * Extended buffer support with explicit retain/release hooks. * **Bug Fixes** * Improved diagnostics and debug output for thread/lock acquisition scenarios. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents dbbd921 + 2c2b137 commit af7bbfa

39 files changed

+89
-58
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ wasm-bindgen = "0.2.106"
222222
unsafe_code = "allow"
223223
unsafe_op_in_unsafe_fn = "deny"
224224
elided_lifetimes_in_paths = "warn"
225+
missing_copy_implementations = "warn"
225226

226227
[workspace.lints.clippy]
227228
# alloc_instead_of_core = "warn"

crates/codegen/src/compile.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ struct Compiler {
113113
in_annotation: bool,
114114
}
115115

116+
#[derive(Clone, Copy)]
116117
enum DoneWithFuture {
117118
No,
118119
DoneWithDoc,
119120
Yes,
120121
}
121122

122-
#[derive(Debug, Clone)]
123+
#[derive(Clone, Copy, Debug)]
123124
pub struct CompileOpts {
124125
/// How optimized the bytecode output should be; any optimize > 0 does
125126
/// not emit assert statements

crates/codegen/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::fmt::Display;
33
use rustpython_compiler_core::SourceLocation;
44
use thiserror::Error;
55

6-
#[derive(Debug)]
6+
#[derive(Clone, Copy, Debug)]
77
pub enum PatternUnreachableReason {
88
NameCapture,
99
Wildcard,

crates/codegen/src/ir.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl ops::IndexMut<BlockIdx> for Vec<Block> {
8585
}
8686
}
8787

88-
#[derive(Debug, Clone)]
88+
#[derive(Clone, Copy, Debug)]
8989
pub struct InstructionInfo {
9090
pub instr: AnyInstruction,
9191
pub arg: OpArg,
@@ -95,8 +95,8 @@ pub struct InstructionInfo {
9595
pub except_handler: Option<ExceptHandlerInfo>,
9696
}
9797

98-
/// Exception handler information for an instruction
99-
#[derive(Debug, Clone)]
98+
/// Exception handler information for an instruction.
99+
#[derive(Clone, Copy, Debug)]
100100
pub struct ExceptHandlerInfo {
101101
/// Block to jump to when exception occurs
102102
pub handler_block: BlockIdx,
@@ -742,13 +742,13 @@ fn generate_exception_table(blocks: &[Block], block_to_index: &[u32]) -> Box<[u8
742742
// instr_size includes EXTENDED_ARG instructions
743743
let instr_size = instr.arg.instr_size() as u32;
744744

745-
match (&current_entry, &instr.except_handler) {
745+
match (&current_entry, instr.except_handler) {
746746
// No current entry, no handler - nothing to do
747747
(None, None) => {}
748748

749749
// No current entry, handler starts - begin new entry
750750
(None, Some(handler)) => {
751-
current_entry = Some((handler.clone(), instr_index));
751+
current_entry = Some((handler, instr_index));
752752
}
753753

754754
// Current entry exists, same handler - continue
@@ -767,7 +767,7 @@ fn generate_exception_table(blocks: &[Block], block_to_index: &[u32]) -> Box<[u8
767767
curr_handler.stack_depth as u16,
768768
curr_handler.preserve_lasti,
769769
));
770-
current_entry = Some((handler.clone(), instr_index));
770+
current_entry = Some((handler, instr_index));
771771
}
772772

773773
// Current entry exists, no handler - finish current entry

crates/codegen/src/symboltable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ impl SymbolTableAnalyzer {
758758
}
759759
}
760760

761-
#[derive(Debug, Clone)]
761+
#[derive(Clone, Copy, Debug)]
762762
enum SymbolUsage {
763763
Global,
764764
Nonlocal,

crates/common/src/cformat.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustpython_literal::{float, format::Case};
1414

1515
use crate::wtf8::{CodePoint, Wtf8, Wtf8Buf};
1616

17-
#[derive(Debug, PartialEq)]
17+
#[derive(Clone, Copy, Debug, PartialEq)]
1818
pub enum CFormatErrorType {
1919
UnmatchedKeyParentheses,
2020
MissingModuloSign,
@@ -27,7 +27,7 @@ pub enum CFormatErrorType {
2727
// also contains how many chars the parsing function consumed
2828
pub type ParsingError = (CFormatErrorType, usize);
2929

30-
#[derive(Debug, PartialEq)]
30+
#[derive(Clone, Copy, Debug, PartialEq)]
3131
pub struct CFormatError {
3232
pub typ: CFormatErrorType, // FIXME
3333
pub index: usize,

crates/common/src/encodings.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ pub mod errors {
262262
use super::*;
263263
use core::fmt::Write;
264264

265+
#[derive(Clone, Copy)]
265266
pub struct Strict;
266267

267268
impl<Ctx: EncodeContext> EncodeErrorHandler<Ctx> for Strict {
@@ -286,6 +287,7 @@ pub mod errors {
286287
}
287288
}
288289

290+
#[derive(Clone, Copy)]
289291
pub struct Ignore;
290292

291293
impl<Ctx: EncodeContext> EncodeErrorHandler<Ctx> for Ignore {
@@ -310,6 +312,7 @@ pub mod errors {
310312
}
311313
}
312314

315+
#[derive(Clone, Copy)]
313316
pub struct Replace;
314317

315318
impl<Ctx: EncodeContext> EncodeErrorHandler<Ctx> for Replace {
@@ -338,6 +341,7 @@ pub mod errors {
338341
}
339342
}
340343

344+
#[derive(Clone, Copy)]
341345
pub struct XmlCharRefReplace;
342346

343347
impl<Ctx: EncodeContext> EncodeErrorHandler<Ctx> for XmlCharRefReplace {
@@ -358,6 +362,7 @@ pub mod errors {
358362
}
359363
}
360364

365+
#[derive(Clone, Copy)]
361366
pub struct BackslashReplace;
362367

363368
impl<Ctx: EncodeContext> EncodeErrorHandler<Ctx> for BackslashReplace {
@@ -394,6 +399,7 @@ pub mod errors {
394399
}
395400
}
396401

402+
#[derive(Clone, Copy)]
397403
pub struct NameReplace;
398404

399405
impl<Ctx: EncodeContext> EncodeErrorHandler<Ctx> for NameReplace {
@@ -422,6 +428,7 @@ pub mod errors {
422428
}
423429
}
424430

431+
#[derive(Clone, Copy)]
425432
pub struct SurrogateEscape;
426433

427434
impl<Ctx: EncodeContext> EncodeErrorHandler<Ctx> for SurrogateEscape {

crates/common/src/fileutils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub mod windows {
4646

4747
pub const SECS_BETWEEN_EPOCHS: i64 = 11644473600; // Seconds between 1.1.1601 and 1.1.1970
4848

49-
#[derive(Default)]
49+
#[derive(Clone, Copy, Default)]
5050
pub struct StatStruct {
5151
pub st_dev: libc::c_ulong,
5252
pub st_ino: u64,
@@ -256,6 +256,7 @@ pub mod windows {
256256
m as _
257257
}
258258

259+
#[derive(Clone, Copy)]
259260
#[repr(C)]
260261
pub struct FILE_STAT_BASIC_INFORMATION {
261262
pub FileId: i64,
@@ -275,8 +276,9 @@ pub mod windows {
275276
pub FileId128: [u64; 2],
276277
}
277278

278-
#[repr(C)]
279279
#[allow(dead_code)]
280+
#[derive(Clone, Copy)]
281+
#[repr(C)]
280282
pub enum FILE_INFO_BY_NAME_CLASS {
281283
FileStatByNameInfo,
282284
FileStatLxByNameInfo,

crates/common/src/format.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl FormatParse for FormatSign {
110110
}
111111
}
112112

113-
#[derive(Debug, PartialEq)]
113+
#[derive(Clone, Copy, Debug, PartialEq)]
114114
pub enum FormatGrouping {
115115
Comma,
116116
Underscore,
@@ -136,7 +136,7 @@ impl From<&FormatGrouping> for char {
136136
}
137137
}
138138

139-
#[derive(Debug, PartialEq)]
139+
#[derive(Clone, Copy, Debug, PartialEq)]
140140
pub enum FormatType {
141141
String,
142142
Binary,
@@ -199,7 +199,7 @@ impl FormatParse for FormatType {
199199
}
200200
}
201201

202-
#[derive(Debug, PartialEq)]
202+
#[derive(Clone, Copy, Debug, PartialEq)]
203203
pub struct FormatSpec {
204204
conversion: Option<FormatConversion>,
205205
fill: Option<CodePoint>,
@@ -845,7 +845,7 @@ impl Deref for AsciiStr<'_> {
845845
}
846846
}
847847

848-
#[derive(Debug, PartialEq)]
848+
#[derive(Clone, Copy, Debug, PartialEq)]
849849
pub enum FormatSpecError {
850850
DecimalDigitsTooMany,
851851
PrecisionTooBig,
@@ -862,7 +862,7 @@ pub enum FormatSpecError {
862862
NotImplemented(char, &'static str),
863863
}
864864

865-
#[derive(Debug, PartialEq)]
865+
#[derive(Clone, Copy, Debug, PartialEq)]
866866
pub enum FormatParseError {
867867
UnmatchedBracket,
868868
MissingStartBracket,

crates/common/src/hash.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub const SEED_BITS: usize = core::mem::size_of::<u64>() * 2 * 8;
2525

2626
// pub const CUTOFF: usize = 7;
2727

28+
#[derive(Clone, Copy)]
2829
pub struct HashSecret {
2930
k0: u64,
3031
k1: u64,

0 commit comments

Comments
 (0)