@@ -4,8 +4,8 @@ use emmylua_parser::{
44use rowan:: { NodeOrToken , TextRange } ;
55
66use crate :: {
7- diagnostic:: checker:: assign_type_mismatch:: check_table_expr, humanize_type , DiagnosticCode ,
8- LuaSemanticDeclId , LuaSignatureId , LuaType , RenderLevel , SemanticDeclLevel , SemanticModel ,
7+ diagnostic:: checker:: { assign_type_mismatch:: check_table_expr, humanize_lint_type } ,
8+ DiagnosticCode , LuaSemanticDeclId , LuaSignatureId , LuaType , SemanticDeclLevel , SemanticModel ,
99 SignatureReturnStatus , TypeCheckFailReason , TypeCheckResult ,
1010} ;
1111
@@ -127,6 +127,8 @@ fn check_return_stat(
127127 let return_expr_type = & return_expr_types[ 0 ] ;
128128 let return_expr_range = return_expr_ranges[ 0 ] ;
129129 let result = semantic_model. type_check ( check_type, & return_expr_type) ;
130+ dbg ! ( & check_type) ;
131+ dbg ! ( & return_expr_type) ;
130132 if !result. is_ok ( ) {
131133 if return_expr_type. is_table ( ) {
132134 if let Some ( return_expr) = return_exprs. get ( 0 ) {
@@ -160,34 +162,6 @@ fn check_return_stat(
160162 Some ( ( ) )
161163}
162164
163- // fn check_variadic_return_type_match(
164- // context: &mut DiagnosticContext,
165- // semantic_model: &SemanticModel,
166- // start_idx: usize,
167- // variadic_type: &LuaType,
168- // return_expr_types: &[LuaType],
169- // return_expr_ranges: &[TextRange],
170- // ) {
171- // let mut idx = start_idx;
172- // for (return_expr_type, return_expr_range) in
173- // return_expr_types.iter().zip(return_expr_ranges.iter())
174- // {
175- // let result = semantic_model.type_check(variadic_type, return_expr_type);
176- // if !result.is_ok() {
177- // add_type_check_diagnostic(
178- // context,
179- // semantic_model,
180- // start_idx + idx,
181- // *return_expr_range,
182- // variadic_type,
183- // return_expr_type,
184- // result,
185- // );
186- // }
187- // idx += 1;
188- // }
189- // }
190-
191165fn add_type_check_diagnostic (
192166 context : & mut DiagnosticContext ,
193167 semantic_model : & SemanticModel ,
@@ -200,47 +174,28 @@ fn add_type_check_diagnostic(
200174 let db = semantic_model. get_db ( ) ;
201175 match result {
202176 Ok ( _) => return ,
203- Err ( reason) => match reason {
204- TypeCheckFailReason :: TypeNotMatchWithReason ( reason) => {
205- context. add_diagnostic (
206- DiagnosticCode :: ReturnTypeMismatch ,
207- range,
208- t ! (
209- "Annotations specify that return value %{index} has a type of `%{source}`, returning value of type `%{found}` here instead. %{reason}" ,
210- index = index + 1 ,
211- source = humanize_type( db, & param_type, RenderLevel :: Simple ) ,
212- found = humanize_type( db, & expr_type, RenderLevel :: Simple ) ,
213- reason = reason
214- )
215- . to_string ( ) ,
216- None ,
217- ) ;
218- }
219- TypeCheckFailReason :: TypeNotMatch => {
220- context. add_diagnostic (
221- DiagnosticCode :: ReturnTypeMismatch ,
222- range,
223- t ! (
224- "Annotations specify that return value %{index} has a type of `%{source}`, returning value of type `%{found}` here instead. %{reason}" ,
225- index = index + 1 ,
226- source = humanize_type( db, & param_type, RenderLevel :: Simple ) ,
227- found = humanize_type( db, & expr_type, RenderLevel :: Simple ) ,
228- reason = ""
229- )
230- . to_string ( ) ,
231- None ,
232- ) ;
233- }
234- TypeCheckFailReason :: TypeRecursion => {
235- context. add_diagnostic (
236- DiagnosticCode :: ReturnTypeMismatch ,
237- range,
238- "type recursion" . into ( ) ,
239- None ,
240- ) ;
241- }
242- TypeCheckFailReason :: DonotCheck => { }
243- } ,
177+ Err ( reason) => {
178+ let reason_message = match reason {
179+ TypeCheckFailReason :: TypeNotMatchWithReason ( reason) => reason,
180+ TypeCheckFailReason :: TypeNotMatch | TypeCheckFailReason :: DonotCheck => {
181+ "" . to_string ( )
182+ }
183+ TypeCheckFailReason :: TypeRecursion => "type recursion" . to_string ( ) ,
184+ } ;
185+ context. add_diagnostic (
186+ DiagnosticCode :: ReturnTypeMismatch ,
187+ range,
188+ t ! (
189+ "Annotations specify that return value %{index} has a type of `%{source}`, returning value of type `%{found}` here instead. %{reason}" ,
190+ index = index + 1 ,
191+ source = humanize_lint_type( db, & param_type) ,
192+ found = humanize_lint_type( db, & expr_type) ,
193+ reason = reason_message
194+ )
195+ . to_string ( ) ,
196+ None ,
197+ ) ;
198+ }
244199 }
245200}
246201
0 commit comments