Skip to content

Issue 2506 #2507

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 3 commits into from
Mar 5, 2018
Merged

Issue 2506 #2507

merged 3 commits into from
Mar 5, 2018

Conversation

kngwyu
Copy link
Contributor

@kngwyu kngwyu commented Mar 5, 2018

For #2506, added dyn check when rewriting TraitObject.
I added 2 tests, but I'm not sure they're enough comprehensive.

Copy link
Contributor

@topecongiro topecongiro left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! I added some inline comments, please take a look.

src/types.rs Outdated
@@ -616,7 +616,15 @@ impl Rewrite for ast::TraitRef {
impl Rewrite for ast::Ty {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
match self.node {
ast::TyKind::TraitObject(ref bounds, ..) => bounds.rewrite(context, shape),
ast::TyKind::TraitObject(ref bounds, tobj_syntax) => {
let res = bounds.rewrite(context, shape)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please add .offset_left(4) when dyn is used to make sure that we do not exceed max width with a long trait? For example, we need something like the following:

let shape = if tobj_syntax == ast::TraitObjectSyntax::Dyn {
    // 4 = "dyn "
    shape.offset_left(4)?
} else {
    shape
};

And it would be nice if there is a test for this, though I cannot come up with a good example.

Copy link
Contributor Author

@kngwyu kngwyu Mar 5, 2018

Choose a reason for hiding this comment

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

I added, but it doesn't seem enough.
When trying to format this,

fn main() {
    fn f3() -> Box<Very__________________________Long__________________Name____________________Trait>
    {

    }
}

rustfmt cause an error, error: line exceeded maximum width (maximum: 100, found: 101).
However, in this case,

fn main() {
    fn f3() -> Box<dyn Very__________________________Long__________________Name____________________Trait>
    {

    }
}

rustfmt outputs

fn main() {
    fn f3(
) -> Box<dyn Very__________________________Long__________________Name____________________Trait>
    {

    }
}

Edited:
However, just

fn f3() -> Box<dyn Very__________________________Long__________________Name____________________Trait>
{
}

cause error: line exceeded maximum width (maximum: 100, found: 101)!

Copy link
Contributor Author

@kngwyu kngwyu Mar 5, 2018

Choose a reason for hiding this comment

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

I checked rustfmt formats

fn main() {
    fn f3() -> Box<Very______________________________Long__________________Name____________________Trait>
    {
    }
}

into

fn main() {
    fn f3(
) -> Box<Very______________________________Long__________________Name____________________Trait>
    {
    }
}

So, this odd formatting is not because of dyn, but the length of trait name.
Though I'm not sure it's a bug, I think it's good to write tests after checking this is intended.


// checks if line wrap works correctly
trait Very_______________________Long__________________Name____________________Trait
{
Copy link
Contributor

Choose a reason for hiding this comment

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

The opening bracket should not be indented, but I guess this is a seperate issue. So this is fine as is.

@kngwyu
Copy link
Contributor Author

kngwyu commented Mar 5, 2018

Thanks for your review.
I added offset_left(4) to shape, but I didn't write a test for it.
See https://github.com/rust-lang-nursery/rustfmt/pull/2507/files/f8f5d5c68cdc5168c32f19d7e09b81fe733c9bac#diff-dfd26227c4842bd3288e9b1f53f0089f
for the reason.

@topecongiro topecongiro merged commit 6191902 into rust-lang:master Mar 5, 2018
@topecongiro
Copy link
Contributor

Thank you for the update and looking deeper to the underlying issue!

Looks like that rustfmt cannot handle trait with a long name. That is a separate issue from #2506, so I am going to merge this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants