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

Use String or Int to set the opt level #113273

Merged
merged 1 commit into from
Jul 9, 2023

Conversation

Rustin170506
Copy link
Member

Address https://github.com/rust-lang/rust/pull/112756/files#r1249345725

Use String or Int to set the opt level.

r? @jyn514

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels Jul 3, 2023
src/bootstrap/config.rs Outdated Show resolved Hide resolved
@jyn514
Copy link
Member

jyn514 commented Jul 3, 2023

don't have time to review this, sorry

r? bootstrap

@rustbot rustbot assigned Mark-Simulacrum and unassigned jyn514 Jul 3, 2023
src/bootstrap/config.rs Outdated Show resolved Hide resolved
@Rustin170506 Rustin170506 requested a review from Kobzol July 4, 2023 00:05
src/bootstrap/config.rs Outdated Show resolved Hide resolved
src/bootstrap/config.rs Outdated Show resolved Hide resolved
@Rustin170506 Rustin170506 force-pushed the rustin-patch-opt-level branch from d0e3f91 to 7ec52cf Compare July 5, 2023 00:36
@Rustin170506 Rustin170506 requested a review from Kobzol July 5, 2023 00:37
@Rustin170506
Copy link
Member Author

Thanks for your review! 💚 💙 💜 💛 ❤️

Copy link
Contributor

@Kobzol Kobzol left a comment

Choose a reason for hiding this comment

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

I tried it locally. When you enter anything else than the correct values, the only error you will get is this:

failed to parse TOML configuration 'config.toml': data did not match any variant of untagged enum RustOptimize for key `rust.optimize`

It is because the enum is marked as #[serde(untagged)], so when all the individual variants fail to parse, then it fails with this generic message.

This error message is not very good, and basically it makes your error checking code useless. I think that it would be better to just implement deserialize for the whole enum, and combine all the parsing code in a single function, to provide a reasonable error message. So first try to parse as bool, if it works, return bool. If not, try to parse as string, if it's "s"/"z", return string. Lastly, try to parse as an u8, if it's <= 3, return int. Otherwise show the full error message.

It would also be nice to document the possible values to config.example.toml, to the rust.optimize key comment.

@Rustin170506
Copy link
Member Author

Tested locally:

rust git:(master) ✗ ./x.py build
Building bootstrap
    Finished dev [unoptimized] target(s) in 0.12s
failed to parse TOML configuration 'config.toml': unrecognized option for rust optimize: "9", expected one of 0, 1, 2, 3, "s", "z", true, false for key `rust.optimize`

@Rustin170506 Rustin170506 force-pushed the rustin-patch-opt-level branch from 6527735 to 7b57df8 Compare July 6, 2023 01:39
Copy link
Member Author

@Rustin170506 Rustin170506 left a comment

Choose a reason for hiding this comment

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

Self check

@Rustin170506 Rustin170506 requested a review from Kobzol July 6, 2023 02:22
src/bootstrap/config.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Kobzol Kobzol left a comment

Choose a reason for hiding this comment

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

I was hoping that we could implement the deserialize method more nicely, like this:

impl<'de> Deserialize<'de> for RustOptimize {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::Error;
        if let Ok(value) = bool::deserialize(deserializer) {
            return Ok(RustOptimize::Bool(value));
        }
        if let Ok(value) = u8::deserialize(deserializer) {
            if value <= 3 {
                return Ok(RustOptimize::Int(value));
            }
        }
        if let Ok(value) = String::deserialize(deserializer) {
            if value == "s" || value == "z" {
                return Ok(RustOptimize::String(value));
            }
        }
        return Err(D::Error::custom(
            r#"unrecognized option for rust optimize: "{}", expected one of 0, 1, 2, 3, "s", "z", true, false"#,
        ));
    }
}

but sadly it seems that this is not possible, because we cannot use deserializer multiple times. It would be possible with serde::Value, but I don't want to include another dependency just for this or use serde_json::Value. So let's keep it as it is :)

Delete the visit_i64 method and I think that this is good to go.

src/bootstrap/config.rs Outdated Show resolved Hide resolved
@Rustin170506
Copy link
Member Author

Rustin170506 commented Jul 6, 2023

Delete the visit_i64 method and I think that this is good to go.

If we don't add it then we can not handle rust.optimize = 0. We can handle it as i64 to avoid this issue.

@Kobzol
Copy link
Contributor

Kobzol commented Jul 6, 2023

Oh, damn, good point. Ok, then let's delete visit_u64 instead and do e.g. this:

fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
    E: serde::de::Error,
{
    if matches!(value, 0..=3) {
        Ok(RustOptimize::Int(value as u8))
    } else {
        Err(format_optimize_error_msg(value)).map_err(serde::de::Error::custom)
    }
}

@Rustin170506 Rustin170506 force-pushed the rustin-patch-opt-level branch from 7b57df8 to 4024836 Compare July 6, 2023 12:41
@Rustin170506 Rustin170506 requested a review from Kobzol July 6, 2023 12:42
@Kobzol
Copy link
Contributor

Kobzol commented Jul 6, 2023

Great, thanks!

@jyn514 I'm not part of the bootstrap team, are you OK with me approving this?

@Mark-Simulacrum
Copy link
Member

Feel free to approve. It would be good to squash commits here though.

@Mark-Simulacrum
Copy link
Member

r? @Kobzol

@rustbot rustbot assigned Kobzol and unassigned Mark-Simulacrum Jul 9, 2023
@Kobzol
Copy link
Contributor

Kobzol commented Jul 9, 2023

Ok, @hi-rustin please squash the commits (let me know if you want me to help with that!) and I'll approve it.

Signed-off-by: hi-rustin <rustin.liu@gmail.com>
@Rustin170506 Rustin170506 force-pushed the rustin-patch-opt-level branch from 4024836 to 92b5d0c Compare July 9, 2023 05:53
@Rustin170506
Copy link
Member Author

Ok, @hi-rustin please squash the commits (let me know if you want me to help with that!) and I'll approve it.

Done.
Thanks for your review! 💚 💙 💜 💛 ❤️

@Kobzol
Copy link
Contributor

Kobzol commented Jul 9, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Jul 9, 2023

📌 Commit 92b5d0c has been approved by Kobzol

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 9, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 9, 2023
…r=Kobzol

Use String or Int to set the opt level

Address https://github.com/rust-lang/rust/pull/112756/files#r1249345725

Use String or Int to set the opt level.

r? `@jyn514`
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 9, 2023
…iaskrgr

Rollup of 5 pull requests

Successful merges:

 - rust-lang#111618 (Always name the return place.)
 - rust-lang#113247 (Add Tests for native wasm exceptions)
 - rust-lang#113273 (Use String or Int to set the opt level)
 - rust-lang#113469 (Remove `default_free_fn` feature)
 - rust-lang#113493 (additional io::copy specializations)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a46589f into rust-lang:master Jul 9, 2023
@rustbot rustbot added this to the 1.73.0 milestone Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants