c2rust-refactor 0.15.0

C2Rust refactoring tool implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
use rlua::prelude::{LuaError, LuaResult, LuaString, LuaTable, LuaValue};
use syntax::ast::{
    BindingMode, Block, BorrowKind, Crate, Expr, ExprKind, Extern, FloatTy, FnDecl, FnSig, ImplItem, ImplItemKind,
    Item, ItemKind, Lit, LitFloatType, LitKind, Local, Mod, Mutability::*, NodeId, Param, Pat, PatKind, Path, PathSegment,
    Stmt, StmtKind, UintTy, IntTy, LitIntType, Ident, DUMMY_NODE_ID, BinOpKind, UnOp, BlockCheckMode,
    Label, StrLit, StrStyle, TyKind, Ty, MutTy, Unsafety, FunctionRetTy, BareFnTy, UnsafeSource::*, Field,
    AnonConst, Lifetime, AngleBracketedArgs, GenericArgs, GenericArg, VisibilityKind, InlineAsm,
    AsmDialect, InlineAsmOutput, Constness, FnHeader, Generics, IsAsync, ImplPolarity, Defaultness,
    UseTree, UseTreeKind, Arm
};
use syntax::source_map::symbol::Symbol;
use syntax::source_map::{DUMMY_SP, dummy_spanned, Span, SpanData};
use syntax::ptr::P;
use syntax::ThinVec;

use std::rc::Rc;

use crate::ast_manip::fn_edit::{FnKind, FnLike};
use crate::scripting::into_lua_ast::LuaSpan;

fn dummy_expr() -> P<Expr> {
    P(Expr {
        attrs: ThinVec::new(),
        id: DUMMY_NODE_ID,
        kind: ExprKind::Err,
        span: DUMMY_SP,
    })
}

fn dummy_block() -> P<Block> {
    P(Block {
        id: DUMMY_NODE_ID,
        rules: BlockCheckMode::Default,
        span: DUMMY_SP,
        stmts: Vec::new(),
    })
}

fn dummy_ty() -> P<Ty> {
    P(Ty {
        id: DUMMY_NODE_ID,
        kind: TyKind::Err,
        span: DUMMY_SP,
    })
}

fn dummy_fn_decl() -> P<FnDecl> {
    P(FnDecl {
        inputs: Vec::new(),
        output: FunctionRetTy::Default(DUMMY_SP),
    })
}

fn dummy_pat() -> P<Pat> {
    P(Pat {
        id: DUMMY_NODE_ID,
        kind: PatKind::Wild,
        span: DUMMY_SP,
    })
}

fn dummy_path() -> Path {
    Path {
        span: DUMMY_SP,
        segments: Vec::new(),
    }
}

fn dummy_generic_args() -> GenericArgs {
    GenericArgs::AngleBracketed(AngleBracketedArgs {
        span: DUMMY_SP,
        args: Vec::new(),
        constraints: Vec::new(),
    })
}

fn dummy_generic_arg() -> GenericArg {
    GenericArg::Lifetime(Lifetime {
        id: DUMMY_NODE_ID,
        ident: Ident::from_str("placeholder"),
    })
}

fn dummy_stmt() -> Stmt {
    Stmt {
        id: DUMMY_NODE_ID,
        kind: StmtKind::Expr(dummy_expr()),
        span: DUMMY_SP,
    }
}

fn dummy_local() -> P<Local> {
    P(Local {
        attrs: ThinVec::new(),
        id: DUMMY_NODE_ID,
        init: None,
        pat: dummy_pat(),
        span: DUMMY_SP,
        ty: None,
    })
}

fn dummy_item() -> P<Item> {
    P(Item {
        attrs: Vec::new(),
        id: DUMMY_NODE_ID,
        ident: Ident::from_str("placeholder"),
        kind: ItemKind::ExternCrate(None),
        span: DUMMY_SP,
        tokens: None,
        vis: dummy_spanned(VisibilityKind::Public),
    })
}

fn dummy_impl_item() -> ImplItem {
    ImplItem {
        attrs: Vec::new(),
        defaultness: Defaultness::Default,
        generics: Generics::default(),
        id: DUMMY_NODE_ID,
        ident: Ident::from_str("placeholder"),
        kind: ImplItemKind::TyAlias(dummy_ty()),
        span: DUMMY_SP,
        tokens: None,
        vis: dummy_spanned(VisibilityKind::Public),
    }
}

fn dummy_use_tree() -> UseTree {
    UseTree {
        prefix: Path::from_ident(Ident::from_str("placeholder")),
        kind: UseTreeKind::Glob,
        span: DUMMY_SP,
    }
}

fn dummy_arm() -> Arm {
    Arm {
        id: DUMMY_NODE_ID,
        attrs: Vec::new(),
        pat: dummy_pat(),
        guard: None,
        body: dummy_expr(),
        span: DUMMY_SP,
        is_placeholder: false,
    }
}

fn get_node_id_or_default(table: &LuaTable<'_>, field_name: &str) -> LuaResult<NodeId> {
    let opt_id: Option<u32> = table.get(field_name)?;

    Ok(opt_id.map(NodeId::from_u32).unwrap_or(DUMMY_NODE_ID))
}

fn get_span_or_default(table: &LuaTable<'_>, field_name: &str) -> LuaResult<Span> {
    let opt_span_data: Option<LuaSpan> = table.get(field_name)?;
    let opt_span: Option<Span> = opt_span_data.map(|data| {
        let SpanData {lo, hi, ctxt} = data.0;

        Ok(Span::new(lo, hi, ctxt))
    }).transpose()?;

    Ok(opt_span.unwrap_or(DUMMY_SP))
}

pub(crate) trait MergeLuaAst {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()>;
}

impl MergeLuaAst for FnLike {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        self.kind = match table.get::<_, LuaString>("kind")?.to_str()? {
            "Normal" => FnKind::Normal,
            "ImplMethod" => FnKind::ImplMethod,
            "TraitMethod" => FnKind::TraitMethod,
            "Foreign" => FnKind::Foreign,
            _ => self.kind,
        };
        self.id = get_node_id_or_default(&table, "id")?;
        self.ident.name = Symbol::intern(table.get::<_, LuaString>("ident")?.to_str()?);
        self.decl.merge_lua_ast(table.get("decl")?)?;
        self.span = get_span_or_default(&table, "span")?;

        if let Some(ref mut block) = self.block {
            block.merge_lua_ast(table.get("block")?)?;
        }

        // TODO: Attrs

        Ok(())
    }
}

impl MergeLuaAst for P<Block> {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let lua_stmts: LuaTable = table.get("stmts")?;

        self.id = get_node_id_or_default(&table, "id")?;
        self.span = get_span_or_default(&table, "span")?;
        self.rules = match table.get::<_, LuaString>("rules")?.to_str()? {
            "Default" => BlockCheckMode::Default,
            "CompilerGeneratedUnsafe" => BlockCheckMode::Unsafe(CompilerGenerated),
            "UserProvidedUnsafe" => BlockCheckMode::Unsafe(UserProvided),
            e => unimplemented!("Found unknown block rule: {}", e),
        };

        let mut stmts = Vec::new();

        for lua_stmt in lua_stmts.sequence_values::<LuaTable>() {
            let mut stmt = dummy_stmt();

            stmt.merge_lua_ast(lua_stmt?)?;
            stmts.push(stmt);
        }

        self.stmts = stmts;

        Ok(())
    }
}

impl MergeLuaAst for Stmt {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let kind: LuaString = table.get("kind")?;

        self.id = get_node_id_or_default(&table, "id")?;
        self.span = get_span_or_default(&table, "span")?;
        self.kind = match kind.to_str()? {
            "Local" => {
                let mut local = dummy_local();

                local.merge_lua_ast(table)?;

                StmtKind::Local(local)
            },
            "Item" => {
                let mut item = dummy_item();
                let lua_item = table.get("item")?;

                item.merge_lua_ast(lua_item)?;

                StmtKind::Item(item)
            },
            "Expr" => {
                let lua_expr = table.get("expr")?;
                let mut expr = dummy_expr();

                expr.merge_lua_ast(lua_expr)?;

                StmtKind::Expr(expr)
            },
            "Semi" => {
                let lua_expr = table.get("expr")?;
                let mut expr = dummy_expr();

                expr.merge_lua_ast(lua_expr)?;

                StmtKind::Semi(expr)
            },
            e => unimplemented!("MergeLuaAst unimplemented for StmtKind::{}", e),
        };

        Ok(())
    }
}

impl MergeLuaAst for P<FnDecl> {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let lua_args: LuaTable = table.get("args")?;
        let lua_return_type: Option<LuaTable> = table.get("return_type")?;
        let return_type = lua_return_type.map(|lua_ty| {
            let mut ty = dummy_ty();

            ty.merge_lua_ast(lua_ty).map(|_| ty)
        }).transpose()?;

        self.output = match return_type {
            Some(ty) => FunctionRetTy::Ty(ty),
            None => FunctionRetTy::Default(DUMMY_SP),
        };

        let mut args = Vec::new();

        for lua_arg in lua_args.sequence_values::<LuaTable>() {
            let lua_arg = lua_arg?;
            let mut arg = Param {
                attrs: ThinVec::new(),
                ty: dummy_ty(),
                pat: dummy_pat(),
                id: get_node_id_or_default(&lua_arg, "id")?,
                span: get_span_or_default(&lua_arg, "span")?,
                is_placeholder: false,
            };

            arg.merge_lua_ast(lua_arg)?;
            args.push(arg);
        }

        self.inputs = args;

        Ok(())
    }
}

impl MergeLuaAst for Param {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        self.id = get_node_id_or_default(&table, "id")?;
        self.pat.merge_lua_ast(table.get("pat")?)?;
        self.ty.merge_lua_ast(table.get("ty")?)?;
        // TODO: Attrs

        Ok(())
    }
}

impl MergeLuaAst for P<Pat> {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let kind: LuaString = table.get("kind")?;

        self.span = get_span_or_default(&table, "span")?;
        self.id = get_node_id_or_default(&table, "id")?;
        self.kind = match kind.to_str()? {
            "Wild" => PatKind::Wild,
            "Ident" => {
                let binding: LuaString = table.get("binding")?;
                let binding = match binding.to_str()? {
                    "ByRefImmutable" => BindingMode::ByRef(Immutable),
                    "ByRefMutable" => BindingMode::ByRef(Mutable),
                    "ByValueImmutable" => BindingMode::ByValue(Immutable),
                    "ByValueMutable" => BindingMode::ByValue(Mutable),
                    e => unimplemented!("Unknown ident binding: {}", e),
                };
                let ident: LuaString = table.get("ident")?;
                let ident = Ident::from_str(ident.to_str()?);

                // TODO: Sub-pattern
                PatKind::Ident(binding, ident, None)
            },
            "Tuple" => {
                let lua_patterns: LuaTable = table.get("pats")?;
                let mut patterns = Vec::new();

                for lua_pat in lua_patterns.sequence_values::<LuaTable>() {
                    let mut pat = dummy_pat();

                    pat.merge_lua_ast(lua_pat?)?;
                    patterns.push(pat);
                }

                PatKind::Tuple(patterns)
            },
            "Lit" => {
                let lua_expr = table.get("expr")?;
                let mut expr = dummy_expr();

                expr.merge_lua_ast(lua_expr)?;

                PatKind::Lit(expr)
            },
            e => unimplemented!("MergeLuaAst unimplemented pat: {:?}", e),
        };

        Ok(())
    }
}

impl MergeLuaAst for Local {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let opt_lua_ty: Option<LuaTable> = table.get("ty")?;
        let pat: LuaTable = table.get("pat")?;
        let opt_init: Option<LuaTable> = table.get("init")?;

        self.pat.merge_lua_ast(pat)?;

        match &mut self.init {
            Some(existing_init) => {
                match opt_init {
                    Some(init) => existing_init.merge_lua_ast(init)?,
                    None => self.init = None,
                }
            },
            None => {
                if let Some(init) = opt_init {
                    let mut expr = dummy_expr();

                    expr.merge_lua_ast(init)?;

                    self.init = Some(expr);
                }
            },
        }

        match &mut self.ty {
            Some(existing_ty) => {
                match opt_lua_ty {
                    Some(ty) => existing_ty.merge_lua_ast(ty)?,
                    None => self.ty = None,
                }
            },
            None => {
                if let Some(ty) = opt_lua_ty {
                    let mut expr = dummy_ty();

                    expr.merge_lua_ast(ty)?;

                    self.ty = Some(expr);
                }
            },
        }

        Ok(())
    }
}

impl MergeLuaAst for Crate {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        self.module.merge_lua_ast(table.get("module")?)?;
        self.span = get_span_or_default(&table, "span")?;

        Ok(())
    }
}

impl MergeLuaAst for Mod {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let lua_items: LuaTable = table.get("items")?;

        self.inline = table.get("inline")?;
        self.inner = get_span_or_default(&table, "inner")?;

        // TODO: This may need to be improved if we want to delete or add
        // items as it currently expects items to be 1-1
        self.items.iter_mut().enumerate().map(|(i, item)| {
            let item_table: LuaTable = lua_items.get(i + 1)?;

            item.merge_lua_ast(item_table)
        }).collect()
    }
}

impl MergeLuaAst for P<Item> {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let kind = table.get::<_, LuaString>("kind")?;
        let kind = kind.to_str()?;

        self.span = get_span_or_default(&table, "span")?;
        self.id = get_node_id_or_default(&table, "id")?;
        self.ident.name = Symbol::intern(&table.get::<_, LuaString>("ident")?.to_str()?);
        self.kind = match kind {
            "Fn" => {
                let lua_fn_decl = table.get("decl")?;
                let lua_block = table.get("block")?;
                let mut block = dummy_block();
                let mut decl = dummy_fn_decl();
                let unsafety = match table.get::<_, LuaString>("unsafety")?.to_str()? {
                    "Unsafe" => Unsafety::Unsafe,
                    "Normal" => Unsafety::Normal,
                    e => unimplemented!("Unknown unsafety: {}", e),
                };
                let constness = match table.get::<_, bool>("is_const")? {
                    true => dummy_spanned(Constness::Const),
                    false => dummy_spanned(Constness::NotConst),
                };
                let ext = match table.get::<_, Option<LuaString>>("ext")? {
                    Some(ext) => match ext.to_str()? {
                        "" => Extern::Implicit,
                        s => Extern::Explicit(StrLit {
                            style: StrStyle::Cooked,
                            symbol: Symbol::intern(s),
                            suffix: None,
                            span: DUMMY_SP,
                            symbol_unescaped: Symbol::intern(s),
                        }),
                    }
                    None => Extern::None,
                };
                let fn_header = FnHeader {
                    ext,
                    asyncness: dummy_spanned(IsAsync::NotAsync), // TODO
                    constness,
                    unsafety,
                };
                let generics = Generics::default(); // TODO

                block.merge_lua_ast(lua_block)?;
                decl.merge_lua_ast(lua_fn_decl)?;
                let sig = FnSig {
                    decl,
                    header: fn_header,
                };

                ItemKind::Fn(sig, generics, block)
            },
            "Impl" => {
                let lua_items: LuaTable = table.get("items")?;
                let lua_ty = table.get("ty")?;
                let mut items = Vec::new();
                let unsafety = Unsafety::Normal; // TODO
                let polarity = ImplPolarity::Positive; // TODO
                let defaultness = Defaultness::Default; // TODO
                let generics = Generics::default(); // TODO
                let trait_ref = None; // TODO
                let mut ty = dummy_ty();

                ty.merge_lua_ast(lua_ty)?;

                for lua_item in lua_items.sequence_values::<LuaTable>() {
                    let lua_item = lua_item?;
                    let mut item = dummy_impl_item();

                    item.merge_lua_ast(lua_item)?;
                    items.push(item);
                }

                ItemKind::Impl(unsafety, polarity, defaultness, generics, trait_ref, ty, items)
            },
            "Use" => {
                let lua_use_tree = table.get("tree")?;
                let mut use_tree = dummy_use_tree();

                use_tree.merge_lua_ast(lua_use_tree)?;

                ItemKind::Use(P(use_tree))
            },
            "TyAlias" => {
                let lua_ty = table.get("ty")?;
                let mut ty = dummy_ty();

                ty.merge_lua_ast(lua_ty)?;

                // TODO: Generics
                ItemKind::TyAlias(ty, Generics::default())
            },
            "Static" => {
                let lua_ty = table.get("ty")?;
                let lua_expr = table.get("expr")?;
                let mutability = match table.get::<_, LuaString>("mutability")?.to_str()? {
                    "Immutable" => Immutable,
                    "Mutable" => Mutable,
                    e => panic!("Found unknown addrof mutability: {}", e),
                };
                let mut ty = dummy_ty();
                let mut expr = dummy_expr();

                ty.merge_lua_ast(lua_ty)?;
                expr.merge_lua_ast(lua_expr)?;

                ItemKind::Static(ty, mutability, expr)
            }
            ref e => unimplemented!("MergeLuaAst unimplemented item kind: {:?}", e),
        };

        Ok(())
    }
}

fn lit_from_int(int: u128, suffix: Option<&str>) -> LitKind {
    let suffix = match suffix {
        None => LitIntType::Unsuffixed,
        Some("u8") => LitIntType::Unsigned(UintTy::U8),
        Some("u16") => LitIntType::Unsigned(UintTy::U16),
        Some("u32") => LitIntType::Unsigned(UintTy::U32),
        Some("u64") => LitIntType::Unsigned(UintTy::U64),
        Some("u128") => LitIntType::Unsigned(UintTy::U128),
        Some("usize") => LitIntType::Unsigned(UintTy::Usize),
        Some("i8") => LitIntType::Signed(IntTy::I8),
        Some("i16") => LitIntType::Signed(IntTy::I16),
        Some("i32") => LitIntType::Signed(IntTy::I32),
        Some("i64") => LitIntType::Signed(IntTy::I64),
        Some("i128") => LitIntType::Signed(IntTy::I128),
        Some("isize") => LitIntType::Signed(IntTy::Isize),
        _ => unreachable!("Unknown int suffix"),
    };

    LitKind::Int(int, suffix)
}

impl MergeLuaAst for P<Expr> {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let kind = table.get::<_, LuaString>("kind")?;
        let kind = kind.to_str()?;

        self.span = get_span_or_default(&table, "span")?;
        self.id = get_node_id_or_default(&table, "id")?;
        self.kind = match kind {
            "Path" => {
                let lua_segments: LuaTable = table.get("segments")?;
                let mut path = dummy_path();

                path.merge_lua_ast(lua_segments)?;

                // TODO: QSelf support
                ExprKind::Path(None, path)
            },
            "Lit" => {
                let val: LuaValue = table.get("value")?;
                let suffix: Option<LuaString> = table.get("suffix")?;
                let suffix = suffix.as_ref().map(|s| s.to_str()).transpose()?;
                let lit_kind = match val {
                    LuaValue::Boolean(val) => LitKind::Bool(val),
                    LuaValue::Integer(i) => lit_from_int(i as u128, suffix),
                    LuaValue::Number(num) => {
                        let num_kind: LuaString = table.get("num_kind")?;

                        // (R)Lua will convert any value > i64::max_value()
                        // to a float so we must stringly type that it's
                        // expected to be an integer
                        if num_kind.to_str()? == "Int" {
                            lit_from_int(num as u128, suffix)
                        } else {
                            let mut string = num.to_string();

                            // to_string won't add a trailing period if unsuffixed
                            if !string.contains('.') {
                                string.push('.');
                            }

                            let sym = Symbol::intern(&string);

                            match suffix {
                                None => LitKind::Float(sym, LitFloatType::Unsuffixed),
                                Some("f32") => LitKind::Float(sym, LitFloatType::Suffixed(FloatTy::F32)),
                                Some("f64") => LitKind::Float(sym, LitFloatType::Suffixed(FloatTy::F64)),
                                Some(e) => unreachable!("Unknown float suffix: {}{}", num, e),
                            }
                        }
                    },
                    LuaValue::String(lua_string) => {
                        let str_kind = table.get::<_, LuaString>("str_kind")?;
                        let str_kind = str_kind.to_str()?;
                        let is_bytes = str_kind == "ByteStr";
                        let is_char = str_kind == "Char";

                        if is_bytes {
                            let bytes: Vec<u8> = lua_string
                                .as_bytes()
                                .iter()
                                .map(|b| *b)
                                .collect();

                            LitKind::ByteStr(Rc::new(bytes))
                        } else {
                            let string = lua_string.to_str()?;

                            if is_char {
                                let ch = string
                                    .chars()
                                    .next()
                                    .ok_or(LuaError::external("Found empty string where char was expected."));

                                LitKind::Char(ch?)
                            } else {
                                // TODO: Raw strings?
                                let symbol = Symbol::intern(string);
                                let style = StrStyle::Cooked;

                                LitKind::Str(symbol, style)
                            }
                        }
                    },
                    LuaValue::Nil => {
                        let symbol = Symbol::intern("NIL");

                        LitKind::Err(symbol)
                    },
                    _ => unimplemented!("MergeLuaAst unimplemented lit: {:?}", val),
                };

                ExprKind::Lit(Lit {
                    token: lit_kind.to_lit_token(),
                    kind: lit_kind,
                    span: DUMMY_SP,
                })
            },
            "Binary" | "AssignOp" | "Assign" => {
                let lua_lhs = table.get("lhs")?;
                let lua_rhs = table.get("rhs")?;
                let op: Option<LuaString> = table.get("op")?;
                let op = op.map(|s| Ok(match s.to_str()? {
                    "+" => BinOpKind::Add,
                    "-" => BinOpKind::Sub,
                    "*" => BinOpKind::Mul,
                    "/" => BinOpKind::Div,
                    "%" => BinOpKind::Rem,
                    "&&" => BinOpKind::And,
                    "||" => BinOpKind::Or,
                    "^" => BinOpKind::BitXor,
                    "&" => BinOpKind::BitAnd,
                    "|" => BinOpKind::BitOr,
                    "<<" => BinOpKind::Shl,
                    ">>" => BinOpKind::Shr,
                    "==" => BinOpKind::Eq,
                    "<" => BinOpKind::Lt,
                    "<=" => BinOpKind::Le,
                    "!=" => BinOpKind::Ne,
                    ">=" => BinOpKind::Ge,
                    ">" => BinOpKind::Gt,
                    e => unreachable!("Unknown BinOpKind: {}", e),
                })).transpose()?;

                let mut lhs = dummy_expr();
                let mut rhs = dummy_expr();

                lhs.merge_lua_ast(lua_lhs)?;
                rhs.merge_lua_ast(lua_rhs)?;

                match kind {
                    "Binary" => ExprKind::Binary(dummy_spanned(op.unwrap()), lhs, rhs),
                    "AssignOp" => ExprKind::AssignOp(dummy_spanned(op.unwrap()), lhs, rhs),
                    "Assign" => ExprKind::Assign(lhs, rhs),
                    _ => unreachable!(),
                }
            },
            "Array" => {
                let lua_exprs: LuaTable = table.get("values")?;
                let mut exprs = Vec::new();

                for lua_expr in lua_exprs.sequence_values::<LuaTable>() {
                    let mut expr = dummy_expr();

                    expr.merge_lua_ast(lua_expr?)?;

                    exprs.push(expr);
                }

                ExprKind::Array(exprs)
            },
            "Index" => {
                let lua_indexed: LuaTable = table.get("indexed")?;
                let lua_index: LuaTable = table.get("index")?;
                let mut indexed = dummy_expr();
                let mut index = dummy_expr();

                indexed.merge_lua_ast(lua_indexed)?;
                index.merge_lua_ast(lua_index)?;

                ExprKind::Index(indexed, index)
            },
            "Unary" => {
                let op: LuaString = table.get("op")?;
                let lua_expr: LuaTable = table.get("expr")?;
                let op = match op.to_str()? {
                    "*" => UnOp::Deref,
                    "!" => UnOp::Not,
                    "-" => UnOp::Neg,
                    e => unreachable!("Unknown UnOp: {}", e),
                };
                let mut expr = dummy_expr();

                expr.merge_lua_ast(lua_expr)?;

                ExprKind::Unary(op, expr)
            },
            "Call" => {
                let mut path = dummy_expr();
                let lua_path = table.get("path")?;
                let lua_args: LuaTable = table.get("args")?;
                let mut args = Vec::new();

                path.merge_lua_ast(lua_path)?;

                for lua_arg in lua_args.sequence_values::<LuaTable>() {
                    let mut arg = dummy_expr();

                    arg.merge_lua_ast(lua_arg?)?;
                    args.push(arg);
                }

                ExprKind::Call(path, args)
            },
            "MethodCall" => {
                let lua_args: LuaTable = table.get("args")?;
                let mut args = Vec::new();

                for lua_arg in lua_args.sequence_values::<LuaTable>() {
                    let mut arg = dummy_expr();

                    arg.merge_lua_ast(lua_arg?)?;
                    args.push(arg);
                }

                let lua_segment: LuaTable = table.get("segment")?;
                let name: LuaString = lua_segment.get("ident")?;
                let mut segment = PathSegment::from_ident(Ident::from_str(name.to_str()?));
                let lua_generics: Option<LuaTable> = lua_segment.get("generics")?;
                let opt_generics = lua_generics.map(|lua_generics| {
                    let mut generics = dummy_generic_args();

                    generics.merge_lua_ast(lua_generics).map(|_| P(generics))
                }).transpose()?;

                segment.id = get_node_id_or_default(&lua_segment, "id")?;
                segment.args = opt_generics;

                ExprKind::MethodCall(segment, args)
            },
            "Field" => {
                let lua_expr: LuaTable = table.get("expr")?;
                let mut expr = dummy_expr();
                let name: LuaString = table.get("name")?;

                expr.merge_lua_ast(lua_expr)?;

                ExprKind::Field(expr, Ident::from_str(name.to_str()?))
            },
            "Ret" => {
                let opt_lua_expr: Option<LuaTable> = table.get("value")?;

                match opt_lua_expr {
                    Some(lua_expr) => {
                        let mut expr = dummy_expr();

                        expr.merge_lua_ast(lua_expr)?;

                        ExprKind::Ret(Some(expr))
                    },
                    None => ExprKind::Ret(None),
                }
            },
            "Tup" => {
                let lua_exprs: LuaTable = table.get("exprs")?;
                let mut exprs = Vec::new();

                for lua_expr in lua_exprs.sequence_values::<LuaTable>() {
                    let mut expr = dummy_expr();

                    expr.merge_lua_ast(lua_expr?)?;
                    exprs.push(expr);
                }

                ExprKind::Tup(exprs)
            },
            "AddrOf" => {
                let lua_expr = table.get("expr")?;
                let mut expr = dummy_expr();

                expr.merge_lua_ast(lua_expr)?;

                let mutability = match table.get::<_, LuaString>("mutability")?.to_str()? {
                    "Immutable" => Immutable,
                    "Mutable" => Mutable,
                    e => panic!("Found unknown addrof mutability: {}", e),
                };

                ExprKind::AddrOf(BorrowKind::Ref, mutability, expr)
            },
            "Block" => {
                let lua_block = table.get("block")?;
                let mut block = dummy_block();
                let opt_label_str: Option<LuaString> = table.get("label")?;
                let opt_label = opt_label_str.map(|s| Ok(Label {
                    ident: Ident::from_str(s.to_str()?)
                })).transpose()?;

                block.merge_lua_ast(lua_block)?;

                ExprKind::Block(block, opt_label)
            },
            "While" => {
                let lua_cond = table.get("cond")?;
                let lua_block = table.get("block")?;
                let mut cond = dummy_expr();
                let mut block = dummy_block();
                let opt_label_str: Option<LuaString> = table.get("label")?;
                let opt_label = opt_label_str.map(|s| Ok(Label {
                    ident: Ident::from_str(s.to_str()?)
                })).transpose()?;

                block.merge_lua_ast(lua_block)?;
                cond.merge_lua_ast(lua_cond)?;

                ExprKind::While(cond, block, opt_label)
            },
            "If" => {
                let lua_cond = table.get("cond")?;
                let lua_then = table.get("then")?;
                let opt_lua_else: Option<_> = table.get("else")?;
                let mut cond = dummy_expr();
                let mut then = dummy_block();

                cond.merge_lua_ast(lua_cond)?;
                then.merge_lua_ast(lua_then)?;

                let opt_else = opt_lua_else.map(|lua_else| {
                    let mut expr = dummy_expr();

                    expr.merge_lua_ast(lua_else).map(|_| expr)
                }).transpose()?;

                ExprKind::If(cond, then, opt_else)
            },
            "Cast" => {
                let lua_expr = table.get("expr")?;
                let mut expr = dummy_expr();
                let lua_ty = table.get("ty")?;
                let mut ty = dummy_ty();

                expr.merge_lua_ast(lua_expr)?;
                ty.merge_lua_ast(lua_ty)?;

                ExprKind::Cast(expr, ty)
            },
            "Struct" => {
                let lua_path = table.get("path")?;
                let lua_fields: LuaTable = table.get("fields")?;
                let opt_lua_expr: Option<_> = table.get("expr")?;
                let opt_expr = opt_lua_expr.map(|lua_expr| {
                    let mut expr = dummy_expr();

                    expr.merge_lua_ast(lua_expr).map(|_| expr)
                }).transpose()?;
                let mut path = dummy_path();
                let mut fields = Vec::new();

                path.merge_lua_ast(lua_path)?;

                for field in lua_fields.sequence_values::<LuaTable>() {
                    let field = field?;
                    let string: LuaString = field.get("ident")?;
                    let ident = Ident::from_str(string.to_str()?);
                    let lua_expr = field.get("expr")?;
                    let mut expr = dummy_expr();
                    let is_shorthand = field.get("is_shorthand")?;
                    let span = get_span_or_default(&field, "span")?;
                    let id = get_node_id_or_default(&table, "id")?;

                    expr.merge_lua_ast(lua_expr)?;

                    fields.push(Field {
                        id,
                        ident,
                        expr,
                        span,
                        is_shorthand,
                        attrs: ThinVec::new(), // TODO
                        is_placeholder: false, // TODO
                    })
                }

                ExprKind::Struct(path, fields, opt_expr)
            },
            "Repeat" => {
                let lua_expr = table.get("expr")?;
                let lua_ac_expr = table.get("anon_const")?;
                let mut expr = dummy_expr();
                let mut ac_expr = dummy_expr();
                let id = get_node_id_or_default(&table, "id")?;

                expr.merge_lua_ast(lua_expr)?;
                ac_expr.merge_lua_ast(lua_ac_expr)?;

                let anon_const = AnonConst {
                    id,
                    value: ac_expr,
                };

                ExprKind::Repeat(expr, anon_const)
            },
            "InlineAsm" => {
                let asm: LuaString = table.get("asm")?;
                let asm = Symbol::intern(asm.to_str()?);
                let dialect: LuaString = table.get("dialect")?;
                let dialect = match dialect.to_str()? {
                    "Att" => AsmDialect::Att,
                    "Intel" => AsmDialect::Intel,
                    e => unimplemented!("Unknown ASM dialect: {}", e),
                };
                let lua_inputs: LuaTable = table.get("inputs")?;
                let lua_outputs: LuaTable = table.get("outputs")?;
                let lua_clobbers: LuaTable = table.get("clobbers")?;
                let mut outputs = Vec::new();
                let mut inputs = Vec::new();
                let mut clobbers = Vec::new();

                for lua_output in lua_outputs.sequence_values::<LuaTable>() {
                    let lua_output = lua_output?;
                    let lua_expr = lua_output.get("expr")?;
                    let lua_constraint: LuaString = lua_output.get("constraint")?;
                    let mut expr = dummy_expr();

                    expr.merge_lua_ast(lua_expr)?;

                    let output = InlineAsmOutput {
                        constraint: Symbol::intern(lua_constraint.to_str()?),
                        expr,
                        is_indirect: lua_output.get("is_indirect")?,
                        is_rw: lua_output.get("is_rw")?,
                    };
                    outputs.push(output);
                }

                for lua_input in lua_inputs.sequence_values::<LuaTable>() {
                    let lua_input = lua_input?;
                    let lua_symbol: LuaString = lua_input.get("symbol")?;
                    let symbol = Symbol::intern(lua_symbol.to_str()?);
                    let lua_expr = lua_input.get("expr")?;
                    let mut expr = dummy_expr();

                    expr.merge_lua_ast(lua_expr)?;
                    inputs.push((symbol, expr))
                }

                for lua_clobber in lua_clobbers.sequence_values::<LuaString>() {
                    let lua_clobber = lua_clobber?;
                    let symbol = Symbol::intern(lua_clobber.to_str()?);

                    clobbers.push(symbol);
                }

                ExprKind::InlineAsm(P(InlineAsm {
                    asm,
                    asm_str_style: StrStyle::Cooked, // TODO: Raw strings
                    outputs,
                    inputs,
                    clobbers,
                    volatile: table.get("volatile")?,
                    alignstack: table.get("alignstack")?,
                    dialect,
                }))
            },
            "Loop" => {
                let lua_block = table.get("block")?;
                let lua_label: Option<LuaString> = table.get("label")?;
                let opt_label = lua_label.map(|string| Ok(Label {
                    ident: Ident::from_str(string.to_str()?)
                })).transpose()?;
                let mut block = dummy_block();

                block.merge_lua_ast(lua_block)?;

                ExprKind::Loop(block, opt_label)
            },
            "Break" => {
                let lua_expr: Option<LuaTable> = table.get("expr")?;
                let lua_label: Option<LuaString> = table.get("label")?;
                let opt_label = lua_label.map(|string| Ok(Label {
                    ident: Ident::from_str(string.to_str()?)
                })).transpose()?;
                let opt_expr = lua_expr.map(|lua_expr| {
                    let mut expr = dummy_expr();

                    expr.merge_lua_ast(lua_expr).map(|_| expr)
                }).transpose()?;

                ExprKind::Break(opt_label, opt_expr)
            },
            "Continue" => {
                let lua_label: Option<LuaString> = table.get("label")?;
                let opt_label = lua_label.map(|string| Ok(Label {
                    ident: Ident::from_str(string.to_str()?)
                })).transpose()?;

                ExprKind::Continue(opt_label)
            },
            "Match" => {
                let lua_expr = table.get("expr")?;
                let lua_arms: LuaTable = table.get("arms")?;
                let mut expr = dummy_expr();
                let mut arms = Vec::new();

                for lua_arm in lua_arms.sequence_values::<LuaTable>() {
                    let mut arm = dummy_arm();

                    arm.merge_lua_ast(lua_arm?)?;
                    arms.push(arm);
                }

                expr.merge_lua_ast(lua_expr)?;

                ExprKind::Match(expr, arms)
            },
            "ForLoop" => {
                let lua_expr = table.get("expr")?;
                let lua_pat = table.get("pat")?;
                let lua_block = table.get("block")?;
                let lua_label: Option<LuaString> = table.get("label")?;
                let opt_label = lua_label.map(|string| Ok(Label {
                    ident: Ident::from_str(string.to_str()?)
                })).transpose()?;
                let mut expr = dummy_expr();
                let mut pat = dummy_pat();
                let mut block = dummy_block();

                block.merge_lua_ast(lua_block)?;
                expr.merge_lua_ast(lua_expr)?;
                pat.merge_lua_ast(lua_pat)?;

                ExprKind::ForLoop(pat, expr, block, opt_label)
            },
            "Paren" => {
                let lua_expr = table.get("expr")?;
                let mut expr = dummy_expr();

                expr.merge_lua_ast(lua_expr)?;

                ExprKind::Paren(expr)
            }
            ref e => {
                warn!("MergeLuaAst unimplemented expr: {:?}", e);
                return Ok(());
            },
        };

        Ok(())
    }
}

impl MergeLuaAst for ImplItem {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        self.ident.name = Symbol::intern(&table.get::<_, LuaString>("ident")?.to_str()?);
        self.span = get_span_or_default(&table, "span")?;
        self.id = get_node_id_or_default(&table, "id")?;

        // TODO: Allow for inplace kind mutations
        match &mut self.kind {
            ImplItemKind::Method(sig, block) => {
                let lua_decl: LuaTable = table.get("decl")?;
                let lua_block: LuaTable = table.get("block")?;

                sig.decl.merge_lua_ast(lua_decl)?;
                block.merge_lua_ast(lua_block)?;

                // TODO: generics, attrs, ..
            },
            ref e => unimplemented!("MergeLuaAst for {:?}", e),
        }

        Ok(())
    }
}

impl MergeLuaAst for P<Ty> {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let kind: LuaString = table.get("kind")?;
        let kind = kind.to_str()?;

        self.span = get_span_or_default(&table, "span")?;
        self.id = get_node_id_or_default(&table, "id")?;
        self.kind = match kind {
            "Path" => {
                let lua_segments: LuaTable = table.get("path")?;
                let mut path = dummy_path();

                path.merge_lua_ast(lua_segments)?;

                // TODO: QSelf support
                TyKind::Path(None, path)
            },
            "Ptr" | "Rptr" => {
                let mutbl = match table.get::<_, LuaString>("mutbl")?.to_str()? {
                    "Immutable" => Immutable,
                    "Mutable" => Mutable,
                    e => panic!("Found unknown ptr mutability: {}", e),
                };
                let lua_ty = table.get("ty")?;
                let mut ty = dummy_ty();

                ty.merge_lua_ast(lua_ty)?;

                let mut_ty = MutTy {ty, mutbl};

                if kind == "Ptr" {
                    TyKind::Ptr(mut_ty)
                } else {
                    let lua_lifetime: Option<LuaString> = table.get("lifetime")?;
                    let opt_lifetime = lua_lifetime.map(|s| {
                        s.to_str()
                            .map(|s| Ident::from_str(s))
                            .map(|i| Lifetime {id: DUMMY_NODE_ID, ident: i})
                    }).transpose()?;

                    TyKind::Rptr(opt_lifetime, mut_ty)
                }
            },
            "BareFn" => {
                let lua_decl = table.get("decl")?;
                let mut decl = dummy_fn_decl();
                let unsafety = match table.get::<_, LuaString>("unsafety")?.to_str()? {
                    "Unsafe" => Unsafety::Unsafe,
                    "Normal" => Unsafety::Normal,
                    e => panic!("Found unknown unsafety: {}", e),
                };
                let ext = match table.get::<_, Option<LuaString>>("ext")? {
                    Some(ext) => match ext.to_str()? {
                        "" => Extern::Implicit,
                        s => Extern::Explicit(StrLit {
                            style: StrStyle::Cooked,
                            symbol: Symbol::intern(s),
                            suffix: None,
                            span: DUMMY_SP,
                            symbol_unescaped: Symbol::intern(s),
                        }),
                    }
                    None => Extern::None,
                };
                decl.merge_lua_ast(lua_decl)?;

                let bare_fn = BareFnTy {
                    unsafety,
                    ext,
                    generic_params: Vec::new(), // TODO
                    decl,
                };

                TyKind::BareFn(P(bare_fn))
            },
            "Array" => {
                let lua_ty = table.get("ty")?;
                let lua_ac_expr = table.get("anon_const")?;
                let mut ac_expr = dummy_expr();
                let mut ty = dummy_ty();

                ac_expr.merge_lua_ast(lua_ac_expr)?;
                ty.merge_lua_ast(lua_ty)?;

                let anon_const = AnonConst {
                    id: DUMMY_NODE_ID,
                    value: ac_expr,
                };

                TyKind::Array(ty, anon_const)
            },
            "Typeof" => {
                let lua_ac_expr = table.get("anon_const")?;
                let mut ac_expr = dummy_expr();

                ac_expr.merge_lua_ast(lua_ac_expr)?;

                let anon_const = AnonConst {
                    id: DUMMY_NODE_ID,
                    value: ac_expr,
                };

                TyKind::Typeof(anon_const)
            },
            "Paren" | "Slice" => {
                let lua_ty = table.get("ty")?;
                let mut ty = dummy_ty();

                ty.merge_lua_ast(lua_ty)?;

                if kind == "Paren" {
                    TyKind::Paren(ty)
                } else {
                    TyKind::Slice(ty)
                }
            },
            "Tup" => {
                let lua_tys: LuaTable = table.get("tys")?;
                let mut tys = Vec::new();

                for lua_ty in lua_tys.sequence_values::<LuaTable>() {
                    let mut ty = dummy_ty();

                    ty.merge_lua_ast(lua_ty?)?;
                    tys.push(ty);
                }

                TyKind::Tup(tys)
            },
            "Never" => TyKind::Never,
            "ImplicitSelf" => TyKind::ImplicitSelf,
            "CVarArgs" => TyKind::CVarArgs,
            "Infer" => TyKind::Infer,
            ref e => unimplemented!("MergeLuaAst unimplemented ty kind: {:?}", e),
        };

        Ok(())
    }
}

impl MergeLuaAst for Path {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let mut segments = Vec::new();

        for lua_segment in table.sequence_values::<LuaTable>() {
            let lua_segment = lua_segment?;
            let ident: LuaString = lua_segment.get("ident")?;
            let lua_generics: Option<LuaTable> = lua_segment.get("generics")?;
            let mut path_segment = PathSegment::from_ident(Ident::from_str(ident.to_str()?));
            let opt_generics = lua_generics.map(|lua_generics| {
                let mut generics = dummy_generic_args();

                generics.merge_lua_ast(lua_generics).map(|_| P(generics))
            }).transpose()?;

            path_segment.id = get_node_id_or_default(&lua_segment, "id")?;
            path_segment.args = opt_generics;

            segments.push(path_segment);
        }

        self.segments = segments;

        Ok(())
    }
}

impl MergeLuaAst for GenericArgs {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let kind: LuaString = table.get("kind")?;
        let kind = kind.to_str()?;
        let span = get_span_or_default(&table, "span")?;

        *self = match kind {
            "AngleBracketed" => {
                let lua_args: LuaTable = table.get("args")?;
                let mut args = Vec::new();

                for lua_arg in lua_args.sequence_values::<LuaTable>() {
                    let mut arg = dummy_generic_arg();

                    arg.merge_lua_ast(lua_arg?)?;
                    args.push(arg);
                }

                GenericArgs::AngleBracketed(AngleBracketedArgs {
                    args,
                    span,
                    constraints: Vec::new(), // TODO
                })
            },
            "Parenthesized" => unimplemented!("MergeLuaAst unimplemented for Parenthesized"),
            e => unimplemented!("Unknown GenericArgs kind: {}", e),
        };

        Ok(())
    }
}

impl MergeLuaAst for GenericArg {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let kind: LuaString = table.get("kind")?;
        let kind = kind.to_str()?;

        *self = match kind {
            "Type" => {
                let lua_ty = table.get("ty")?;
                let mut ty = dummy_ty();

                ty.merge_lua_ast(lua_ty)?;

                GenericArg::Type(ty)
            },
            e => unimplemented!("Unknown GenericArg kind: {}", e),
        };

        Ok(())
    }
}

impl MergeLuaAst for UseTree {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let kind: LuaString = table.get("kind")?;
        let kind = kind.to_str()?;
        let lua_prefix_segments = table.get::<_, LuaTable>("prefix")?;

        self.span = get_span_or_default(&table, "span")?;
        self.prefix.merge_lua_ast(lua_prefix_segments)?;
        self.kind = match kind {
            "Simple" => {
                let lua_ident: Option<LuaString> = table.get("ident")?;
                let opt_ident = lua_ident.map(|lua_string|
                    Ok(Ident::from_str(lua_string.to_str()?))
                ).transpose()?;

                UseTreeKind::Simple(opt_ident, DUMMY_NODE_ID, DUMMY_NODE_ID)
            },
            "Nested" => {
                let lua_trees: LuaTable = table.get("trees")?;
                let mut trees = Vec::new();

                for lua_tree in lua_trees.sequence_values::<LuaTable>() {
                    let lua_tree = lua_tree?;
                    let mut tree = dummy_use_tree();

                    tree.merge_lua_ast(lua_tree)?;
                    trees.push((tree, DUMMY_NODE_ID));
                }

                UseTreeKind::Nested(trees)
            },
            "Glob" => UseTreeKind::Glob,
            e => unimplemented!("Unknown UseTree kind: {}", e),
        };

        Ok(())
    }
}

impl MergeLuaAst for Arm {
    fn merge_lua_ast<'lua>(&mut self, table: LuaTable<'lua>) -> LuaResult<()> {
        let lua_body = table.get("body")?;
        let lua_guard: Option<LuaTable> = table.get("guard")?;
        let lua_pat: LuaTable = table.get("pat")?;

        self.body.merge_lua_ast(lua_body)?;

        match (&mut self.guard, lua_guard) {
            (Some(expr), Some(ref lua_guard)) => expr.merge_lua_ast(lua_guard.clone())?,
            (Some(_), None) => self.guard = None,
            (None, Some(lua_guard)) => {
                let mut expr = dummy_expr();

                expr.merge_lua_ast(lua_guard)?;

                self.guard = Some(expr);
            },
            _ => (),
        }

        self.pat.merge_lua_ast(lua_pat)?;

        // TODO: Attrs

        Ok(())
    }
}