Skip to content

Error Message (Dict,Set,List ) Type Mismatch#2577

Closed
assem2002 wants to merge 10 commits intolcompilers:mainfrom
assem2002:main
Closed

Error Message (Dict,Set,List ) Type Mismatch#2577
assem2002 wants to merge 10 commits intolcompilers:mainfrom
assem2002:main

Conversation

@assem2002
Copy link
Copy Markdown
Contributor

@assem2002 assem2002 commented Mar 6, 2024

Fixes #2042

The Fix

Screen Shot Of The Code In The Issue Description After Fix.
Screenshot from 2024-03-06 20-07-53

The issue we're facing could be described as "CONSTRUCTION WHILE ASSIGNING" problem.
It happens when visit_assign function is invoked.
It could happen with annassign function also as shown below

Mention To Some Other Cases

The issue appears in the following code:

someVar : list[i32]
someVar = {1 ,"whatever"}

Screenshot from 2024-03-06 20-12-29

And also here:

@dataclass
class FullFormValue:
    list_i32 : list[i32]
    string   : str
    
ttype    : str = FullFormValue(DEAD_LIST, 1)
contents : FullFormValue = FullFormValue([1, 2], '')
  • The first code and its other forms (set,dict,list) are handled by the fix.
  • The Second code is handling with data class construction, which the fix doesn't provide the same debug message for. but data classes are constructed and get bound to a variable in the same line of code, so it would be easy to see the whole mismatch mess you've made.

**Let me know if the object construction handling is a concern and needs to be debugged in some other way.

Simplifying the code

  • We could write the type check code as a function under ASRUtiles namespace, as it's redundant piece of code here.

  • I don't have deep grasp on the visit_assign function so I didn't mess with its flow because - as I think - there's type coercion happening.

  • I just added a piece of code to solve the problem, but I think the redundancy of the code we face could be handled easily by doing early type check and remove the latter one.

@kmr-srbh
Copy link
Copy Markdown
Contributor

kmr-srbh commented Mar 6, 2024

Please replace "Fix Issue #2042" with "Fixes #2042" so that the PR gets linked to the issue.

@assem2002 assem2002 changed the title Fixing Issue #2042 - CONSTRUCTION WHILE ASSIGNING Fixes #2042 Mar 6, 2024
@assem2002
Copy link
Copy Markdown
Contributor Author

I Edited it, #Thanks @kmr-srbh

@assem2002 assem2002 changed the title Fixes #2042 Fixes #2042 : Error Message (Dict,Set,List ) Type Mismatch Mar 6, 2024
}

}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unsure if this is a good approach. We already catch the SemanticError() in body_visitor().

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also see #2042 (comment).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I don't quite get what you mean by a catch in the same class, I would be glad to know what you have in mind so I can improve the code.
  • I think you mean i have a way of accessing the error without using the try..catch.
  • error recovery is a great idea to have in such a situation -as i read about-, but for now we just brute force on the cases we face; If you want to print both errors i could print out the semantic error and then the semantic abort.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR @assem2002. I appreciate your work. The changes in this PR look good but they seem to be more like hard coded for the specific test and might not be needed at the moment.

I think a better (and more accurate) way to resolve the issue #2042 is to implement error recovery mechanisms (which may/might be a larger issue to be dealt with at the moment).

@Thirumalai-Shaktivel
Copy link
Copy Markdown
Collaborator

Please mark this PR ready for review once it is ready.

@assem2002
Copy link
Copy Markdown
Contributor Author

@Thirumalai-Shaktivel

The problem we face in issue #2042 appears to be more complex.

@Shaikh-Ubaid suggested an error recovery mechanism to be implemented in the system to handle such multiple errors while doing ASR construction.

Probably we should make a bigger issue with specified tasks as long as there's no such mechanism or similar ones already implemented.

I'll draft the PR,but I think the issue should be closed unless a hard-coded fix is fine for now.

@assem2002 assem2002 marked this pull request as draft March 7, 2024 07:10
ASR::ttype_t* list_type = ASRUtils::TYPE(ASR::make_List_t(al, x.base.base.loc, type));
tmp = ASR::make_ListConstant_t(al, x.base.base.loc, list.p,
list.size(), list_type);
throw SemanticError("All List elements must be of the same type for now",
Copy link
Copy Markdown
Collaborator

@Thirumalai-Shaktivel Thirumalai-Shaktivel Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the following idea in mind, check if it's possible to implement or handle. If any blockers, do let me know.

Instead of throw, do diag.add(diag::Diagnostic(...)) and return. Check if diag has some error and throw error based on that in visit_Assign.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay this is doable,but I need to know what benefit we'll get after replacing a throw statement with diag.add(),so I can reason my fix as I don't quite get what you mean .

I think I could handle the error directly without creating ASR element. If that what you imply, that would make the code change in the visit_assign() only, and (visit_set() , visit_dict() , visit_list() ) would remain the same.

@assem2002 assem2002 changed the title Fixes #2042 : Error Message (Dict,Set,List ) Type Mismatch Error Message (Dict,Set,List ) Type Mismatch Mar 21, 2024
@ubaidsk
Copy link
Copy Markdown
Collaborator

ubaidsk commented May 1, 2024

The fix in this PR doesn't seem to be a high priority. I think we should fix it issue by adding support for error-recovery (as mentioned in #2042 (comment)), which in itself is a larger issue. Should we tackle this later @certik?

@certik
Copy link
Copy Markdown
Contributor

certik commented May 2, 2024

Yes, correct. We have to extend the ASR->ASR transformation to just report all errors and recover from them. We should not be catching exceptions like that. In fact ideally we should not use exceptions at all. I think we should be able to recover on the level of statements, and then have special handling in blocks of statements (functions).

In terms of priorities, first we should deliver beta quality ASR and LLVM (via LFortran) for compiling valid code. Only then focus on beautiful error messages for invalid code.

@assem2002 assem2002 closed this May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request better error message for dict and set

5 participants