-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Add Complex type and repr(complex)
#158885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
68550af
7b7f7f6
047bd9c
f3ece34
00b6500
bd32164
9f87e3b
5cf9142
a9586bb
fac1794
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,6 +113,8 @@ fn check_struct(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGuarante | |
| check_scalable_vector(tcx, span, def_id, scalable); | ||
| } else if def.repr().simd() { | ||
| check_simd(tcx, span, def_id); | ||
| } else if def.repr().complex() { | ||
| check_complex(tcx, span, def_id); | ||
| } | ||
|
|
||
| check_transparent(tcx, def); | ||
|
|
@@ -1527,6 +1529,46 @@ fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) { | |
| } | ||
| } | ||
|
|
||
| fn check_complex(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) { | ||
| let t = tcx.type_of(def_id).instantiate_identity().skip_norm_wip(); | ||
| let ty::Adt(def, args) = t.kind() else { return }; | ||
|
|
||
| // This constraint is enforced during attribute parsing. | ||
| assert!(def.is_struct(), "`repr(complex)` is only allowed on structs"); | ||
|
|
||
| let fields = &def.non_enum_variant().fields; | ||
|
|
||
| // A complex number is a pair, so we require exactly two fields of the same type. | ||
| let (Some(first), Some(second)) = (fields.get(FieldIdx::ZERO), fields.get(FieldIdx::ONE)) | ||
| else { | ||
| tcx.dcx().struct_span_err(sp, "`repr(complex)` type must have two fields").emit(); | ||
| return; | ||
| }; | ||
|
|
||
| if let Some(third) = fields.get(FieldIdx::from_usize(2)) { | ||
| tcx.dcx() | ||
| .struct_span_err(sp, "`repr(complex)` type cannot have more than two fields") | ||
| .with_span_label(tcx.def_span(third.did), "excess field") | ||
| .emit(); | ||
| return; | ||
| } | ||
|
|
||
| // repr(complex) is internal, so this check is not as thorough as we'd require if it were | ||
| // user-facing. | ||
|
Comment on lines
+1556
to
+1557
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lmk if we should be more thorough here, but given that we never want to stabilize |
||
| let first_ty = first.ty(tcx, args).skip_norm_wip(); | ||
| let second_ty = second.ty(tcx, args).skip_norm_wip(); | ||
| if first_ty != second_ty { | ||
| tcx.dcx() | ||
| .struct_span_err(sp, "`repr(complex)` type must have two fields of the same type") | ||
| .with_span_label(tcx.def_span(first.did), format!("this field is of type `{first_ty}`")) | ||
| .with_span_label( | ||
| tcx.def_span(second.did), | ||
| format!("this field is of type `{second_ty}`"), | ||
| ) | ||
| .emit(); | ||
| } | ||
| } | ||
|
|
||
| #[tracing::instrument(skip(tcx), level = "debug")] | ||
| fn check_scalable_vector(tcx: TyCtxt<'_>, span: Span, def_id: LocalDefId, scalable: ScalableElt) { | ||
| let ty = tcx.type_of(def_id).instantiate_identity().skip_norm_wip(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -635,7 +635,7 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { | |
| debug_assert!(matches!(def.adt_kind(), AdtKind::Struct | AdtKind::Union)); | ||
| use FfiResult::*; | ||
|
|
||
| if !def.repr().c() && !def.repr().transparent() { | ||
| if !def.repr().c() && !def.repr().transparent() && !def.repr().complex() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This likely needs some work, for example
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend we err conservatively and treat it as nonsense for now until anyone asks for it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, takes some more logic but we now only accept (transparently wrapped) float types, anything else triggers
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw it is my intention to make |
||
| return FfiUnsafe { | ||
| ty, | ||
| reason: if def.is_struct() { | ||
|
|
@@ -656,6 +656,38 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> { | |
| }; | ||
| } | ||
|
|
||
| // We only guarantee that Complex<{float}> is C-compatible for now. | ||
| if def.repr().complex() | ||
| && let Some(field) = def.non_enum_variant().fields.iter().next() | ||
| { | ||
| let mut field_ty = | ||
| maybe_normalize_erasing_regions(self.cx, field.ty(self.cx.tcx, args)); | ||
|
|
||
| // Peel off any transparent wrappers. | ||
| while let ty::Adt(inner, inner_args) = field_ty.kind() | ||
| && inner.repr().transparent() | ||
| && let Some(inner_field) = | ||
| super::transparent_newtype_field(self.cx.tcx, inner.non_enum_variant()) | ||
| { | ||
| field_ty = maybe_normalize_erasing_regions( | ||
| self.cx, | ||
| inner_field.ty(self.cx.tcx, inner_args), | ||
| ); | ||
| } | ||
|
|
||
| if !field_ty.is_floating_point() { | ||
| return FfiUnsafe { | ||
| ty, | ||
| reason: msg!( | ||
| "this `repr(complex)` struct only has a C-compatible layout with floating-point fields" | ||
| ), | ||
| help: Some(msg!( | ||
| "only `repr(complex)` structs whose fields are `f16`, `f32`, `f64`, or `f128` have a C-compatible layout" | ||
| )), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| if def.non_enum_variant().field_list_has_applicable_non_exhaustive() { | ||
| return FfiUnsafe { | ||
| ty, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /// A complex number. | ||
| #[derive(Clone, Copy, Debug, PartialEq)] | ||
| #[unstable(feature = "complex_numbers", issue = "154023")] | ||
| #[repr(complex)] | ||
| pub struct Complex<T> { | ||
| /// The real component. | ||
| pub re: T, | ||
| /// The imaginary component. | ||
| pub im: T, | ||
| } | ||
|
|
||
| #[unstable(feature = "complex_numbers", issue = "154023")] | ||
| impl<T> Complex<T> { | ||
| /// Create a new complex number from a real and imaginary component. | ||
| #[must_use] | ||
| pub fn new(re: T, im: T) -> Complex<T> { | ||
| Complex { re, im } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.