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

Allow parentheses in dyn (Trait) #48481

Closed
wants to merge 3 commits into from

Conversation

Manishearth
Copy link
Member

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 23, 2018
@petrochenkov
Copy link
Contributor

petrochenkov commented Feb 23, 2018

There's a test compile-fail/dyn-trait-compatibility.rs that's going to be broken by this change and needs updating.
(This is technically a breaking change, but the practical impact should be zero.)

@Manishearth
Copy link
Member Author

Manishearth commented Feb 23, 2018

Oh, right, you can alias FnFoo to dyn. This is unfortunate.

@nikomatsakis is this something we should just land (perhaps w/ crater) or make it an epoch breakage? If we do the latter, bear in mind that there will be no automated way to rustfix &::Foo pre-epoch since &dyn::Foo is interpreted as a path.

@petrochenkov
Copy link
Contributor

We should just land this, IMO, crater won't find anything.

@Manishearth
Copy link
Member Author

brb publishing a crate to prove you wrong 😜

but yeah, perhaps. Though I'm really wary about breaking changes. Feel free to r+

@nikomatsakis
Copy link
Contributor

I agree with @petrochenkov we can probably get away with this one, though like @Manishearth I am getting ever more wary about breaking changes. =)

@nikomatsakis nikomatsakis added the relnotes Marks issues that should be documented in the release notes of the next release. label Feb 23, 2018
@nikomatsakis
Copy link
Contributor

Nonehtless, tagging as relnotes. Description would be something like this:


The dyn keyword now accepts parenthesis, meaning you can do something like dyn (Debug + Send) instead of (dyn Debug + Send). It is technically possible for this to affect existing code, if that code were to alias a Fn trait with the name dyn (e.g., use ::std::ops::Fn as dyn).


@petrochenkov, that's the case you were thinking of, right?

@nikomatsakis
Copy link
Contributor

I would like some tests that cover the precedence and the interesting cases. For example:

let x: &dyn (Debug + Send) = ..; // this should work, right?
let x: &dyn (Debug + Send + Sync) = ..; // this too
let x: &dyn (Debug + Send + ) = ..; // I think we support trailing `+` terminators elsewhere
let x: Box<dyn (Debug + Send) + Sync> = ...; // what about this? =) I forget what we decided there

Also, does this work for impl? It should work analogously, I think.

@Manishearth
Copy link
Member Author

ah, none of those work

@Manishearth
Copy link
Member Author

We don't seem to allow parens in trait bounds, T: (Debug + Send) does not work either.

@nikomatsakis
Copy link
Contributor

@Manishearth yes, I know. I think the way to fix this is by allowing parens in trait bounds, or at least I think that's what @cramertj and I always had in mind (they can confirm)

@nikomatsakis
Copy link
Contributor

This has been a historical point of disagreement with @petrochenkov though =)

@Manishearth
Copy link
Member Author

I can make it work with dyn trait for now; it does not exacerbate any breakages, and is necessary to make &dyn ::Debug + Send work I think

@petrochenkov
Copy link
Contributor

petrochenkov commented Feb 23, 2018

@nikomatsakis

let x: &dyn (Debug + Send) = ..;

This PR doesn't implement that extension, it does other thing - it supports already existing parentheses around a single bound (e.g. impl (Debug) + (Send) is already ok) after dyn specifically, that weren't supported purely for backward compatibility reasons.

@cramertj
Copy link
Member

cramertj commented Feb 24, 2018

My personal preference is for (Bound) to be a valid bound, so that users can write Box<dyn (Bound + Bound)>, &dyn (Bound + Bound + (Bound)), T: (Bound + Bound) etc.

@petrochenkov
Copy link
Contributor

@bors r+

(dyn (Debug + Send) is an orthogonal issue.)

@bors
Copy link
Contributor

bors commented Feb 24, 2018

📌 Commit 4c73f82 has been approved by petrochenkov

@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 Feb 24, 2018
@petrochenkov petrochenkov assigned petrochenkov and unassigned eddyb Feb 24, 2018
Manishearth added a commit to Manishearth/rust that referenced this pull request Feb 24, 2018
@ishitatsuyuki
Copy link
Contributor

Regression in rollup, pretty print test failed.

[01:39:41] ---- [pretty] run-pass/dyn-trait.rs stdout ----
[01:39:41] 	
[01:39:41] error: pretty-printed source does not match expected source
[01:39:41] 
[01:39:41] expected:
[01:39:41] ------------------------------------------
[01:39:41] // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
[01:39:41] // file at the top-level directory of this distribution and at
[01:39:41] // http://rust-lang.org/COPYRIGHT.
[01:39:41] //
[01:39:41] // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
[01:39:41] // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
[01:39:41] // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
[01:39:41] // option. This file may not be copied, modified, or distributed
[01:39:41] // except according to those terms.
[01:39:41] 
[01:39:41] #![feature(dyn_trait)]
[01:39:41] 
[01:39:41] use std::fmt::Display;
[01:39:41] 
[01:39:41] static BYTE: u8 = 33;
[01:39:41] 
[01:39:41] fn main() {
[01:39:41]     let x: &(dyn 'static + Display) = &BYTE;
[01:39:41]     let y: Box<dyn Display + 'static> = Box::new(BYTE);
[01:39:41]     let _: &dyn Display = &BYTE;
[01:39:41]     let _: &dyn ::std::fmt::Display = &BYTE;
[01:39:41]     let xstr = format!("{}" , x);
[01:39:41]     let ystr = format!("{}" , y);
[01:39:41]     assert_eq!(xstr , "33");
[01:39:41]     assert_eq!(ystr , "33");
[01:39:41] }
[01:39:41] 
[01:39:41] ------------------------------------------
[01:39:41] actual:
[01:39:41] ------------------------------------------
[01:39:41] // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
[01:39:41] // file at the top-level directory of this distribution and at
[01:39:41] // http://rust-lang.org/COPYRIGHT.
[01:39:41] //
[01:39:41] // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
[01:39:41] // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
[01:39:41] // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
[01:39:41] // option. This file may not be copied, modified, or distributed
[01:39:41] // except according to those terms.
[01:39:41] 
[01:39:41] #![feature(dyn_trait)]
[01:39:41] 
[01:39:41] use std::fmt::Display;
[01:39:41] 
[01:39:41] static BYTE: u8 = 33;
[01:39:41] 
[01:39:41] fn main() {
[01:39:41]     let x: &(dyn 'static + Display) = &BYTE;
[01:39:41]     let y: Box<dyn Display + 'static> = Box::new(BYTE);
[01:39:41]     let _: &dyn Display = &BYTE;
[01:39:41]     let _: &dyn::std::fmt::Display = &BYTE;
[01:39:41]     let xstr = format!("{}" , x);
[01:39:41]     let ystr = format!("{}" , y);
[01:39:41]     assert_eq!(xstr , "33");
[01:39:41]     assert_eq!(ystr , "33");
[01:39:41] }
[01:39:41] 
[01:39:41] ------------------------------------------
[01:39:41] 
[01:39:41] 
[01:39:41] 
[01:39:41] error: pretty-printed source does not typecheck
[01:39:41] status: exit code: 101
[01:39:41] command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "-" "-Zno-trans" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-pass/dyn-trait.pretty-out" "--target=x86_64-unknown-linux-gnu" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-pass" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/run-pass/dyn-trait.stage2-x86_64-unknown-linux-gnu.pretty.aux" "-Crpath" "-O" "-Zmiri" "-Zunstable-options" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers"
[01:39:41] stdout:
[01:39:41] ------------------------------------------
[01:39:41] 
[01:39:41] ------------------------------------------
[01:39:41] stderr:
[01:39:41] ------------------------------------------
[01:39:41] error[E0433]: failed to resolve. Use of undeclared type or module `dyn`
[01:39:41]   --> <anon>:21:13
[01:39:41]    |
[01:39:41] 21 |     let _: &dyn::std::fmt::Display = &BYTE;
[01:39:41]    |             ^^^ Use of undeclared type or module `dyn`
[01:39:41] 
[01:39:41] error: aborting due to previous error
[01:39:41] 
[01:39:41] 
[01:39:41] ------------------------------------------
[01:39:41] 
[01:39:41] thread '[pretty] run-pass/dyn-trait.rs' panicked at 'explicit panic', tools/compiletest/src/runtest.rs:2892:9
[01:39:41] note: Run with `RUST_BACKTRACE=1` for a backtrace.
[01:39:41] 
[01:39:41] 
[01:39:41] failures:
[01:39:41]     [pretty] run-pass/dyn-trait.rs

bors added a commit that referenced this pull request Feb 25, 2018
Rollup of 15 pull requests

- Successful merges: #47689, #48110, #48197, #48296, #48386, #48392, #48404, #48415, #48441, #48448, #48452, #48481, #48490, #48499, #48503
- Failed merges:
@Manishearth
Copy link
Member Author

This merged, I just messed up on including it in the rollup

7e68299

@Manishearth Manishearth added this to the Rust Epoch 2018 milestone Mar 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release. 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.

8 participants