E4034

E4034#

发现多个可能的结构体类型,请添加更多注释。

错误示例:#

pub struct S {
  x : Int
  y : Int
}

pub struct T {
  x : Int
  y : Int
}

pub fn make() -> Unit {
  let _ = { x: 2, y: 1 }
}

在上述例子中,试图将一个带有字段 xy 的结构体赋值给一个变量 c,但这个字段组合同时匹配了 ST 两种类型,会在第 4 行报错:

Multiple possible struct types detected: T, S, please add more annotation.

建议#

通过添加类型注释来去歧义:

pub struct S {
  x : Int
  y : Int
}

pub struct T {
  x : Int
  y : Int
}

pub let c : S = { x: 2, y: 1 }
pub let c2 : S = S::{ x: 2, y: 1 }
pub let t : T = { x: 0, y: 0 }