Skip to content

Commit

Permalink
feat: add specific error for attempting string[x] = ".." (#4611)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #4267

## Summary\*

New error:

```bash
error: Strings do not support indexed assignment
  ┌─ /Users/michaelklein/Coding/rust/noir/test_programs/compile_failure/mutable_str_index/src/main.nr:3:5
  │
3 │     example[0] = "?";
  │     -------
  │

Aborting due to 1 previous error
```

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
michaeljklein authored and TomAFrench committed Apr 3, 2024
1 parent 72e5e3d commit f68d2e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ pub enum TypeCheckError {
method_name: String,
span: Span,
},
#[error("Strings do not support indexed assignment")]
StringIndexAssign { span: Span },
}

impl TypeCheckError {
Expand Down Expand Up @@ -237,7 +239,8 @@ impl From<TypeCheckError> for Diagnostic {
| TypeCheckError::ConstrainedReferenceToUnconstrained { span }
| TypeCheckError::UnconstrainedReferenceToConstrained { span }
| TypeCheckError::UnconstrainedSliceReturnToConstrained { span }
| TypeCheckError::NonConstantSliceLength { span } => {
| TypeCheckError::NonConstantSliceLength { span }
| TypeCheckError::StringIndexAssign { span } => {
Diagnostic::simple_error(error.to_string(), String::new(), span)
}
TypeCheckError::PublicReturnType { typ, span } => Diagnostic::simple_error(
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_frontend/src/hir/type_check/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ impl<'interner> TypeChecker<'interner> {
Type::Array(_, elem_type) => *elem_type,
Type::Slice(elem_type) => *elem_type,
Type::Error => Type::Error,
Type::String(_) => {
let (_lvalue_name, lvalue_span) = self.get_lvalue_name_and_span(&lvalue);
self.errors.push(TypeCheckError::StringIndexAssign { span: lvalue_span });
Type::Error
}
other => {
// TODO: Need a better span here
self.errors.push(TypeCheckError::TypeMismatch {
Expand Down

0 comments on commit f68d2e8

Please sign in to comment.