chain: drop redundant check - #7095
Conversation
52e60e9 to
44c2cc3
Compare
44c2cc3 to
2746838
Compare
|
@matthewhughes934 Not a huge deal, but since we've added the LLM disclosure checkboxes to our PR template it would be nice to see them checked on all PRs going forward. |
|
Also, when referencing issues or PRs for this repo in commit messages it's probably best to use |
Hm, triagebot has a detection/message for that. EDIT: #7098, we just didn't enable it on this repo. |
In the code: `chain_item` is a _child_ in the chain, and not the root, e.g. `1.foo.bar` in `root.1.foo.bar`. So the question is, can a `try!(..)` expression appear there, e.g. `root.try!(something).0`? My conclusion is that it cannot: firstly note `try!` is not a valid identifier (because of the `!`), so working through the possible chain types (see `pop_expr_chain`): * `MethodCallExpression → Expression . PathExprSegment ( CallParams? )` (e.g. `root.some_method()`): `PathExprSegment` starts with a `PathIdentSegment`, starts with a `IDENTIFIER`: `try!` doesn't match * `FieldExpression → Expression . IDENTIFIER` (e.g. `root.sub`): `try!` doesn't match * `TupleIndexingExpression → Expression . TUPLE_INDEX` (e.g. `root.0`): tuple index is repeated decimal digits: `try!` doesn't match * keywords:`.await`, `.use`, `.yield`: `try!` doesn't match Of course a: `try!` can be the _root_ of a chain `try!(foo).bar` is perfectly valid. This fixes an issue: as previously, if the `use_try_shorthand` config was set we would skip over any comments in any children of the chain, so they would be dropped. The added test case covers this situation. Fixes: rust-lang#6121
2746838 to
d4a22f4
Compare
👍 restored that. I have some muscle memory from keeping the PR description in sync with commits where I'll just
If the commit can potentially end up in rust-lang/rust (this is what the repo syncing does I think?). Then I think I should make an effort to disambiguate it there too, but I've update the description/commit for both those things |
| // FIXME: Figure out the way to get a correct span when converting `try!` to `?`. | ||
| let handle_comment = | ||
| !(context.config.use_try_shorthand() || is_tries(comment_snippet.trim())); | ||
| let handle_comment = !is_tries(comment_snippet.trim()); |
There was a problem hiding this comment.
The PR description didn't fully help me understand why this is a redundant check. Can you try to explain it differently?
Also, is it fine to just remove the FIXME comment?
There was a problem hiding this comment.
Here's my attempt at rewriting description/commit body to be clearer (trying to TL;DR: previously we would skip rewriting comments here if we were removing try! expressions because of how spans work. But in fact, we can never have a try! here, so we don't need to worry about it).
While processing children in a chain, e.g. the 1.foo.bar in
root.1.foo.bar there was a check that would skip handling comments
(and so remove them) if the use_try_shorthand config option was set.
This is because previously in the processing (trace: Chain::from_ast
-> Chain::make_subexpr_list -> Chain::pop_expr_chain) we would
replace try!(..) expressions with ? ones but not update spans,
making it difficult to recover comment snippets.
However, it's not possible for a try! macro to appear as a child, e.g.
root.try!(bar);. Firstly note the try! is not a valid identifier,
then the possible types of chain are:
MethodCallExpression → Expression . PathExprSegment ( CallParams? )
(e.g.root.some_method()):PathExprSegmentstarts with a
PathIdentSegment, starts with aIDENTIFIER:try!cannot be the
start of aPathExprSegmentso we can't havetry!in a method
chain.FieldExpression → Expression . IDENTIFIER(e.g.root.sub):try!
is not a valid identifier, so we can't havetry!as a field
expressionTupleIndexingExpression → Expression . TUPLE_INDEX(e.g.root.0):
tuple index is repeated decimal digits:try!doesn't match- keywords:
.await,.use,.yield:try!doesn't
match
Of course a: try! can be the root of a chain try!(foo).bar is
perfectly valid. And ? can be used anywhere in a chain:
foo.try!(bar) is invalid, but foo.bar? is valid. This change only
focuses on the try! macros.
In the code:
chain_itemis a child in the chain, and not the root,e.g.
1.foo.barinroot.1.foo.bar. So the question is, can atry!(..)expression appear there, e.g.root.try!(something).0? Myconclusion is that it cannot: firstly note
try!is not a valididentifier (because of the
!), so working through the possible chaintypes (see
pop_expr_chain):MethodCallExpression → Expression . PathExprSegment ( CallParams? )(e.g.
root.some_method()):PathExprSegmentstarts with aPathIdentSegment, starts with aIDENTIFIER:try!doesn't matchFieldExpression → Expression . IDENTIFIER(e.g.root.sub):try!doesn't match
TupleIndexingExpression → Expression . TUPLE_INDEX(e.g.root.0):tuple index is repeated decimal digits:
try!doesn't match.await,.use,.yield:try!doesn'tmatch
Of course a:
try!can be the root of a chaintry!(foo).barisperfectly valid.
This fixes an issue: as previously, if the
use_try_shorthandconfigwas set we would skip over any comments in any children of the chain,
so they would be dropped. The added test case covers this situation.
Fixes: #6121