22
33use std:: sync:: Arc ;
44
5+ use crate :: error:: Span ;
6+
57/// Implementations for newtypes that wrap [`Arc<str>`].
68macro_rules! wrapped_string {
79 ( $wrapper: ident, $name: expr) => {
@@ -16,7 +18,7 @@ macro_rules! wrapped_string {
1618 #[ doc = "## Panics\n \n " ]
1719 #[ doc = "Panics may occur down the line if the precondition is not satisfied." ]
1820 pub fn from_str_unchecked( s: & str ) -> Self {
19- Self ( Arc :: from( s) )
21+ Self ( Arc :: from( s) , Span :: new ( 0 , 0 ) )
2022 }
2123
2224 /// Access the inner string.
@@ -26,7 +28,17 @@ macro_rules! wrapped_string {
2628
2729 /// Make a cheap copy of the name.
2830 pub fn shallow_clone( & self ) -> Self {
29- Self ( Arc :: clone( & self . 0 ) )
31+ Self ( Arc :: clone( & self . 0 ) , self . 1 )
32+ }
33+
34+ /// Add span to the name.
35+ pub fn with_span( self , span: Span ) -> Self {
36+ Self ( self . 0 , span)
37+ }
38+
39+ /// Access span of the name.
40+ pub fn span( & self ) -> Span {
41+ self . 1
3042 }
3143 }
3244
@@ -41,6 +53,20 @@ macro_rules! wrapped_string {
4153 std:: fmt:: Display :: fmt( & self . 0 , f)
4254 }
4355 }
56+
57+ impl PartialEq for $wrapper {
58+ fn eq( & self , other: & Self ) -> bool {
59+ self . 0 == other. 0
60+ }
61+ }
62+
63+ impl Eq for $wrapper { }
64+
65+ impl std:: hash:: Hash for $wrapper {
66+ fn hash<H : std:: hash:: Hasher >( & self , state: & mut H ) {
67+ self . 0 . hash( state) ;
68+ }
69+ }
4470 } ;
4571}
4672
@@ -68,13 +94,13 @@ macro_rules! impl_arbitrary_lowercase_alpha {
6894}
6995
7096/// The name of a function.
71- #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
72- pub struct FunctionName ( Arc < str > ) ;
97+ #[ derive( Clone , Ord , PartialOrd ) ]
98+ pub struct FunctionName ( Arc < str > , Span ) ;
7399
74100impl FunctionName {
75101 /// Return the name of the main function.
76102 pub fn main ( ) -> Self {
77- Self ( Arc :: from ( "main" ) )
103+ Self :: from_str_unchecked ( "main" )
78104 }
79105}
80106
@@ -112,22 +138,22 @@ impl<'a> arbitrary::Arbitrary<'a> for FunctionName {
112138}
113139
114140/// The identifier of a variable.
115- #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
116- pub struct Identifier ( Arc < str > ) ;
141+ #[ derive( Clone , Ord , PartialOrd ) ]
142+ pub struct Identifier ( Arc < str > , Span ) ;
117143
118144wrapped_string ! ( Identifier , "variable identifier" ) ;
119145impl_arbitrary_lowercase_alpha ! ( Identifier ) ;
120146
121147/// The name of a witness.
122- #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
123- pub struct WitnessName ( Arc < str > ) ;
148+ #[ derive( Clone , Ord , PartialOrd ) ]
149+ pub struct WitnessName ( Arc < str > , Span ) ;
124150
125151wrapped_string ! ( WitnessName , "witness name" ) ;
126152impl_arbitrary_lowercase_alpha ! ( WitnessName ) ;
127153
128154/// The name of a jet.
129- #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
130- pub struct JetName ( Arc < str > ) ;
155+ #[ derive( Clone , Ord , PartialOrd ) ]
156+ pub struct JetName ( Arc < str > , Span ) ;
131157
132158wrapped_string ! ( JetName , "jet name" ) ;
133159
@@ -137,13 +163,13 @@ impl<'a> arbitrary::Arbitrary<'a> for JetName {
137163 u. choose ( & simplicity:: jet:: Elements :: ALL )
138164 . map ( simplicity:: jet:: Elements :: to_string)
139165 . map ( Arc :: from)
140- . map ( Self )
166+ . map ( |jet| Self ( jet , Span :: DUMMY ) )
141167 }
142168}
143169
144170/// The name of a type alias.
145- #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
146- pub struct AliasName ( Arc < str > ) ;
171+ #[ derive( Clone , Ord , PartialOrd ) ]
172+ pub struct AliasName ( Arc < str > , Span ) ;
147173
148174wrapped_string ! ( AliasName , "name of a type alias" ) ;
149175
@@ -205,8 +231,8 @@ impl<'a> arbitrary::Arbitrary<'a> for AliasName {
205231}
206232
207233/// A string of decimal digits.
208- #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
209- pub struct Decimal ( Arc < str > ) ;
234+ #[ derive( Clone , Ord , PartialOrd ) ]
235+ pub struct Decimal ( Arc < str > , Span ) ;
210236
211237wrapped_string ! ( Decimal , "decimal string" ) ;
212238
@@ -224,8 +250,8 @@ impl<'a> arbitrary::Arbitrary<'a> for Decimal {
224250}
225251
226252/// A string of binary digits.
227- #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
228- pub struct Binary ( Arc < str > ) ;
253+ #[ derive( Clone , Ord , PartialOrd ) ]
254+ pub struct Binary ( Arc < str > , Span ) ;
229255
230256wrapped_string ! ( Binary , "binary string" ) ;
231257
@@ -244,8 +270,8 @@ impl<'a> arbitrary::Arbitrary<'a> for Binary {
244270}
245271
246272/// A string of hexadecimal digits.
247- #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
248- pub struct Hexadecimal ( Arc < str > ) ;
273+ #[ derive( Clone , Ord , PartialOrd ) ]
274+ pub struct Hexadecimal ( Arc < str > , Span ) ;
249275
250276wrapped_string ! ( Hexadecimal , "hexadecimal string" ) ;
251277
@@ -268,18 +294,18 @@ impl<'a> arbitrary::Arbitrary<'a> for Hexadecimal {
268294}
269295
270296/// The name of a module.
271- #[ derive( Clone , Ord , PartialOrd , Eq , PartialEq , Hash ) ]
272- pub struct ModuleName ( Arc < str > ) ;
297+ #[ derive( Clone , Ord , PartialOrd ) ]
298+ pub struct ModuleName ( Arc < str > , Span ) ;
273299
274300impl ModuleName {
275301 /// Return the name of the witness module.
276302 pub fn witness ( ) -> Self {
277- Self ( Arc :: from ( "witness" ) )
303+ Self :: from_str_unchecked ( "witness" )
278304 }
279305
280306 /// Return the name of the parameter module.
281307 pub fn param ( ) -> Self {
282- Self ( Arc :: from ( "param" ) )
308+ Self :: from_str_unchecked ( "param" )
283309 }
284310}
285311
0 commit comments