Skip to content

Strange behaviour while resoving modules placed in constants #54525

Closed
@weiznich

Description

@weiznich

There is a strange/inconsistent behaviour in the name resolution of models placed in constants.
In detail placing a module inside of a constant works.
Referring directly to types inside the module also works.

const _DUMMY: () = {
    mod some_mod {
        pub struct Bar;
    }
    
    fn foo(_: some_mod::Bar) {}
};

compiles successfully.

Trying to use use statements or starting the path otherwise with self:: results in compiler errors that some_mod is not found:

const _DUMMY: () = {
    mod some_mod {
        pub struct Bar;
    }
    // This fails
    use self::some_mod::Bar;    

    // This also fails
    fn foo(_: self::some_mod::Bar) {}
};

This behaviour seems inconsistent for me. Either it should not be allowed to place modules inside of constants, or it should be possible to use relative paths to reference items inside of such modules (in scope of the constant block).

I would guess that the scope of self is not associated with the inner scope of the constant, but with the current module. From there the module inside of the constant is not visible.

This happens on all channels.

Playground links:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions