Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ unsafe_op_in_unsafe_fn = "deny"
elided_lifetimes_in_paths = "warn"

[workspace.lints.clippy]
# alloc_instead_of_core = "warn"
# std_instead_of_alloc = "warn"
# std_instead_of_core = "warn"
perf = "warn"
style = "warn"
complexity = "warn"
Expand Down
13 changes: 7 additions & 6 deletions crates/codegen/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
symboltable::{self, CompilerScope, SymbolFlags, SymbolScope, SymbolTable},
unparse::UnparseExpr,
};
use alloc::borrow::Cow;
use itertools::Itertools;
use malachite_bigint::BigInt;
use num_complex::Complex;
Expand All @@ -42,7 +43,7 @@ use rustpython_compiler_core::{
},
};
use rustpython_wtf8::Wtf8Buf;
use std::{borrow::Cow, collections::HashSet};
use std::collections::HashSet;

const MAXBLOCKS: usize = 20;

Expand Down Expand Up @@ -293,7 +294,7 @@ fn compiler_unwrap_option<T>(zelf: &Compiler, o: Option<T>) -> T {
o.unwrap()
}

// fn compiler_result_unwrap<T, E: std::fmt::Debug>(zelf: &Compiler, result: Result<T, E>) -> T {
// fn compiler_result_unwrap<T, E: core::fmt::Debug>(zelf: &Compiler, result: Result<T, E>) -> T {
// if result.is_err() {
// eprintln!("=== CODEGEN PANIC INFO ===");
// eprintln!("This IS an internal error, an result was unwrapped during codegen");
Expand Down Expand Up @@ -1831,7 +1832,7 @@ impl Compiler {
name.to_owned(),
);

let args_iter = std::iter::empty()
let args_iter = core::iter::empty()
.chain(&parameters.posonlyargs)
.chain(&parameters.args)
.map(|arg| &arg.parameter)
Expand Down Expand Up @@ -2438,7 +2439,7 @@ impl Compiler {
let mut funcflags = bytecode::MakeFunctionFlags::empty();

// Handle positional defaults
let defaults: Vec<_> = std::iter::empty()
let defaults: Vec<_> = core::iter::empty()
.chain(&parameters.posonlyargs)
.chain(&parameters.args)
.filter_map(|x| x.default.as_deref())
Expand Down Expand Up @@ -2566,7 +2567,7 @@ impl Compiler {
let mut num_annotations = 0;

// Handle parameter annotations
let parameters_iter = std::iter::empty()
let parameters_iter = core::iter::empty()
.chain(&parameters.posonlyargs)
.chain(&parameters.args)
.chain(&parameters.kwonlyargs)
Expand Down Expand Up @@ -4965,7 +4966,7 @@ impl Compiler {
let name = "<lambda>".to_owned();

// Prepare defaults before entering function
let defaults: Vec<_> = std::iter::empty()
let defaults: Vec<_> = core::iter::empty()
.chain(&params.posonlyargs)
.chain(&params.args)
.filter_map(|x| x.default.as_deref())
Expand Down
5 changes: 3 additions & 2 deletions crates/codegen/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::fmt;
use core::fmt::Display;
use rustpython_compiler_core::SourceLocation;
use std::fmt::{self, Display};
use thiserror::Error;

#[derive(Debug)]
Expand Down Expand Up @@ -93,7 +94,7 @@ pub enum CodegenErrorType {
NotImplementedYet, // RustPython marker for unimplemented features
}

impl std::error::Error for CodegenErrorType {}
impl core::error::Error for CodegenErrorType {}

impl fmt::Display for CodegenErrorType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
6 changes: 3 additions & 3 deletions crates/codegen/src/ir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops;
use core::ops;

use crate::{IndexMap, IndexSet, error::InternalError};
use rustpython_compiler_core::{
Expand Down Expand Up @@ -198,7 +198,7 @@ impl CodeInfo {
*arg = new_arg;
}
let (extras, lo_arg) = arg.split();
locations.extend(std::iter::repeat_n(info.location, arg.instr_size()));
locations.extend(core::iter::repeat_n(info.location, arg.instr_size()));
instructions.extend(
extras
.map(|byte| CodeUnit::new(Instruction::ExtendedArg, byte))
Expand Down Expand Up @@ -401,7 +401,7 @@ fn stackdepth_push(

fn iter_blocks(blocks: &[Block]) -> impl Iterator<Item = (BlockIdx, &Block)> + '_ {
let mut next = BlockIdx(0);
std::iter::from_fn(move || {
core::iter::from_fn(move || {
if next == BlockIdx::NULL {
return None;
}
Expand Down
2 changes: 2 additions & 0 deletions crates/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#[macro_use]
extern crate log;

extern crate alloc;

type IndexMap<K, V> = indexmap::IndexMap<K, V, ahash::RandomState>;
type IndexSet<T> = indexmap::IndexSet<T, ahash::RandomState>;

Expand Down
4 changes: 2 additions & 2 deletions crates/codegen/src/string_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! after ruff has already successfully parsed the string literal, meaning
//! we don't need to do any validation or error handling.

use std::convert::Infallible;
use core::convert::Infallible;

use ruff_python_ast::{AnyStringFlags, StringFlags};
use rustpython_wtf8::{CodePoint, Wtf8, Wtf8Buf};
Expand Down Expand Up @@ -96,7 +96,7 @@ impl StringParser {
}

// OK because radix_bytes is always going to be in the ASCII range.
let radix_str = std::str::from_utf8(&radix_bytes[..len]).expect("ASCII bytes");
let radix_str = core::str::from_utf8(&radix_bytes[..len]).expect("ASCII bytes");
let value = u32::from_str_radix(radix_str, 8).unwrap();
char::from_u32(value).unwrap()
}
Expand Down
12 changes: 6 additions & 6 deletions crates/codegen/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
IndexMap,
error::{CodegenError, CodegenErrorType},
};
use alloc::{borrow::Cow, fmt};
use bitflags::bitflags;
use ruff_python_ast::{
self as ast, Comprehension, Decorator, Expr, Identifier, ModExpression, ModModule, Parameter,
Expand All @@ -20,7 +21,6 @@ use ruff_python_ast::{
};
use ruff_text_size::{Ranged, TextRange};
use rustpython_compiler_core::{PositionEncoding, SourceFile, SourceLocation};
use std::{borrow::Cow, fmt};

/// Captures all symbols in the current scope, and has a list of sub-scopes in this scope.
#[derive(Clone)]
Expand Down Expand Up @@ -215,8 +215,8 @@ impl SymbolTableError {

type SymbolTableResult<T = ()> = Result<T, SymbolTableError>;

impl std::fmt::Debug for SymbolTable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Debug for SymbolTable {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"SymbolTable({:?} symbols, {:?} sub scopes)",
Expand Down Expand Up @@ -261,8 +261,8 @@ fn drop_class_free(symbol_table: &mut SymbolTable) {
type SymbolMap = IndexMap<String, Symbol>;

mod stack {
use core::ptr::NonNull;
use std::panic;
use std::ptr::NonNull;
pub struct StackStack<T> {
v: Vec<NonNull<T>>,
}
Expand Down Expand Up @@ -325,7 +325,7 @@ struct SymbolTableAnalyzer {

impl SymbolTableAnalyzer {
fn analyze_symbol_table(&mut self, symbol_table: &mut SymbolTable) -> SymbolTableResult {
let symbols = std::mem::take(&mut symbol_table.symbols);
let symbols = core::mem::take(&mut symbol_table.symbols);
let sub_tables = &mut *symbol_table.sub_tables;

let mut info = (symbols, symbol_table.typ);
Expand Down Expand Up @@ -689,7 +689,7 @@ impl SymbolTableBuilder {
fn leave_scope(&mut self) {
let mut table = self.tables.pop().unwrap();
// Save the collected varnames to the symbol table
table.varnames = std::mem::take(&mut self.current_varnames);
table.varnames = core::mem::take(&mut self.current_varnames);
self.tables.last_mut().unwrap().sub_tables.push(table);
}

Expand Down
9 changes: 5 additions & 4 deletions crates/codegen/src/unparse.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use alloc::fmt;
use core::fmt::Display as _;
use ruff_python_ast::{
self as ruff, Arguments, BoolOp, Comprehension, ConversionFlag, Expr, Identifier, Operator,
Parameter, ParameterWithDefault, Parameters,
};
use ruff_text_size::Ranged;
use rustpython_compiler_core::SourceFile;
use rustpython_literal::escape::{AsciiEscape, UnicodeEscape};
use std::fmt::{self, Display as _};

mod precedence {
macro_rules! precedence {
Expand Down Expand Up @@ -51,7 +52,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
}

fn p_delim(&mut self, first: &mut bool, s: &str) -> fmt::Result {
self.p_if(!std::mem::take(first), s)
self.p_if(!core::mem::take(first), s)
}

fn write_fmt(&mut self, f: fmt::Arguments<'_>) -> fmt::Result {
Expand Down Expand Up @@ -575,7 +576,7 @@ impl<'a, 'b, 'c> Unparser<'a, 'b, 'c> {
if conversion != ConversionFlag::None {
self.p("!")?;
let buf = &[conversion as u8];
let c = std::str::from_utf8(buf).unwrap();
let c = core::str::from_utf8(buf).unwrap();
self.p(c)?;
}

Expand Down Expand Up @@ -650,7 +651,7 @@ impl fmt::Display for UnparseExpr<'_> {
}

fn to_string_fmt(f: impl FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result) -> String {
use std::cell::Cell;
use core::cell::Cell;
struct Fmt<F>(Cell<Option<F>>);
impl<F: FnOnce(&mut fmt::Formatter<'_>) -> fmt::Result> fmt::Display for Fmt<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
6 changes: 2 additions & 4 deletions crates/common/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use crate::lock::{
MapImmutable, PyImmutableMappedMutexGuard, PyMappedMutexGuard, PyMappedRwLockReadGuard,
PyMappedRwLockWriteGuard, PyMutexGuard, PyRwLockReadGuard, PyRwLockWriteGuard,
};
use std::{
fmt,
ops::{Deref, DerefMut},
};
use alloc::fmt;
use core::ops::{Deref, DerefMut};

macro_rules! impl_from {
($lt:lifetime, $gen:ident, $t:ty, $($var:ident($from:ty),)*) => {
Expand Down
12 changes: 6 additions & 6 deletions crates/common/src/boxvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
//! An unresizable vector backed by a `Box<[T]>`

#![allow(clippy::needless_lifetimes)]

use std::{
use alloc::{fmt, slice};
use core::{
borrow::{Borrow, BorrowMut},
cmp, fmt,
cmp,
mem::{self, MaybeUninit},
ops::{Bound, Deref, DerefMut, RangeBounds},
ptr, slice,
ptr,
};

pub struct BoxVec<T> {
Expand Down Expand Up @@ -555,7 +555,7 @@ impl<T> Extend<T> for BoxVec<T> {
};
let mut iter = iter.into_iter();
loop {
if std::ptr::eq(ptr, end_ptr) {
if core::ptr::eq(ptr, end_ptr) {
break;
}
if let Some(elt) = iter.next() {
Expand Down Expand Up @@ -693,7 +693,7 @@ impl<T> CapacityError<T> {

const CAPERROR: &str = "insufficient capacity";

impl<T> std::error::Error for CapacityError<T> {}
impl<T> core::error::Error for CapacityError<T> {}

impl<T> fmt::Display for CapacityError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down
15 changes: 8 additions & 7 deletions crates/common/src/cformat.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
//! Implementation of Printf-Style string formatting
//! as per the [Python Docs](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting).
use alloc::fmt;
use bitflags::bitflags;
use core::{
cmp,
iter::{Enumerate, Peekable},
str::FromStr,
};
use itertools::Itertools;
use malachite_bigint::{BigInt, Sign};
use num_traits::Signed;
use rustpython_literal::{float, format::Case};
use std::{
cmp, fmt,
iter::{Enumerate, Peekable},
str::FromStr,
};

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

Expand Down Expand Up @@ -785,7 +786,7 @@ impl<S> CFormatStrOrBytes<S> {
if !literal.is_empty() {
parts.push((
part_index,
CFormatPart::Literal(std::mem::take(&mut literal)),
CFormatPart::Literal(core::mem::take(&mut literal)),
));
}
let spec = CFormatSpecKeyed::parse(iter).map_err(|err| CFormatError {
Expand Down Expand Up @@ -816,7 +817,7 @@ impl<S> CFormatStrOrBytes<S> {

impl<S> IntoIterator for CFormatStrOrBytes<S> {
type Item = (usize, CFormatPart<S>);
type IntoIter = std::vec::IntoIter<Self::Item>;
type IntoIter = alloc::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
self.parts.into_iter()
Expand Down
4 changes: 3 additions & 1 deletion crates/common/src/crt_fd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! A module implementing an io type backed by the C runtime's file descriptors, i.e. what's
//! returned from libc::open, even on windows.

use std::{cmp, ffi, fmt, io};
use alloc::fmt;
use core::cmp;
use std::{ffi, io};

#[cfg(not(windows))]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/encodings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{self, Range};
use core::ops::{self, Range};

use num_traits::ToPrimitive;

Expand Down Expand Up @@ -260,7 +260,7 @@ pub mod errors {
use crate::str::UnicodeEscapeCodepoint;

use super::*;
use std::fmt::Write;
use core::fmt::Write;

pub struct Strict;

Expand Down
10 changes: 5 additions & 5 deletions crates/common/src/fileutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use windows::{StatStruct, fstat};

#[cfg(not(windows))]
pub fn fstat(fd: crate::crt_fd::Borrowed<'_>) -> std::io::Result<StatStruct> {
let mut stat = std::mem::MaybeUninit::uninit();
let mut stat = core::mem::MaybeUninit::uninit();
unsafe {
let ret = libc::fstat(fd.as_raw(), stat.as_mut_ptr());
if ret == -1 {
Expand Down Expand Up @@ -165,7 +165,7 @@ pub mod windows {
}

fn file_time_to_time_t_nsec(in_ptr: &FILETIME) -> (libc::time_t, libc::c_int) {
let in_val: i64 = unsafe { std::mem::transmute_copy(in_ptr) };
let in_val: i64 = unsafe { core::mem::transmute_copy(in_ptr) };
let nsec_out = (in_val % 10_000_000) * 100; // FILETIME is in units of 100 nsec.
let time_out = (in_val / 10_000_000) - SECS_BETWEEN_EPOCHS;
(time_out, nsec_out as _)
Expand Down Expand Up @@ -204,7 +204,7 @@ pub mod windows {
let st_nlink = info.nNumberOfLinks as i32;

let st_ino = if let Some(id_info) = id_info {
let file_id: [u64; 2] = unsafe { std::mem::transmute_copy(&id_info.FileId) };
let file_id: [u64; 2] = unsafe { core::mem::transmute_copy(&id_info.FileId) };
file_id
} else {
let ino = ((info.nFileIndexHigh as u64) << 32) + info.nFileIndexLow as u64;
Expand Down Expand Up @@ -313,7 +313,7 @@ pub mod windows {
unsafe { GetProcAddress(module, name.as_bytes_with_nul().as_ptr()) }
{
Some(unsafe {
std::mem::transmute::<
core::mem::transmute::<
unsafe extern "system" fn() -> isize,
unsafe extern "system" fn(
*const u16,
Expand Down Expand Up @@ -441,7 +441,7 @@ pub mod windows {
// Open a file using std::fs::File and convert to FILE*
// Automatically handles path encoding and EINTR retries
pub fn fopen(path: &std::path::Path, mode: &str) -> std::io::Result<*mut libc::FILE> {
use std::ffi::CString;
use alloc::ffi::CString;
use std::fs::File;

// Currently only supports read mode
Expand Down
Loading
Loading