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 different numbers of `#`s when expanding documentation comments #27499

Merged
merged 1 commit into from Feb 3, 2016

Conversation

Projects
None yet
10 participants
@barosl
Copy link
Contributor

barosl commented Aug 3, 2015

Any documentation comments that contain raw-string-looking sequences may pretty-print invalid code when expanding them, as the current logic always uses the r"literal" form, without appending any #s.

This commit calculates the minimum number of #s required to wrap a comment correctly and appends #s appropriately.

Fixes #27489.

@rust-highfive

This comment has been minimized.

Copy link
Collaborator

rust-highfive commented Aug 3, 2015

r? @huonw

(rust_highfive has picked a reviewer for you, use r? to override)

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Aug 4, 2015

This solution seems ok since the pretty-printer isn't relied on for its fidelity, but an alternate approach would be to store this information somewhere so the roundtrip doesn't lose information.

Edit: I think I misunderstood this patch.

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Aug 4, 2015

By looking at the test case exercising this with stringify! my worry is that this behavior might be observable via stable Rust source, and thus have to be specified like so. I think this is not the case and the number of hashes this spits out can't be observed via the macro system. Can anybody confirm?

@huonw

This comment has been minimized.

Copy link
Member

huonw commented Aug 4, 2015

By stable rust source, you mean code will actually stop compiling?

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Aug 4, 2015

@huonw I don't know, maybe. The same way barosol can verify that the pretty printer (which is mostly not part of the Rust language itself) behaves this way via a test case, I'm imagining somebody might be able to write code that compiles and runs that can also detect this behavior - in which case the exact behavior requires more careful consideration than an implementation detail would.

@huonw

This comment has been minimized.

Copy link
Member

huonw commented Aug 4, 2015

Yeah, I guess code could theoretically have an assert_eq!(..., ...) like the test case, but it seems unlikely, and it seems like the new behaviour is strictly better. I'm unsure what the best way forward is.

@brson

This comment has been minimized.

Copy link
Contributor

brson commented Aug 5, 2015

Yes, agree it's better, but not sure if it's the solution that we want to put in the spec. Maybe we can get some other eyes.

@barosl

This comment has been minimized.

Copy link
Contributor Author

barosl commented Aug 9, 2015

So, the thing is that the "encoding" of documentation comments in macros has already been changed heavily recently.

Revision Conversion result Description
Stable #[doc = "/// hello \" world"] Notice " is escaped to \" correctly.
After #26777 #[doc = r"/// hello " world"] The encoding was changed to use raw string literals instead. Notice " is not handled now, which led to the current buggy behavior.
After #27056 #[doc = r" hello " world"] The leading /// was stripped. Although this is a fix for the previous incorrect behavior, it also changed the encoding in an observable way. Also note that there is one excessive leading space that is not stripped, as only the /// was removed, without trimming. This might not be what we want, so the behavior may have to be changed again in the future.

Therefore, the problems I think are:

  1. Is the usage of raw string literals acceptable? The change to raw string literals was done because I thought the previous escaping algorithm didn't work. (And I also introduced this bug. Sorry!) However, it seems that at least " was correctly handled at that time. Should we revert to normal string literals, but improve the escaping algorithm instead this time? Or keep using raw string literals but with the algorithm suggested by this PR applied?
  2. Should the behavior be documented? As I mentioned above, I think the current conversion from /// text to #[doc = r" text"] is not ideal. It should be #[doc = r"text"] (or #[doc = "text"]). Then, should we document this behavior, and make it "stable"? Or can we leave it as an implementation detail?
@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Oct 28, 2015

@rust-lang/lang, this has been waiting on a decision for two (almost three) months now.

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Oct 28, 2015

nominating

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Oct 29, 2015

Discussed at lang team meeting. Consensus was: I do think @brson raised a good point, but I also feel comfortable with this solution. It seems like a strict improvement on the status quo -- we desugar /// into valid #[doc] attributes instead of invalid ones, seems like a win. On the other hand, the fact that stringify! theoretically (at least) introduces a backwards compat. dependency on the current pretty printer (and the /// desugaring) is definitely unacceptable, we should make sure to comment that macro (at minimum) and caveat that the precise output for expressions and non-trivial AST nodes may change.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Nov 3, 2015

@barosl would you mind adding the comments that @nikomatsakis referred to, before we merge this?

@barosl

This comment has been minimized.

Copy link
Contributor Author

barosl commented Nov 3, 2015

Ok, so I guess stringify! should be commented. Are there any other things that can be thought of?

Also, do you think this PR itself is a breaking change? In that case, I will have to append [breaking-change] to the commit message.

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Nov 4, 2015

@barosl I don't consider this a breaking change, personally. It seems POSSIBLE that someone is relying on this, but highly unlikely, so an annotation seems more likely to produce noise than be of use. But others may have another POV.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Nov 4, 2015

I feel the same as @nikomatsakis

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Nov 6, 2015

☔️ The latest upstream changes (presumably #29582) made this pull request unmergeable. Please resolve the merge conflicts.

@durka

This comment has been minimized.

Copy link
Contributor

durka commented Dec 16, 2015

Was there a decision on this?

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Dec 22, 2015

@durka yes: #27499 (comment)

PR is considered OK.

@steveklabnik

This comment has been minimized.

Copy link
Member

steveklabnik commented Dec 31, 2015

@barosl can you rebase this so we can FINALLY get it merged? :)

@barosl barosl force-pushed the barosl:macro-doc-raw-str-hashes branch from 8597e59 to 928fdb2 Jan 18, 2016

Use different numbers of `#`s when expanding documentation comments
Any documentation comments that contain raw-string-looking sequences may
pretty-print invalid code when expanding them, as the current logic
always uses the `r"literal"` form, without appending any `#`s.

This commit calculates the minimum number of `#`s required to wrap a
comment correctly and appends `#`s appropriately.

Fixes #27489.

@barosl barosl force-pushed the barosl:macro-doc-raw-str-hashes branch from 928fdb2 to 1a8cdc0 Jan 18, 2016

@@ -365,6 +365,9 @@ pub mod builtin {
/// stringification of all the tokens passed to the macro. No restrictions
/// are placed on the syntax of the macro invocation itself.
///
/// Note that the expanded results of the input tokens may change in the
/// future. You should be careful if you rely on the output.
///

This comment has been minimized.

@barosl

barosl Jan 18, 2016

Author Contributor

Rebased, sorry for the huge delay.

Also I added this comment. Please tell me if it lacks some information.

@alexcrichton

This comment has been minimized.

Copy link
Member

alexcrichton commented Jan 27, 2016

(ping)

Does this just need a re-review to move forward?

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Feb 2, 2016

@bors r+

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Feb 2, 2016

📌 Commit 1a8cdc0 has been approved by nikomatsakis

@nikomatsakis

This comment has been minimized.

Copy link
Contributor

nikomatsakis commented Feb 2, 2016

@bors rollup

Manishearth added a commit to Manishearth/rust that referenced this pull request Feb 2, 2016

Rollup merge of rust-lang#27499 - barosl:macro-doc-raw-str-hashes, r=…
…nikomatsakis

Any documentation comments that contain raw-string-looking sequences may pretty-print invalid code when expanding them, as the current logic always uses the `r"literal"` form, without appending any `#`s.

This commit calculates the minimum number of `#`s required to wrap a comment correctly and appends `#`s appropriately.

Fixes rust-lang#27489.

bors added a commit that referenced this pull request Feb 3, 2016

@bors bors merged commit 1a8cdc0 into rust-lang:master Feb 3, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.