Skip to content

Commit

Permalink
Add support for typedef'd struct const
Browse files Browse the repository at this point in the history
Summary: Add support for typedef'd struct consts by resolving type aliases recursively.

Reviewed By: simonmar

Differential Revision: D67096788

fbshipit-source-id: 0216246f3b3f09620480a7a97344e761d924921e
  • Loading branch information
helfper authored and facebook-github-bot committed Dec 12, 2024
1 parent 7103496 commit 6c1048d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
21 changes: 11 additions & 10 deletions compiler/Thrift/Compiler/Typechecker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -889,15 +889,6 @@ resolveStructuredAnn StructuredAnn{..} = do
-- locEndLine defLoc == locStartLine loc
| locEndCol defLoc <= locStartCol loc = True
| otherwise = False
lookupSchemaRec :: ThriftName -> Loc -> TC l (Some (Schema l))
lookupSchemaRec name loc = do
thisty <- lookupType name loc
case thisty of
Some ty -> do
let nm = case getAliasedType ty of
TStruct Name{..} _ -> sourceName
_ -> name
lookupSchema nm loc

-- Build the Constant Map ------------------------------------------------------

Expand Down Expand Up @@ -1121,7 +1112,7 @@ typecheckConst tyTop@(TStruct Name{} _loc)
-- First typecheck the struct with the type annotated then check this matches
-- the type given at the top level
svTypeName <- mkThriftName svType
tschema <- lookupSchema svTypeName lLocation
tschema <- lookupSchemaRec svTypeName lLocation
ttyAnn <- lookupType svTypeName lLocation
case (tschema, ttyAnn) of
(Some schema, Some tyAnn) -> do
Expand Down Expand Up @@ -1412,6 +1403,16 @@ getAliasedType :: Type l t -> Type l t
getAliasedType (TTypedef _ ty _loc) = getAliasedType ty
getAliasedType ty = ty

lookupSchemaRec :: ThriftName -> Loc -> TC l (Some (Schema l))
lookupSchemaRec name loc = do
thisty <- lookupType name loc
case thisty of
Some ty -> do
let nm = case getAliasedType ty of
TStruct Name{..} _ -> sourceName
_ -> name
lookupSchema nm loc

mkFieldMap
:: [ListElem MapPair Loc]
-> TC l (Map.Map Text (UntypedConst Loc))
Expand Down
15 changes: 15 additions & 0 deletions tests/if/typedef_struct_const.thrift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

typedef Foo Bar

struct Foo {
1: i16 a;
}

const Bar BAR = Bar{a = 1};

0 comments on commit 6c1048d

Please sign in to comment.