Skip to content

Commit c307dae

Browse files
committed
add span for AliasedType
This would allow make better error messages, which wouldn highlight only problematic parts
1 parent 42020ac commit c307dae

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

src/types.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use miniscript::iter::{Tree, TreeLike};
66
use simplicity::types::{CompleteBound, Final};
77

88
use crate::array::{BTreeSlice, Partition};
9+
use crate::error::Span;
910
use crate::num::{NonZeroPow2Usize, Pow2Usize};
1011
use crate::str::AliasName;
1112

@@ -486,7 +487,7 @@ impl<'a> arbitrary::Arbitrary<'a> for ResolvedType {
486487

487488
/// SimplicityHL type with type aliases.
488489
#[derive(PartialEq, Eq, Hash, Clone)]
489-
pub struct AliasedType(AliasedInner);
490+
pub struct AliasedType(AliasedInner, Span);
490491

491492
/// Type alias or primitive.
492493
///
@@ -548,14 +549,26 @@ impl AliasedType {
548549
}
549550
}
550551

552+
const fn new(inner: AliasedInner) -> Self {
553+
Self(inner, Span::new(0, 0))
554+
}
555+
556+
pub fn span(&self) -> Span {
557+
self.1
558+
}
559+
560+
pub fn with_span(self, span: Span) -> Self {
561+
Self(self.0, span)
562+
}
563+
551564
/// Create a type alias from the given `identifier`.
552565
pub const fn alias(name: AliasName) -> Self {
553-
Self(AliasedInner::Alias(name))
566+
Self::new(AliasedInner::Alias(name))
554567
}
555568

556569
/// Create a builtin type alias.
557570
pub const fn builtin(builtin: BuiltinAlias) -> Self {
558-
Self(AliasedInner::Builtin(builtin))
571+
Self::new(AliasedInner::Builtin(builtin))
559572
}
560573

561574
/// Resolve all aliases in the type based on the given map of `aliases` to types.
@@ -615,35 +628,35 @@ impl AliasedType {
615628

616629
impl TypeConstructible for AliasedType {
617630
fn either(left: Self, right: Self) -> Self {
618-
Self(AliasedInner::Inner(TypeInner::Either(
631+
Self::new(AliasedInner::Inner(TypeInner::Either(
619632
Arc::new(left),
620633
Arc::new(right),
621634
)))
622635
}
623636

624637
fn option(inner: Self) -> Self {
625-
Self(AliasedInner::Inner(TypeInner::Option(Arc::new(inner))))
638+
Self::new(AliasedInner::Inner(TypeInner::Option(Arc::new(inner))))
626639
}
627640

628641
fn boolean() -> Self {
629-
Self(AliasedInner::Inner(TypeInner::Boolean))
642+
Self::new(AliasedInner::Inner(TypeInner::Boolean))
630643
}
631644

632645
fn tuple<I: IntoIterator<Item = Self>>(elements: I) -> Self {
633-
Self(AliasedInner::Inner(TypeInner::Tuple(
646+
Self::new(AliasedInner::Inner(TypeInner::Tuple(
634647
elements.into_iter().map(Arc::new).collect(),
635648
)))
636649
}
637650

638651
fn array(element: Self, size: usize) -> Self {
639-
Self(AliasedInner::Inner(TypeInner::Array(
652+
Self::new(AliasedInner::Inner(TypeInner::Array(
640653
Arc::new(element),
641654
size,
642655
)))
643656
}
644657

645658
fn list(element: Self, bound: NonZeroPow2Usize) -> Self {
646-
Self(AliasedInner::Inner(TypeInner::List(
659+
Self::new(AliasedInner::Inner(TypeInner::List(
647660
Arc::new(element),
648661
bound,
649662
)))
@@ -737,7 +750,7 @@ impl fmt::Display for AliasedType {
737750

738751
impl From<UIntType> for AliasedType {
739752
fn from(value: UIntType) -> Self {
740-
Self(AliasedInner::Inner(TypeInner::UInt(value)))
753+
Self::new(AliasedInner::Inner(TypeInner::UInt(value)))
741754
}
742755
}
743756

0 commit comments

Comments
 (0)