4

feat: inline const as literal by viktorlott · Pull Request #14925 · rust-lang/ru...

 10 months ago
source link: https://github.com/rust-lang/rust-analyzer/pull/14925
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Conversation

Contributor

Assist: inline_const_as_literal

Evaluate and inline const variable as literal.

const STRING: &str = "Hello, World!";

fn something() -> &'static str {
    STR$0ING
}

->

const STRING: &str = "Hello, World!";

fn something() -> &'static str {
    "Hello, World!"
}

rustbot

added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label

May 29, 2023

viktorlott

force-pushed the inline-const-expr-as-static-str

branch 6 times, most recently from 489d5e0 to 1e803cc Compare

May 29, 2023 20:53

viktorlott

marked this pull request as ready for review

May 29, 2023 21:10

Member

Interesting assist. Why it's name is limited to string literals? I think the current code works with integers and other kinds of literals without any changes, and with some trivial changes it could support arrays and tuples (supporting structs and enums are a little harder I think)

Veykril reacted with thumbs up emoji

Contributor

Author

Interesting assist. Why it's name is limited to string literals? I think the current code works with integers and other kinds of literals without any changes, and with some trivial changes it could support arrays and tuples (supporting structs and enums are a little harder I think)

That was actually my initial intention, but I decided to scope it to &str because I had some trouble applying effective tests for all the cases (e.g testing built-in/proc macros and evaluating blocks with resource locks). But yeah, we could rescope it such that other primitive types also work.

Should I include lazy const expressions also, or just avoid ADTs all together?

Member

That was actually my initial intention, but I decided to scope it to &str because I had some trouble applying effective tests for all the cases (e.g testing built-in/proc macros and evaluating blocks with resource locks). But yeah, we could rescope it such that other primitive types also work.

It's fine if this PR only supports &str and other cases are deferred to future (I think it already supports some more things, but anyway), just using general words for name and message of the assist is good enough for start.

Should I include lazy const expressions also, or just avoid ADTs all together?

By those, you mean expressions that contains closures? I guess those are theoretically impossible to support. For adt the problem is private fields, unresolved names, names that need to be imported and similar things, so I think it should avoid ADTs.

viktorlott reacted with thumbs up emoji

viktorlott

force-pushed the inline-const-expr-as-static-str

branch 2 times, most recently from 579db63 to ae3be8e Compare

May 31, 2023 10:34

viktorlott

changed the title feat: inline const expr as static str

feat: inline const as literal

May 31, 2023

viktorlott

force-pushed the inline-const-expr-as-static-str

branch 3 times, most recently from 21d83a5 to d68d11e Compare

May 31, 2023 16:33

Comment on lines

+683 to +713

fn inline_const_as_literal_expr_as_str_lit_not_applicable() {

check_assist_not_applicable(

inline_const_as_literal,

r#"

Can you also add some negative tests for cases that their type is not supported (adt, closure, ...)?

Contributor

Author

Absolutely,
Are the newly added tests enough, or should I add more variance to them?

Member

#14947 fixes some of the render_eval problems in this PR tests.

viktorlott reacted with heart emoji

Contributor

Author

#14947 fixes some of the render_eval problems in this PR tests.

Great! I'll take a look at your comments after work

HKalbasi reacted with thumbs up emoji

Member

Thanks!
@bors r+

Collaborator

pushpin Commit eef716d has been approved by HKalbasi

It is now in the queue for this repository.

Collaborator

hourglass Testing commit eef716d with merge 058e2d2...

Collaborator

sunny Test successful - checks-actions
Approved by: HKalbasi
Pushing 058e2d2 to master...

bors

merged commit 058e2d2 into

rust-lang:master

Jun 6, 2023

10 checks passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

HKalbasi

HKalbasi left review comments
Assignees

No one assigned

Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

None yet

4 participants

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK