solar-lsp 0.2.0

Solidity LSP
Documentation
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
use super::support::RequestFixture;
use snapbox::str;

#[test]
fn returns_parameter_hints_for_positional_calls_and_skips_named_args() {
    let fixture = RequestFixture::new(
        r#"
        //- /Hints.sol
        contract C {
            function target(uint256 amount, address account) public pure returns (uint256) {
                return amount;
            }

            function caller(address user) public pure returns (uint256) {
                return target(1, user) + target({amount: 2, account: user});
            }
        }
        "#,
        "/Hints.sol",
    );

    fixture.check_inlay_hints(
        "/Hints.sol",
        str![[r#"
PARAMETER amount:
PARAMETER account:
TYPE : uint256
TYPE : uint256

"#]],
    );
}

#[test]
fn uses_selected_overload_for_parameter_hints() {
    let fixture = RequestFixture::new(
        r#"
        //- /Overload.sol
        contract C {
            function f(uint256 value) public pure returns (uint256) {
                return value;
            }

            function f(string memory text) public pure returns (uint256) {
                return bytes(text).length;
            }

            function caller() public pure returns (uint256) {
                return f("abc");
            }
        }
        "#,
        "/Overload.sol",
    );

    fixture.check_inlay_hints(
        "/Overload.sol",
        str![[r#"
PARAMETER text:
TYPE : uint256

"#]],
    );
}

#[test]
fn skips_attached_using_receiver_for_parameter_hints() {
    let fixture = RequestFixture::new(
        r#"
        //- /Using.sol
        library L {
            function add(uint256 self, uint256 amount) internal pure returns (uint256) {
                return self + amount;
            }
        }

        using L for uint256;

        contract C {
            function caller(uint256 value) public pure returns (uint256) {
                return value.add(3);
            }
        }
        "#,
        "/Using.sol",
    );

    fixture.check_inlay_hints(
        "/Using.sol",
        str![[r#"
PARAMETER amount:
TYPE : uint256

"#]],
    );
}

#[test]
fn skips_parameter_hints_for_arguments_with_matching_names() {
    let fixture = RequestFixture::new_allowing_diagnostics(
        r#"
        //- /Names.sol
        contract C {
            error Bad(uint256 code, address account);

            function target(uint256 amount, address account) public pure returns (uint256) {
                return amount;
            }

            function caller(address account, uint256 amount) public pure returns (uint256) {
                uint256 bothSame = target(amount, account);
                uint256 secondSame = target(1, account);
                return bothSame + secondSame;
            }

            function fail(address user) public pure {
                revert Bad(7, account);
            }
        }
        "#,
        "/Names.sol",
    );

    fixture.check_inlay_hints(
        "/Names.sol",
        str![[r#"
TYPE : uint256
PARAMETER amount:
TYPE : uint256
PARAMETER code:

"#]],
    );
}

#[test]
fn handles_contextual_builtin_callees() {
    let fixture = RequestFixture::new(
        r#"
        //- /Builtins.sol
        type MyUdvt is uint256;

        contract C {
            uint256[] xs;

            function run(uint256 x, MyUdvt y) public returns (uint256) {
                xs.push(1);
                xs.pop();

                MyUdvt wrapped = MyUdvt.wrap(x);
                uint256 unwrapped = MyUdvt.unwrap(y);

                return MyUdvt.unwrap(wrapped) + unwrapped;
            }
        }
        "#,
        "/Builtins.sol",
    );

    fixture.check_inlay_hints(
        "/Builtins.sol",
        str![[r#"
TYPE : MyUdvt
TYPE : uint256
TYPE : uint256

"#]],
    );
}

#[test]
fn skips_inlay_hints_inside_inline_assembly() {
    let fixture = RequestFixture::new(
        r#"
        //- /Assembly.sol
        contract C {
            function run() public pure {
                assembly {
                    let y := add(1, 2)
                }
            }
        }
        "#,
        "/Assembly.sol",
    );

    fixture.check_inlay_hints(
        "/Assembly.sol",
        str![[r#"
"#]],
    );
}

#[test]
fn skips_type_hints_for_explicit_casts() {
    let fixture = RequestFixture::new(
        r#"
        //- /Casts.sol
        contract Target {}

        enum SomeEnum { A, B }

        contract Repro {
            function value() public pure returns (uint256) {
                return 1;
            }

            function run(address addr) public pure returns (SomeEnum) {
                Target t = Target(addr);
                SomeEnum e = SomeEnum(0);
                uint256 n = uint256(1);
                uint256 x = value();
                return e;
            }
        }
        "#,
        "/Casts.sol",
    );

    fixture.check_inlay_hints(
        "/Casts.sol",
        str![[r#"
TYPE : uint256

"#]],
    );
}

#[test]
fn returns_parameter_hints_for_solidity_callable_forms() {
    let fixture = RequestFixture::new(
        r#"
        //- /Forms.sol
        contract BaseList {
            constructor(uint256 baseValue) {}
        }

        contract BaseCtor {
            constructor(uint256 ctorValue) {}
        }

        contract C is BaseList(1), BaseCtor {
            struct Pair { uint256 left; uint256 right; }
            event Seen(uint256 indexed id, address account);
            error Bad(uint256 code, address account);

            modifier only(uint256 requiredValue) {
                _;
            }

            constructor() BaseCtor(2) {}

            function run(address user) public only(3) {
                Pair memory pair = Pair(4, 5);
                emit Seen(6, user);
                revert Bad(7, user);
            }
        }
        "#,
        "/Forms.sol",
    );

    fixture.check_inlay_hints(
        "/Forms.sol",
        str![[r#"
PARAMETER baseValue:
PARAMETER ctorValue:
PARAMETER requiredValue:
PARAMETER left:
PARAMETER right:
PARAMETER id:
PARAMETER account:
PARAMETER code:
PARAMETER account:

"#]],
    );
}

#[test]
fn returns_parameter_hints_for_new_contract_constructor_calls() {
    let fixture = RequestFixture::new(
        r#"
        //- /New.sol
        contract Target {
            constructor(uint256 amount, address owner) {}
        }

        contract C {
            function run(address user) public {
                Target deployed = new Target(1, user);
            }
        }
        "#,
        "/New.sol",
    );

    fixture.check_inlay_hints(
        "/New.sol",
        str![[r#"
PARAMETER amount:
PARAMETER owner:
TYPE : contract Target

"#]],
    );
}

#[test]
fn returns_call_type_hints_for_non_unit_calls() {
    let fixture = RequestFixture::new(
        r#"
        //- /Types.sol
        contract C {
            function value() public pure returns (uint256) {
                return 1;
            }

            function sideEffect(uint256 input) public pure {
                input;
            }

            function caller() public pure {
                uint256 x = value();
                sideEffect(x);
            }
        }
        "#,
        "/Types.sol",
    );

    fixture.check_inlay_hints(
        "/Types.sol",
        str![[r#"
TYPE : uint256
PARAMETER input:

"#]],
    );
}

#[test]
fn displays_multi_return_call_type_hints_as_solidity_tuples() {
    let fixture = RequestFixture::new(
        r#"
        //- /MultiReturn.sol
        contract C {
            function pair(uint256 amount) internal pure returns (uint256, bool) {
                return (amount, amount != 0);
            }

            function caller() public pure returns (uint256, bool) {
                return pair(1);
            }
        }
        "#,
        "/MultiReturn.sol",
    );

    fixture.check_inlay_hints(
        "/MultiReturn.sol",
        str![[r#"
PARAMETER amount:
TYPE : (uint256, bool)

"#]],
    );
}

#[test]
fn uses_function_type_parameter_names_for_function_variable_calls() {
    let fixture = RequestFixture::new_allowing_diagnostics(
        r#"
        //- /FunctionType.sol
        contract C {
            function target(uint256 amount, address account) internal pure returns (uint256) {
                return amount;
            }

            function caller(address user) public pure returns (uint256) {
                function(uint256 amount, address account) internal pure returns (uint256) f = target;
                return f(1, user);
            }
        }
        "#,
        "/FunctionType.sol",
    );

    fixture.check_inlay_hints(
        "/FunctionType.sol",
        str![[r#"
PARAMETER amount:
PARAMETER account:
TYPE : uint256

"#]],
    );
}

#[test]
fn uses_function_type_parameter_names_for_struct_field_calls() {
    let fixture = RequestFixture::new_allowing_diagnostics(
        r#"
        //- /FunctionField.sol
        contract C {
            struct Holder {
                function(uint256 amount, address account) internal returns (uint256) callback;
            }

            Holder holder;

            function caller(address user) public returns (uint256) {
                return holder.callback(1, user);
            }
        }
        "#,
        "/FunctionField.sol",
    );

    fixture.check_inlay_hints(
        "/FunctionField.sol",
        str![[r#"
PARAMETER amount:
PARAMETER account:
TYPE : uint256

"#]],
    );
}

#[test]
fn uses_target_parameter_names_for_abi_encode_call_tuple() {
    let fixture = RequestFixture::new(
        r#"
        //- /AbiEncodeCall.sol
        interface I {
            function target(uint256 amount, address account) external returns (uint256);
        }

        contract C {
            function caller(address user) public pure returns (bytes memory) {
                return abi.encodeCall(I.target, (1, user));
            }
        }
        "#,
        "/AbiEncodeCall.sol",
    );

    fixture.check_inlay_hints(
        "/AbiEncodeCall.sol",
        str![[r#"
PARAMETER amount:
PARAMETER account:
TYPE : bytes memory

"#]],
    );
}

#[test]
fn skips_abi_encode_call_parameter_hints_for_tuple_arity_mismatch() {
    let fixture = RequestFixture::new_allowing_diagnostics(
        r#"
        //- /AbiEncodeCallArityMismatch.sol
        interface I {
            function target(uint256 amount, address account) external returns (uint256);
        }

        contract C {
            function caller(address user) public pure returns (bytes memory) {
                return abi.encodeCall(I.target, (1, user, 3));
            }
        }
        "#,
        "/AbiEncodeCallArityMismatch.sol",
    );

    fixture.check_inlay_hints(
        "/AbiEncodeCallArityMismatch.sol",
        str![[r#"
TYPE : bytes memory

"#]],
    );
}

#[test]
fn uses_target_parameter_names_for_single_abi_encode_call_argument() {
    let fixture = RequestFixture::new(
        r#"
        //- /AbiEncodeCallSingle.sol
        interface I {
            function target(uint256 amount) external returns (uint256);
        }

        contract C {
            function caller() public pure returns (bytes memory) {
                return abi.encodeCall(I.target, 1);
            }
        }
        "#,
        "/AbiEncodeCallSingle.sol",
    );

    fixture.check_inlay_hints(
        "/AbiEncodeCallSingle.sol",
        str![[r#"
PARAMETER amount:
TYPE : bytes memory

"#]],
    );
}

#[test]
fn skips_abi_encode_call_parameter_hints_for_tuple_holes() {
    let fixture = RequestFixture::new_allowing_diagnostics(
        r#"
        //- /AbiEncodeCallHole.sol
        interface I {
            function target(uint256 amount, address account) external returns (uint256);
        }

        contract C {
            function caller(address user) public pure returns (bytes memory) {
                return abi.encodeCall(I.target, (, user));
            }
        }
        "#,
        "/AbiEncodeCallHole.sol",
    );

    fixture.check_inlay_hints(
        "/AbiEncodeCallHole.sol",
        str![[r#"
TYPE : bytes memory

"#]],
    );
}

#[test]
fn filters_hints_by_requested_range() {
    let fixture = RequestFixture::new(
        r#"
        //- /Range.sol
        contract C {
            function f(uint256 first, uint256 second) public pure returns (uint256) {
                return first + second;
            }

            function caller() public pure returns (uint256) {
                $1uint256 a = f(1, 2);
                $2uint256 b = f(3, 4);
                return a + b;
            }
        }
        "#,
        "/Range.sol",
    );

    fixture.check_inlay_hints_between(
        "$1",
        "$2",
        str![[r#"
PARAMETER first:
PARAMETER second:
TYPE : uint256

"#]],
    );
}