File tree Expand file tree Collapse file tree
crates/emmylua_code_analysis/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -64,6 +64,52 @@ mod tests {
6464 ) ) ;
6565 }
6666
67+ #[ test]
68+ fn test_enum ( ) {
69+ let mut ws = VirtualWorkspace :: new ( ) ;
70+ assert ! ( ws. check_code_for_namespace(
71+ DiagnosticCode :: AssignTypeMismatch ,
72+ r#"
73+ ---@enum SubscriberFlags
74+ local SubscriberFlags = {
75+ None = 0,
76+ Tracking = 1 << 0,
77+ Recursed = 1 << 1,
78+ ToCheckDirty = 1 << 3,
79+ Dirty = 1 << 4,
80+ }
81+ ---@class Subscriber
82+ ---@field flags SubscriberFlags
83+
84+ ---@type Subscriber
85+ local subscriber
86+
87+ subscriber.flags = subscriber.flags & ~SubscriberFlags.Tracking -- 被推断为`integer`而不是实际整数值, 允许匹配
88+ "#
89+ ) ) ;
90+
91+ assert ! ( !ws. check_code_for_namespace(
92+ DiagnosticCode :: AssignTypeMismatch ,
93+ r#"
94+ ---@enum SubscriberFlags
95+ local SubscriberFlags = {
96+ None = 0,
97+ Tracking = 1 << 0,
98+ Recursed = 1 << 1,
99+ ToCheckDirty = 1 << 3,
100+ Dirty = 1 << 4,
101+ }
102+ ---@class Subscriber
103+ ---@field flags SubscriberFlags
104+
105+ ---@type Subscriber
106+ local subscriber
107+
108+ subscriber.flags = 9 -- 不允许匹配不上的实际值
109+ "#
110+ ) ) ;
111+ }
112+
67113 #[ test]
68114 fn test_issue_193 ( ) {
69115 let mut ws = VirtualWorkspace :: new ( ) ;
Original file line number Diff line number Diff line change @@ -83,6 +83,19 @@ pub fn check_ref_type_compact(
8383 }
8484 }
8585
86+ // 当 enum 的值全为整数常量时, 可能会用于位运算, 此时右值推断为整数
87+ if union_types
88+ . iter ( )
89+ . all ( |t| matches ! ( t, LuaType :: DocIntegerConst ( _) ) )
90+ {
91+ match compact_type {
92+ LuaType :: Integer => {
93+ return Ok ( ( ) ) ;
94+ }
95+ _ => { }
96+ }
97+ }
98+
8699 let fake_union_type = LuaType :: Union ( LuaUnionType :: new ( union_types) . into ( ) ) ;
87100 return check_general_type_compact (
88101 db,
You can’t perform that action at this time.
0 commit comments