Clang error: assignment to cast is illegal, lvalue casts are not supported (err_typecheck_lvalue_casts_not_supported)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: assignment to cast is illegal, lvalue casts are not supported
Type Error
Category Semantic Issue
Internal Id err_typecheck_lvalue_casts_not_supported
Internal Message assignment to cast is illegal, lvalue casts are not supported
Regular Expression (?:error|fatal error)\: assignment to cast is illegal, lvalue casts are not supported
First Commit 2009-03-14 5a8987ca5113 Update tablegen diagnostic files to be in sync with the def files.

Description

The error is issued by the Clang compiler when an attempt is made to assign a value to a casted lvalue, which is not supported in C or C++. An lvalue refers to an object that occupies some identifiable location in memory (i.e., has an address). Casting is the act of converting a variable from one type to another, and an lvalue cast involves casting that results in an lvalue. However, directly assigning a value to such a cast expression is illegal because the cast does not yield an assignable object in the language's semantics.

This limitation arises from the fact that the result of a cast is not an lvalue but a temporary rvalue, which cannot have new values assigned to it. The error aims to prevent operations that are not allowed by the language standards, enforcing type safety and predictability in code behavior. Attempts to bypass this restriction may indicate a misunderstanding of type conversions or an attempt to write to memory locations in an unintended manner.

 
AI Generated

Example

In the following example, an attempt is made to assign a value directly to a casted lvalue, which is a common practice in some programming scenarios aimed at manipulating the value of a dereferenced pointer after performing a type cast. Initially, a pointer to an integer is declared without initialization, pointing to an undefined memory location. The critical line attempts to cast the dereferenced pointer, i.e., the value pointed to by the pointer, to an integer before assigning a new value to it. This operation is illegal because the result of the cast operation does not produce an lvalue that can have a value assigned to it. According to the language rules, the cast produces a temporary rvalue, and attempting to assign a value to it violates the constraints of C and C++, hence triggering the compiler error. This action essentially tries to modify a temporary object, which is disallowed, demonstrating the compiler's role in enforcing type safety and semantics in the language.  
AI Generated


Flags -xc

[Try out in Compiler Explorer]

Source
int main() {
  int *a;

  // Cast dereferenced pointer to int before assignment
  (int)*a = 1;
}
Compiler Output
<source>:5:3: error: assignment to cast is illegal, lvalue casts are not supported


Clang Internals (17.0.6)

Git Commit Message

Update tablegen diagnostic files to be in sync with the def files.

llvm-svn: 67004

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaExpr.cpp (line 14357)

/// CheckForModifiableLvalue - Verify that E is a modifiable lvalue.  If not,
/// emit an error and return true.  If so, return false.
static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) {
  // ...
  case Expr::MLV_LValueCast:
    DiagID = diag::err_typecheck_lvalue_casts_not_supported;

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/Sema/block-misc.c

  • clang/test/Sema/block-misc.c:33:3: error: assignment to cast is illegal, lvalue casts are not supported