Skip to content

Trying to use a assigned value as default value generates code that doesn't compile #159

@hean01

Description

@hean01

compiling following schema into rust code:

World-Schema DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
    SemVer ::= SEQUENCE (SIZE(3)) OF INTEGER
    protocol-version SemVer ::= { 0, 0, 0 }

    Packet ::= SEQUENCE
    {
        protocolVersion SemVer DEFAULT protocol-version
    }
END

It produces the following code:

#[allow(
    non_camel_case_types,
    non_snake_case,
    non_upper_case_globals,
    unused,
    clippy::too_many_arguments
)]
pub mod world_schema {
    extern crate alloc;
    use core::borrow::Borrow;
    use rasn::prelude::*;
    use std::sync::LazyLock;
    #[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, Eq, Hash)]
    #[rasn(automatic_tags)]
    pub struct Packet {
        #[rasn(
            default = "packet_protocol_version_default",
            identifier = "protocolVersion"
        )]
        pub protocol_version: SemVer,
    }
    impl Packet {
        pub fn new(protocol_version: SemVer) -> Self {
            Self { protocol_version }
        }
    }
    impl std::default::Default for Packet {
        fn default() -> Self {
            Self {
                protocol_version: packet_protocol_version_default(),
            }
        }
    }
    fn packet_protocol_version_default() -> SemVer {
        PROTOCOL_VERSION
    }
    #[doc = " Anonymous SEQUENCE OF member "]
    #[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, Eq, Hash)]
    #[rasn(delegate, identifier = "INTEGER")]
    pub struct AnonymousSemVer(pub Integer);
    #[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, Eq, Hash)]
    #[rasn(delegate, size("3"))]
    pub struct SemVer(pub SequenceOf<AnonymousSemVer>);
    pub static PROTOCOL_VERSION: LazyLock<SemVer> = LazyLock::new(|| {
        SemVer(alloc::vec![
            Integer::from(0),
            Integer::from(0),
            Integer::from(0)
        ])
    });
}

Gives me following compilation failure:

error[E0308]: mismatched types
 --> src\generated.rs:1:775
  |
1 | ...ocol_version_default () -> SemVer { PROTOCOL_VERSION } # [doc = " Anonymous SEQUENCE OF member "] # [derive (AsnType , Debu...
  |                               ------   ^^^^^^^^^^^^^^^^ expected `SemVer`, found `LazyLock<SemVer>`
  |                               |
  |                               expected `SemVer` because of return type
  |
  = note: expected struct `SemVer`
             found struct `LazyLock<SemVer>`

error[E0308]: mismatched types
 --> src\generated.rs:1:1269
  |
1 | ...w (|| SemVer (alloc :: vec ! [Integer :: from (0) , Integer :: from (0) , Integer :: from (0)])) ; }
  |                                  ^^^^^^^^^^^^^^^^^^^ expected `AnonymousSemVer`, found `Integer`
  |
help: try wrapping the expression in `generated::world_schema::AnonymousSemVer`
  |
1 | # [allow (non_camel_case_types , non_snake_case , non_upper_case_globals , unused , clippy :: too_many_arguments ,)] pub mod world
_schema { extern crate alloc ; use core :: borrow :: Borrow ; use std :: sync :: LazyLock ; use rasn :: prelude :: * ; # [derive (AsnT
ype , Debug , Clone , Decode , Encode , PartialEq , Eq , Hash)] # [rasn (automatic_tags)] pub struct Packet { # [rasn (default = "pack
et_protocol_version_default" , identifier = "protocolVersion")] pub protocol_version : SemVer , } impl Packet { pub fn new (protocol_v
ersion : SemVer) -> Self { Self { protocol_version } } } impl std :: default :: Default for Packet { fn default () -> Self { Self { pr
otocol_version : packet_protocol_version_default () } } } fn packet_protocol_version_default () -> SemVer { PROTOCOL_VERSION } # [doc
= " Anonymous SEQUENCE OF member "] # [derive (AsnType , Debug , Clone , Decode , Encode , PartialEq , Eq , Hash)] # [rasn (delegate ,
 identifier = "INTEGER")] pub struct AnonymousSemVer (pub Integer) ; # [derive (AsnType , Debug , Clone , Decode , Encode , PartialEq
, Eq , Hash)] # [rasn (delegate , size ("3"))] pub struct SemVer (pub SequenceOf < AnonymousSemVer >) ; pub static PROTOCOL_VERSION :
LazyLock < SemVer > = LazyLock :: new (|| SemVer (alloc :: vec ! [generated::world_schema::AnonymousSemVer(Integer :: from (0)) , Inte
ger :: from (0) , Integer :: from (0)])) ; }
  |








                                                                  +++++++++++++++++++++++++++++++++++++++++                   +

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