Skip to content

Emit warnings for unused fields in custom targets. #85775

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

Merged
merged 1 commit into from
Jun 21, 2021

Conversation

adamrk
Copy link
Contributor

@adamrk adamrk commented May 28, 2021

Add a warning which lists any fields in a custom target json file that aren't used. Currently unrecognized fields are ignored so, for example, a typo in the json will silently produce a target which isn't the one intended.

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @matthewjasper (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 28, 2021
@adamrk adamrk marked this pull request as draft May 28, 2021 08:22
@adamrk adamrk force-pushed the warn-unused-target-fields branch from 419130d to 0476cc7 Compare May 28, 2021 08:38
@adamrk adamrk marked this pull request as ready for review May 28, 2021 08:38
@adamrk adamrk changed the title WIP: Emit warnings for unused field in custom targets. Emit warnings for unused field in custom targets. May 28, 2021
@adamrk adamrk changed the title Emit warnings for unused field in custom targets. Emit warnings for unused fields in custom targets. May 28, 2021
@adamrk adamrk force-pushed the warn-unused-target-fields branch from 0476cc7 to 2890879 Compare May 28, 2021 08:39
@ehuss
Copy link
Contributor

ehuss commented May 28, 2021

Just a outside question, would it be feasible to switch to serde for target specs? It would make changes like this much easier, and I think remove a substantial amount of code.

@adamrk
Copy link
Contributor Author

adamrk commented May 29, 2021

Just a outside question, would it be feasible to switch to serde for target specs? It would make changes like this much easier, and I think remove a substantial amount of code.

From what I saw for this change it seems like it could be done, but I didn't look to carefully at the parsing of specific fields. Happy to work on it if people think it makes sense.

@adamrk
Copy link
Contributor Author

adamrk commented Jun 7, 2021

@matthewjasper should I be doing something to get this moving along? Sorry, this is my first time send a patch to rustc, so not exactly sure on the process. And I don't mean to rush you or anything - just want to make sure that this isn't waiting on me.

@adamrk
Copy link
Contributor Author

adamrk commented Jun 14, 2021

r? @nagisa

Copy link
Member

@nagisa nagisa left a comment

Choose a reason for hiding this comment

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

Seems pretty nice. Couple nits inline.

@@ -1204,6 +1204,15 @@ impl Json {
}
}

/// If the Json value is an Object, deletes the value associated with the
/// provided key from the Object and returns it. Otherwise, returns None.
pub fn remove(&mut self, key: &str) -> Option<Json> {
Copy link
Member

Choose a reason for hiding this comment

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

I think it is worthwhile to find a name this method that implies the method operates on maps (remove_key? pop_key? etc.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Went with remove_key since remove is what we call on the underlying map.

}

pub fn warning_message(&self) -> String {
format!("Some fields from target json were unused: {:?}", self.unused_fields)
Copy link
Member

Choose a reason for hiding this comment

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

Worth spending an effort to avoid using debug repr of vector here and making it more prose like. (e.g. one of the

this, that and another fields in the target.json were not used
the target.json file contains unknown fields: this, that, another

or similar.)

Additionally, the warning and error messages should not generally begin with a capital letter because they are already preceded by a error: or warning: .

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! Although I'm not sure if we should use target.json since that won't actually be the name of the file?

.map(|a| a.as_string().unwrap().to_string())
.collect();
if let Some(j) = obj.remove(&name){
if let Some(v) = Json::as_array(&j) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this really wants to be removed (i.e. not warned against) if the value was definitely used. Right now keys that we expect to be arrays, but weren't used because they were not arrays are not warned against.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I ended up splitting this into a separate warnings for unrecognized fields and recognized ones with the wrong json type.

Copy link
Member

Choose a reason for hiding this comment

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

If you don't mind doing this, it would probably be better to adapt these to be a proper error instead – there's no reason for us to let this through, I feel. We already error for most of the other keys, even in cases where type is correct but the value isn't (e.g. MergeFunctions/RelocModel).

Copy link
Member

Choose a reason for hiding this comment

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

Hm, actually I misread the code, we still proceed executing if the type of the value was wrong (but fail if it was right, but with a wrong value). This entire thing probably wants a refactor 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah definitely agree that most of these should be hard errors instead. I made everything warnings in this case just to avoid breaking changes.

What would the process be for changing them to errors? Would it need to be part of the 2021 edition?

Copy link
Member

Choose a reason for hiding this comment

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

Custom target definitions are an “unstable” feature, so introducing breaking changes here is okay without a process as extensive as an edition. Perhaps MCP at most, though I would say that even that is probably excessive in a situation where we're simply no longer accepting what was invalid in the first place.

@adamrk adamrk force-pushed the warn-unused-target-fields branch from 2890879 to 196303c Compare June 17, 2021 16:47
@adamrk
Copy link
Contributor Author

adamrk commented Jun 17, 2021

Thanks for taking a look @nagisa!

@adamrk adamrk force-pushed the warn-unused-target-fields branch 2 times, most recently from a8cb2e0 to 05eb88d Compare June 17, 2021 16:55
@adamrk adamrk force-pushed the warn-unused-target-fields branch from 05eb88d to 88b01f1 Compare June 17, 2021 19:48
@nagisa
Copy link
Member

nagisa commented Jun 20, 2021

@bors r+

@bors
Copy link
Collaborator

bors commented Jun 20, 2021

📌 Commit 88b01f1 has been approved by nagisa

@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 Jun 20, 2021
@adamrk
Copy link
Contributor Author

adamrk commented Jun 20, 2021

Thanks @nagisa! It looks like this'll be blocked until a maintainer approves the CI pipeline?

@nagisa
Copy link
Member

nagisa commented Jun 20, 2021

No, bors will test the PR regardless once the PR gets to the top of its queue.

@bors
Copy link
Collaborator

bors commented Jun 21, 2021

⌛ Testing commit 88b01f1 with merge d789de6...

@bors
Copy link
Collaborator

bors commented Jun 21, 2021

☀️ Test successful - checks-actions
Approved by: nagisa
Pushing d789de6 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 21, 2021
@bors bors merged commit d789de6 into rust-lang:master Jun 21, 2021
@rustbot rustbot added this to the 1.55.0 milestone Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants