Skip to content

bug(forge fmt): formatter breaks on function named layout #10995

@crystalbit

Description

@crystalbit

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

1.2.3-nightly

What version of Foundryup are you on?

foundryup --version doesn't print the version, it is for installing specific version

What command(s) is the bug in?

forge fmt

Operating System

macOS (Apple Silicon)

Describe the bug

Ok, I understood the reason of the issue while writing it here. But it is still an issue, maybe just minor.

I have a function named layout and forge fmt is breaking on it.

Obviously it is because we now have a keyword layout at as well and currently it isn't restricted to name methods with it

My lib:

// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.30;

library BorrowerVaultStorageLib {
    bytes32 private constant STORAGE_LOCATION = keccak256(abi.encode(uint256(keccak256("diffuse.borrowervault.storage")) - 1)) & ~bytes32(uint256(0xff));

    /// @custom:storage-location erc7201:diffuse.borrowervault.storage
    struct Layout {
        mapping(address user => uint256 amount) borrowerAssetBalances;
        uint256 totalCollateralProvided;
        uint256 assetUtilization;
        uint256 strategyBalance;
        bool strategyEntered;
        mapping(address user => uint256 maxLoss) maxLosses;
        mapping(address user => uint256 leverage) leverages;
    }

    function layout() internal pure returns (Layout storage $) {
        bytes32 slot = STORAGE_LOCATION;
        assembly {
            $.slot := slot
        }
    }

    function getBorrowerAssetBalance(address user) internal view returns (uint256) {
        return layout().borrowerAssetBalances[user];
    }

} 

Output:

% forge fmt
Warning: This is a nightly build of Foundry. It is recommended to use the latest stable version. To mute this warning set `FOUNDRY_DISABLE_NIGHTLY_WARNING` in your environment. 

Error: Failed to parse Solidity code for src/libs/BorrowerVaultStorageLib.sol. Leaving source unchanged.

Context:
- failed to parse file:
Error: ParserError
    ╭─[ :27:16 ]
    │
 27 │         return layout().borrowerAssetBalances[user];
    │                ───┬──  
    │                   ╰──── unrecognised token 'layout', expected identifier, string, hexstring, address, number, rational, hexnumber, ";", "(", "+", "-", "!", "~", "++", "--", "[", Uint, Int, Bytes, "byte", "bool", "address", "string", "bytes", "delete", "new", "type", "true", "false", "payable", "function", "revert", "mapping", "at", "leave", "switch", "case", "default"
────╯
Error: ParserError
    ╭─[ :27:24 ]
    │
 27 │         return layout().borrowerAssetBalances[user];
    │                        ┬  
    │                        ╰── unrecognised token '.', expected identifier, ";", "{", "public", "private", "external", "internal", "constant", "pure", "view", "payable", "returns", "return", "revert", "layout", "at", "virtual", "override", "immutable", "leave", "switch", "case", "default"
────╯
Error: ParserError
    ╭─[ :27:46 ]
    │
 27 │         return layout().borrowerAssetBalances[user];
    │                                              ┬  
    │                                              ╰── unrecognised token '[', expected identifier, ";", "{", "(", ".", "public", "private", "external", "internal", "constant", "pure", "view", "payable", "returns", "return", "revert", "layout", "at", "virtual", "override", "immutable", "leave", "switch", "case", "default"
────╯
Error: ParserError
    ╭─[ :27:51 ]
    │
 27 │         return layout().borrowerAssetBalances[user];
    │                                                   ┬  
    │                                                   ╰── unrecognised token ']', expected identifier, ";", "{", "(", ".", "public", "private", "external", "internal", "constant", "pure", "view", "payable", "returns", "return", "revert", "layout", "at", "virtual", "override", "immutable", "leave", "switch", "case", "default"
────╯
Error: ParserError
    ╭─[ :30:1 ]
    │
 30 │ }
    │ ┬  
    │ ╰── unrecognised token '}', expected identifier, annotation, string, hexstring, address, number, rational, hexnumber, ";", "(", "[", Uint, Int, Bytes, "byte", "struct", "import", "contract", "pragma", "bool", "address", "string", "bytes", "interface", "library", "event", "enum", "type", "true", "false", "payable", "function", "mapping", "at", "abstract", "using", "leave", "switch", "case", "default"
────╯

forge build still works

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions