Skip to content

Commit b49de3d

Browse files
reject pointee without ?Sized
1 parent e7f89a7 commit b49de3d

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

compiler/rustc_builtin_macros/src/deriving/smart_ptr.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ pub fn expand_deriving_smart_ptr(
154154
{
155155
let pointee = &mut impl_generics.params[pointee_param_idx];
156156
self_bounds = pointee.bounds.clone();
157+
if !contains_maybe_sized_bound(&self_bounds)
158+
&& !contains_maybe_sized_bound_on_pointee(
159+
&generics.where_clause.predicates,
160+
pointee_ty_ident.name,
161+
)
162+
{
163+
cx.dcx()
164+
.struct_span_err(
165+
pointee_ty_ident.span,
166+
format!(
167+
"`SmartPointer` is meaningless because `{}` is not `?Sized`",
168+
pointee_ty_ident.name
169+
),
170+
)
171+
.emit();
172+
return;
173+
}
157174
let arg = GenericArg::Type(s_ty.clone());
158175
let unsize = cx.path_all(span, true, path!(span, core::marker::Unsize), vec![arg]);
159176
pointee.bounds.push(cx.trait_bound(unsize, false));
@@ -218,23 +235,6 @@ pub fn expand_deriving_smart_ptr(
218235
//
219236
// We now insert `__S` with the missing bounds marked with (*) above.
220237
// We should also write the bounds from `#[pointee]` to `__S` as required by `Unsize<__S>`.
221-
let sized = cx.path_global(span, path!(span, core::marker::Sized));
222-
// For some reason, we are not allowed to write `?Sized` bound twice like `__S: ?Sized + ?Sized`.
223-
if !contains_maybe_sized_bound(&self_bounds)
224-
&& !contains_maybe_sized_bound_on_pointee(
225-
&generics.where_clause.predicates,
226-
pointee_ty_ident.name,
227-
)
228-
{
229-
self_bounds.push(GenericBound::Trait(
230-
cx.poly_trait_ref(span, sized),
231-
TraitBoundModifiers {
232-
polarity: ast::BoundPolarity::Maybe(span),
233-
constness: ast::BoundConstness::Never,
234-
asyncness: ast::BoundAsyncness::Normal,
235-
},
236-
));
237-
}
238238
{
239239
let mut substitution =
240240
TypeSubstitution { from_name: pointee_ty_ident.name, to_ty: &s_ty, rewritten: false };

tests/ui/deriving/deriving-smart-pointer-neg.rs

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ struct NotTransparent<'a, #[pointee] T: ?Sized> {
3535
ptr: &'a T,
3636
}
3737

38+
#[derive(SmartPointer)]
39+
#[repr(transparent)]
40+
struct NoMaybeSized<'a, #[pointee] T> {
41+
//~^ ERROR: SmartPointer` is meaningless because `T` is not `?Sized
42+
ptr: &'a T,
43+
}
44+
3845
// However, reordering attributes should work nevertheless.
3946
#[repr(transparent)]
4047
#[derive(SmartPointer)]

tests/ui/deriving/deriving-smart-pointer-neg.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ LL | #[derive(SmartPointer)]
3838
|
3939
= note: this error originates in the derive macro `SmartPointer` (in Nightly builds, run with -Z macro-backtrace for more info)
4040

41+
error: `SmartPointer` is meaningless because `T` is not `?Sized`
42+
--> $DIR/deriving-smart-pointer-neg.rs:40:36
43+
|
44+
LL | struct NoMaybeSized<'a, #[pointee] T> {
45+
| ^
46+
4147
error[E0392]: lifetime parameter `'a` is never used
4248
--> $DIR/deriving-smart-pointer-neg.rs:21:16
4349
|
@@ -70,6 +76,6 @@ LL | struct NoFieldUnit<'a, #[pointee] T: ?Sized>();
7076
|
7177
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
7278

73-
error: aborting due to 9 previous errors
79+
error: aborting due to 10 previous errors
7480

7581
For more information about this error, try `rustc --explain E0392`.

0 commit comments

Comments
 (0)