Skip to content

Commit b12575f

Browse files
committed
Fix DISALLOW_INSTANTIATION
1 parent 73a8dd0 commit b12575f

File tree

25 files changed

+137
-144
lines changed

25 files changed

+137
-144
lines changed

crates/derive-impl/src/pyclass.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,10 @@ where
954954
} else if let Ok(f) = args.item.function_or_method() {
955955
(&f.sig().ident, f.span())
956956
} else {
957-
return Err(self.new_syn_error(args.item.span(), "can only be on a method or const function pointer"));
957+
return Err(self.new_syn_error(
958+
args.item.span(),
959+
"can only be on a method or const function pointer",
960+
));
958961
};
959962

960963
let item_attr = args.attrs.remove(self.index());
@@ -1636,7 +1639,7 @@ fn extract_impl_attrs(attr: PunctuatedNestedMeta, item: &Ident) -> Result<Extrac
16361639
"Try `#[pyclass(with(Constructor, ...))]` instead of `#[pyclass(with(DefaultConstructor, ...))]`. DefaultConstructor implicitly implements Constructor."
16371640
)
16381641
}
1639-
if path.is_ident("Constructor") || path.is_ident("Unconstructible") {
1642+
if path.is_ident("Constructor") {
16401643
has_constructor = true;
16411644
}
16421645
(
@@ -1691,11 +1694,7 @@ fn extract_impl_attrs(attr: PunctuatedNestedMeta, item: &Ident) -> Result<Extrac
16911694
attr => bail_span!(attr, "Unknown pyimpl attribute"),
16921695
}
16931696
}
1694-
// TODO: DISALLOW_INSTANTIATION check is required
16951697
let _ = has_constructor;
1696-
// if !withs.is_empty() && !has_constructor {
1697-
// bail_span!(item, "#[pyclass(with(...))] does not have a Constructor. Either #[pyclass(with(Constructor, ...))] or #[pyclass(with(Unconstructible, ...))] is mandatory. Consider to add `impl DefaultConstructor for T {{}}` or `impl Unconstructible for T {{}}`.")
1698-
// }
16991698

17001699
Ok(ExtractedImplAttrs {
17011700
payload,

crates/stdlib/src/pystruct.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) mod _struct {
1616
function::{ArgBytesLike, ArgMemoryBuffer, PosArgs},
1717
match_class,
1818
protocol::PyIterReturn,
19-
types::{Constructor, IterNext, Iterable, Representable, SelfIter, Unconstructible},
19+
types::{Constructor, IterNext, Iterable, Representable, SelfIter},
2020
};
2121
use crossbeam_utils::atomic::AtomicCell;
2222

@@ -197,7 +197,7 @@ pub(crate) mod _struct {
197197
}
198198
}
199199
impl SelfIter for UnpackIterator {}
200-
impl Unconstructible for UnpackIterator {}
200+
201201
impl IterNext for UnpackIterator {
202202
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {
203203
let size = zelf.format_spec.size;

crates/stdlib/src/sqlite.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ mod _sqlite {
7575
sliceable::{SaturatedSliceIter, SliceableSequenceOp},
7676
types::{
7777
AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Hashable,
78-
Initializer, IterNext, Iterable, PyComparisonOp, SelfIter, Unconstructible,
78+
Initializer, IterNext, Iterable, PyComparisonOp, SelfIter,
7979
},
8080
utils::ToCString,
8181
};
@@ -2197,8 +2197,6 @@ mod _sqlite {
21972197
inner: PyMutex<Option<BlobInner>>,
21982198
}
21992199

2200-
impl Unconstructible for Blob {}
2201-
22022200
#[derive(Debug)]
22032201
struct BlobInner {
22042202
blob: SqliteBlob,
@@ -2211,7 +2209,7 @@ mod _sqlite {
22112209
}
22122210
}
22132211

2214-
#[pyclass(with(AsMapping, Unconstructible, AsNumber, AsSequence))]
2212+
#[pyclass(flags(DISALLOW_INSTANTIATION), with(AsMapping, AsNumber, AsSequence))]
22152213
impl Blob {
22162214
#[pymethod]
22172215
fn close(&self) {
@@ -2592,9 +2590,7 @@ mod _sqlite {
25922590
}
25932591
}
25942592

2595-
impl Unconstructible for Statement {}
2596-
2597-
#[pyclass(with(Unconstructible))]
2593+
#[pyclass(flags(DISALLOW_INSTANTIATION))]
25982594
impl Statement {
25992595
fn new(
26002596
connection: &Connection,

crates/vm/src/builtins/asyncgenerator.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
frame::FrameRef,
99
function::OptionalArg,
1010
protocol::PyIterReturn,
11-
types::{IterNext, Iterable, Representable, SelfIter, Unconstructible},
11+
types::{IterNext, Iterable, Representable, SelfIter},
1212
};
1313

1414
use crossbeam_utils::atomic::AtomicCell;
@@ -32,7 +32,7 @@ impl PyPayload for PyAsyncGen {
3232
}
3333
}
3434

35-
#[pyclass(with(PyRef, Unconstructible, Representable))]
35+
#[pyclass(flags(DISALLOW_INSTANTIATION), with(PyRef, Representable))]
3636
impl PyAsyncGen {
3737
pub const fn as_coro(&self) -> &Coro {
3838
&self.inner
@@ -201,8 +201,6 @@ impl Representable for PyAsyncGen {
201201
}
202202
}
203203

204-
impl Unconstructible for PyAsyncGen {}
205-
206204
#[pyclass(module = false, name = "async_generator_wrapped_value")]
207205
#[derive(Debug)]
208206
pub(crate) struct PyAsyncGenWrappedValue(pub PyObjectRef);

crates/vm/src/builtins/builtin_func.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
common::wtf8::Wtf8,
66
convert::TryFromObject,
77
function::{FuncArgs, PyComparisonValue, PyMethodDef, PyMethodFlags, PyNativeFn},
8-
types::{Callable, Comparable, PyComparisonOp, Representable, Unconstructible},
8+
types::{Callable, Comparable, PyComparisonOp, Representable},
99
};
1010
use std::fmt;
1111

@@ -74,7 +74,7 @@ impl Callable for PyNativeFunction {
7474
}
7575
}
7676

77-
#[pyclass(with(Callable, Unconstructible), flags(HAS_DICT))]
77+
#[pyclass(with(Callable), flags(HAS_DICT, DISALLOW_INSTANTIATION))]
7878
impl PyNativeFunction {
7979
#[pygetset]
8080
fn __module__(zelf: NativeFunctionOrMethod) -> Option<&'static PyStrInterned> {
@@ -145,8 +145,6 @@ impl Representable for PyNativeFunction {
145145
}
146146
}
147147

148-
impl Unconstructible for PyNativeFunction {}
149-
150148
// `PyCMethodObject` in CPython
151149
#[pyclass(name = "builtin_method", module = false, base = PyNativeFunction, ctx = "builtin_method_type")]
152150
pub struct PyNativeMethod {
@@ -155,8 +153,8 @@ pub struct PyNativeMethod {
155153
}
156154

157155
#[pyclass(
158-
with(Unconstructible, Callable, Comparable, Representable),
159-
flags(HAS_DICT)
156+
with(Callable, Comparable, Representable),
157+
flags(HAS_DICT, DISALLOW_INSTANTIATION)
160158
)]
161159
impl PyNativeMethod {
162160
#[pygetset]
@@ -246,8 +244,6 @@ impl Representable for PyNativeMethod {
246244
}
247245
}
248246

249-
impl Unconstructible for PyNativeMethod {}
250-
251247
pub fn init(context: &Context) {
252248
PyNativeFunction::extend_class(context, context.types.builtin_function_or_method_type);
253249
PyNativeMethod::extend_class(context, context.types.builtin_method_type);

crates/vm/src/builtins/bytearray.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::{
3333
types::{
3434
AsBuffer, AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor,
3535
DefaultConstructor, Initializer, IterNext, Iterable, PyComparisonOp, Representable,
36-
SelfIter, Unconstructible,
36+
SelfIter,
3737
},
3838
};
3939
use bstr::ByteSlice;
@@ -865,7 +865,7 @@ impl PyPayload for PyByteArrayIterator {
865865
}
866866
}
867867

868-
#[pyclass(with(Unconstructible, IterNext, Iterable))]
868+
#[pyclass(flags(DISALLOW_INSTANTIATION), with(IterNext, Iterable))]
869869
impl PyByteArrayIterator {
870870
#[pymethod]
871871
fn __length_hint__(&self) -> usize {
@@ -886,8 +886,6 @@ impl PyByteArrayIterator {
886886
}
887887
}
888888

889-
impl Unconstructible for PyByteArrayIterator {}
890-
891889
impl SelfIter for PyByteArrayIterator {}
892890
impl IterNext for PyByteArrayIterator {
893891
fn next(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<PyIterReturn> {

crates/vm/src/builtins/bytes.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
sliceable::{SequenceIndex, SliceableSequenceOp},
2626
types::{
2727
AsBuffer, AsMapping, AsNumber, AsSequence, Callable, Comparable, Constructor, Hashable,
28-
IterNext, Iterable, PyComparisonOp, Representable, SelfIter, Unconstructible,
28+
IterNext, Iterable, PyComparisonOp, Representable, SelfIter,
2929
},
3030
};
3131
use bstr::ByteSlice;
@@ -749,7 +749,7 @@ impl PyPayload for PyBytesIterator {
749749
}
750750
}
751751

752-
#[pyclass(with(Unconstructible, IterNext, Iterable))]
752+
#[pyclass(flags(DISALLOW_INSTANTIATION), with(IterNext, Iterable))]
753753
impl PyBytesIterator {
754754
#[pymethod]
755755
fn __length_hint__(&self) -> usize {
@@ -770,7 +770,6 @@ impl PyBytesIterator {
770770
.set_state(state, |obj, pos| pos.min(obj.len()), vm)
771771
}
772772
}
773-
impl Unconstructible for PyBytesIterator {}
774773

775774
impl SelfIter for PyBytesIterator {}
776775
impl IterNext for PyBytesIterator {

crates/vm/src/builtins/coroutine.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
frame::FrameRef,
77
function::OptionalArg,
88
protocol::PyIterReturn,
9-
types::{IterNext, Iterable, Representable, SelfIter, Unconstructible},
9+
types::{IterNext, Iterable, Representable, SelfIter},
1010
};
1111
use crossbeam_utils::atomic::AtomicCell;
1212

@@ -24,7 +24,7 @@ impl PyPayload for PyCoroutine {
2424
}
2525
}
2626

27-
#[pyclass(with(Py, Unconstructible, IterNext, Representable))]
27+
#[pyclass(flags(DISALLOW_INSTANTIATION), with(Py, IterNext, Representable))]
2828
impl PyCoroutine {
2929
pub const fn as_coro(&self) -> &Coro {
3030
&self.inner
@@ -123,8 +123,6 @@ impl Py<PyCoroutine> {
123123
}
124124
}
125125

126-
impl Unconstructible for PyCoroutine {}
127-
128126
impl Representable for PyCoroutine {
129127
#[inline]
130128
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {

crates/vm/src/builtins/descriptor.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
builtins::{PyTypeRef, builtin_func::PyNativeMethod, type_},
55
class::PyClassImpl,
66
function::{FuncArgs, PyMethodDef, PyMethodFlags, PySetterValue},
7-
types::{Callable, GetDescriptor, Representable, Unconstructible},
7+
types::{Callable, GetDescriptor, Representable},
88
};
99
use rustpython_common::lock::PyRwLock;
1010

@@ -105,8 +105,8 @@ impl PyMethodDescriptor {
105105
}
106106

107107
#[pyclass(
108-
with(GetDescriptor, Callable, Unconstructible, Representable),
109-
flags(METHOD_DESCRIPTOR)
108+
with(GetDescriptor, Callable, Representable),
109+
flags(METHOD_DESCRIPTOR, DISALLOW_INSTANTIATION)
110110
)]
111111
impl PyMethodDescriptor {
112112
#[pygetset]
@@ -159,8 +159,6 @@ impl Representable for PyMethodDescriptor {
159159
}
160160
}
161161

162-
impl Unconstructible for PyMethodDescriptor {}
163-
164162
#[derive(Debug)]
165163
pub enum MemberKind {
166164
Bool = 14,
@@ -246,7 +244,10 @@ fn calculate_qualname(descr: &PyDescriptorOwned, vm: &VirtualMachine) -> PyResul
246244
}
247245
}
248246

249-
#[pyclass(with(GetDescriptor, Unconstructible, Representable), flags(BASETYPE))]
247+
#[pyclass(
248+
with(GetDescriptor, Representable),
249+
flags(BASETYPE, DISALLOW_INSTANTIATION)
250+
)]
250251
impl PyMemberDescriptor {
251252
#[pygetset]
252253
fn __doc__(&self) -> Option<String> {
@@ -339,8 +340,6 @@ fn set_slot_at_object(
339340
Ok(())
340341
}
341342

342-
impl Unconstructible for PyMemberDescriptor {}
343-
344343
impl Representable for PyMemberDescriptor {
345344
#[inline]
346345
fn repr_str(zelf: &Py<Self>, _vm: &VirtualMachine) -> PyResult<String> {

0 commit comments

Comments
 (0)