@@ -19,6 +19,7 @@ type FileSet struct {
1919 Package string // package name
2020 Specs map [string ]ast.Expr // type specs in file
2121 Identities map [string ]gen.Elem // processed from specs
22+ Aliased map [string ]string // Aliased types.
2223 Directives []string // raw preprocessor directives
2324 Imports []* ast.ImportSpec // imports
2425 CompactFloats bool // Use smaller floats when feasible
@@ -121,6 +122,22 @@ func (fs *FileSet) applyDirectives() {
121122 }
122123 }
123124 }
125+ // Apply aliases last, so we don't overrule any manually specified replace directives.
126+ for _ , d := range fs .Aliased {
127+ chunks := strings .Split (d , " " )
128+ if len (chunks ) > 0 {
129+ if fn , ok := directives [chunks [0 ]]; ok {
130+ pushstate (chunks [0 ])
131+ err := fn (chunks , fs )
132+ if err != nil {
133+ warnf ("directive error: %s" , err )
134+ }
135+ popstate ()
136+ } else {
137+ newdirs = append (newdirs , d )
138+ }
139+ }
140+ }
124141 fs .Directives = newdirs
125142}
126143
@@ -318,6 +335,15 @@ func (fs *FileSet) getTypeSpecs(f *ast.File) {
318335 for _ , s := range g .Specs {
319336 // for ast.TypeSpecs....
320337 if ts , ok := s .(* ast.TypeSpec ); ok {
338+ // Handle type aliases, by adding a "replace" directive.
339+ if ts .Assign != 0 {
340+ if fs .Aliased == nil {
341+ fs .Aliased = make (map [string ]string )
342+ }
343+ fs .Aliased [ts .Name .Name ] = fmt .Sprintf ("replace %s with:%s" , ts .Name .Name , stringify (ts .Type ))
344+ continue
345+ }
346+
321347 switch ts .Type .(type ) {
322348 // this is the list of parse-able
323349 // type specs
@@ -589,7 +615,7 @@ func (fs *FileSet) parseExpr(e ast.Expr) gen.Elem {
589615 // can be done later, once we've resolved
590616 // everything else.
591617 if b .Value == gen .IDENT {
592- if _ , ok := fs .Specs [e .Name ]; ! ok {
618+ if _ , ok := fs .Specs [e .Name ]; ! ok && fs . Aliased [ e . Name ] == "" {
593619 warnf ("possible non-local identifier: %s\n " , e .Name )
594620 }
595621 }
0 commit comments