@@ -1867,13 +1867,15 @@ impl<'a, 'ast> Generator<'a, 'ast> {
18671867
18681868 /// The place a generic class's static field is read and written at: its
18691869 /// entry in the shared array's registry, found by the closed type's key or
1870- /// appended (with the field's default) the first time any program asks.
1871- /// The registry is two parallel arrays — keys and values — kept in the two
1872- /// slots the layout reserves past the plain shared statics.
1870+ /// appended the first time any program asks — with the field's initializer
1871+ /// value (a constant literal) or its default. The registry is two parallel
1872+ /// arrays — keys and values — kept in the two slots the layout reserves
1873+ /// past the plain shared statics.
18731874 fn generic_static_place (
18741875 & mut self ,
18751876 ctx : & Ctx < ' ast > ,
18761877 field : SymbolId ,
1878+ declaring : & Type ,
18771879 key : & str ,
18781880 ty : Type ,
18791881 span : Range < usize > ,
@@ -1887,24 +1889,49 @@ impl<'a, 'ast> Generator<'a, 'ast> {
18871889 ) ;
18881890 return Place :: Error ;
18891891 }
1890- // an initializer would have to run once per closed type, which nothing
1891- // models yet; leaving it out would make `= 5` silently read 0, so this
1892- // is an error rather than a wrong value
1893- let has_initializer = self
1892+
1893+ // the closed type's arguments bind the class's own type parameters
1894+ // (`Cache<int>` gives T = int), so a field typed or initialized in
1895+ // terms of T resolves to the concrete type
1896+ let class = self . declarations . table . symbol ( field) . parent ;
1897+ let class_bindings: Vec < ( SymbolId , Type ) > = match ( class, declaring) {
1898+ ( Some ( class) , Type :: Named { arguments, .. } ) => self
1899+ . declarations
1900+ . table
1901+ . symbol ( class)
1902+ . type_parameters
1903+ . iter ( )
1904+ . copied ( )
1905+ . zip ( arguments. iter ( ) . cloned ( ) )
1906+ . collect ( ) ,
1907+ _ => Vec :: new ( ) ,
1908+ } ;
1909+ let ty = self . substitute ( & ty, & class_bindings) ;
1910+
1911+ // the initializer, if any. Only a constant literal is supported: a
1912+ // computed one (`= new List<T>()`, a call) would have to run once per
1913+ // closed type with a real call frame, which the inline append has not.
1914+ // A literal bakes to a heap value with no code, so it is safe here.
1915+ let written = self
18941916 . declarations
18951917 . table
18961918 . symbol ( field)
18971919 . declarations
18981920 . first ( )
1899- . is_some_and ( |site| {
1900- matches ! ( & site . syntax , SyntaxRef :: Field { declarator, .. }
1901- if declarator . initializer . is_some ( ) )
1921+ . and_then ( |site| match site . syntax {
1922+ SyntaxRef :: Field { declarator, .. } => declarator . initializer . as_ref ( ) ,
1923+ _ => None ,
19021924 } ) ;
1903- if has_initializer {
1925+ let udon = self . heap_type ( & ty) ;
1926+ let literal_init = match written {
1927+ Some ( InitializerValue :: Expression ( expression) ) => literal_heap_init ( expression, & udon) ,
1928+ _ => None ,
1929+ } ;
1930+ if written. is_some ( ) && literal_init. is_none ( ) {
19041931 self . error (
19051932 ctx,
1906- "a static field of a generic class cannot have an initializer yet (its value \
1907- would have to be built once per closed type ); leave it default "
1933+ "a static field of a generic class can only be initialized with a constant \
1934+ literal (= 5, = \" text \" , = true ); a computed initializer is not supported yet "
19081935 . to_string ( ) ,
19091936 span,
19101937 ) ;
@@ -1965,8 +1992,22 @@ impl<'a, 'ast> Generator<'a, 'ast> {
19651992 span. clone ( ) ,
19661993 ) ;
19671994 let new_keys = self . grow_object_array ( ctx, keys, length, grown, key_constant, span. clone ( ) ) ;
1968- let default = self . default_value ( & ty) ;
1969- let new_values = self . grow_object_array ( ctx, values, length, grown, default, span. clone ( ) ) ;
1995+ // the field's initial value: its constant-literal initializer, baked to
1996+ // a heap slot with no code (safe to place directly), or its default
1997+ let tail = match literal_init {
1998+ Some ( init) => {
1999+ let serial = self . program . data . len ( ) ;
2000+ self . program . add_data ( DataSymbol {
2001+ name : format ! ( "__gstatic_init_{serial}" ) ,
2002+ udon_type : udon. clone ( ) ,
2003+ init,
2004+ export : false ,
2005+ sync : None ,
2006+ } )
2007+ }
2008+ None => self . default_value ( & ty) ,
2009+ } ;
2010+ let new_values = self . grow_object_array ( ctx, values, length, grown, tail, span. clone ( ) ) ;
19702011 self . set_element ( ctx, array, keys_index, new_keys, span. clone ( ) ) ;
19712012 self . set_element ( ctx, array, values_index, new_values, span. clone ( ) ) ;
19722013 self . copy ( new_values, values) ;
0 commit comments