Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incomplete code snippets in compiler output #5499

Open
ironcev opened this issue Jan 21, 2024 · 0 comments
Open

Incomplete code snippets in compiler output #5499

ironcev opened this issue Jan 21, 2024 · 0 comments
Labels
bug Something isn't working compiler General compiler. Should eventually become more specific as the issue is triaged

Comments

@ironcev
Copy link
Member

ironcev commented Jan 21, 2024

When displaying additional code snippets in the compiler output (those are hints, and notes that are coming from other parts of the same file or from other files), the snippets are sometimes missing the first line and showing only the last one.

Here is a reproducible example. Create a new script with the following library in the file called lib.sw:

library;

pub
    const
        LIB_X = 3;

Import the LIB_X and shadow it in the main.sw:

script;

mod lib;

fn main() {
    let LIB_X_ALIAS = 7;
}

use lib::LIB_X as LIB_X_ALIAS;

The compiler error will look like this. Note that the info snippet is missing the opening pub line.

Multiline issue

A quick experimenting indicates that the issue is there only with the small number of lines of code (three in this example). If the snippet is larger in terms of lines of code, the first line gets properly rendered.

The bug stayed also after moving to the latest version of annotate_snippets. For the in-depth analysis - this also has to be considered, that the bug is just a configuration of snippet's rendering in annotate_snippets or perhaps a bug in annotate_snippets.

@ironcev ironcev added compiler General compiler. Should eventually become more specific as the issue is triaged bug Something isn't working labels Jan 21, 2024
@ironcev ironcev mentioned this issue Jan 23, 2024
7 tasks
ironcev added a commit that referenced this issue Jan 30, 2024
## Description

This PR makes struct fields private by default and introduces explicit
public struct fields:

```
pub struct Struct {
    pub public_field: u8,
    private_field: u8,    
}
```

Private fields can be accessed only withing the module in which their
struct is declared.

Error messages are properly orchestrated so that no conflicting or
duplicated messages are emitted. Since the change is a breaking change,
relevant suggestion on how to fix the particular issue are given.

To avoid an abrupt breaking change, the errors are temporarily turned
into warnings. These warnings will become errors in the upcoming
versions of Sway. The demo section below demonstrate how the errors will
look like, and how a one warning equivalent looks now.

Standard library structs like `Vec` or `String`, are adapted where
needed by adding the `pub` keyword to the fields that are accessed
outside of the struct declaration module. Some of these fields should
very likely remain public, but some, like e.g., `RawBytes::ptr` or
`Vec::buf` should be private. Adjusting the standard library to properly
utilize private fields is out of scope of this PR and will be done
separately. I expect breaking changes in the STD once we start modeling
structs keeping encapsulation in mind.

In addition, the PR:
- migrates
[annotate_snippets](https://github.com/rust-lang/annotate-snippets-rs)
crate to its latest version (breaking changes)
- removes a redundant non-existing field error and orchestrates
non-existing field error with privacy errors
- replaces an invalid and misleading error when accessing fields on
storage elements that are not structs

Closes #4692.

## Known limitations

There is an issue in displaying multi-line code snippets, which is
independent of these changes, but wasn't apparent before. The issue is
visible in the demo section below, where the struct bodies are sometimes
not fully shown, and they should be. This issue is reported in #5499 and
will be solved in a separate PR.

## Demo (an excerpt 😄)

### Private struct field is inaccessible

![Private struct field is
inaccessible](https://github.com/FuelLabs/sway/assets/4142833/8ac07c2b-8135-470b-ad7a-820a4934f232)

![Private struct field is
inaccessible](https://github.com/FuelLabs/sway/assets/4142833/ca944a7a-e6c4-4b6f-97f1-18000e649452)

### Struct cannot be instantiated

![Struct cannot be instantiated due to inaccessible private
fields](https://github.com/FuelLabs/sway/assets/4142833/05993416-91d6-4f58-8fd6-8c35c23595f8)

![Struct cannot be instantiated due to inaccessible private
fields](https://github.com/FuelLabs/sway/assets/4142833/655c17df-a520-45a0-8af4-f1e424ddf085)

### Struct pattern must ignore inaccessible private fields

![Struct pattern must ignore inaccessible private
fields](https://github.com/FuelLabs/sway/assets/4142833/90396d14-de63-4b08-9f22-e260f406542d)

### Struct pattern has missing fields

![Struct pattern has missing
fields](https://github.com/FuelLabs/sway/assets/4142833/0ddf44e8-7598-461a-b85b-48006670b0ca)

### Errors temporarily turned into warnings

![Error turned into
warning](https://github.com/FuelLabs/sway/assets/4142833/ba235248-740f-4fd2-b1fa-29fc35ee8c84)

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler General compiler. Should eventually become more specific as the issue is triaged
Projects
None yet
Development

No branches or pull requests

1 participant