Improve &-ptr printing #35611

Merged
merged 1 commit into from Aug 15, 2016

Conversation

Projects
None yet
7 participants
@jonathandturner
Contributor

jonathandturner commented Aug 11, 2016

This PR replaces printing &-ptr with a more readable description. To do so it uses a few heuristics.

If the name of the type is unknown, too long (longer than just saying "reference"), or too complex (a type with explicit lifetime annotations), it will instead opt to print either "reference" or "mutable reference", depending on the mutability of the type.

Before:

error[E0308]: mismatched types
  --> src/test/compile-fail/issue-7061.rs:14:46
   |
14 |     fn foo(&'a mut self) -> Box<BarStruct> { self }
   |                                              ^^^^ expected box, found &-ptr
   |
   = note: expected type `Box<BarStruct>`
   = note:    found type `&'a mut BarStruct`

error: aborting due to previous error

After:

error[E0308]: mismatched types
  --> src/test/compile-fail/issue-7061.rs:14:46
   |
14 |     fn foo(&'a mut self) -> Box<BarStruct> { self }
   |                                              ^^^^ expected box, found mutable reference
   |
   = note: expected type `Box<BarStruct>`
   = note:    found type `&'a mut BarStruct`

error: aborting due to previous error
@rust-highfive

This comment has been minimized.

Show comment
Hide comment
@rust-highfive

rust-highfive Aug 11, 2016

Collaborator

r? @arielb1

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

Collaborator

rust-highfive commented Aug 11, 2016

r? @arielb1

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

@jonathandturner

This comment has been minimized.

Show comment
Hide comment
Contributor

jonathandturner commented Aug 11, 2016

@jonathandturner

This comment has been minimized.

Show comment
Hide comment
@jonathandturner

jonathandturner Aug 11, 2016

Contributor

(adding Niko as reviewer since he said he wanted to be)

Contributor

jonathandturner commented Aug 11, 2016

(adding Niko as reviewer since he said he wanted to be)

@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Aug 12, 2016

Member

omg yessssssssssss

Member

steveklabnik commented Aug 12, 2016

omg yessssssssssss

src/librustc/ty/error.rs
+ match tymut {
+ ty::TypeAndMut{ref ty, ref mutbl} => {
+ if ty.to_string() == "_" || //unknown type name,
+ ty.to_string().len() > 10 || //name longer than saying "reference",

This comment has been minimized.

@eddyb

eddyb Aug 12, 2016

Member

This should really not call to_string twice, better save it.

@eddyb

eddyb Aug 12, 2016

Member

This should really not call to_string twice, better save it.

src/librustc/ty/error.rs
- ty::TyRef(_, _) => "&-ptr".to_string(),
+ ty::TyRef(region, tymut) =>
+ match tymut {
+ ty::TypeAndMut{ref ty, ref mutbl} => {

This comment has been minimized.

@eddyb

eddyb Aug 12, 2016

Member

This whole match is unnecessary, we'd rather try to pretend TypeAndMut doesn't exist - you can put this pattern in the TyRef pattern if you want; the by-ref matching is also unnecessary.

@eddyb

eddyb Aug 12, 2016

Member

This whole match is unnecessary, we'd rather try to pretend TypeAndMut doesn't exist - you can put this pattern in the TyRef pattern if you want; the by-ref matching is also unnecessary.

@eddyb

This comment has been minimized.

Show comment
Hide comment
@eddyb

eddyb Aug 12, 2016

Member

LGTM, my nitpicking notwithstanding 👍.

Member

eddyb commented Aug 12, 2016

LGTM, my nitpicking notwithstanding 👍.

@jonathandturner

This comment has been minimized.

Show comment
Hide comment
@jonathandturner

jonathandturner Aug 12, 2016

Contributor

@eddyb - hehe, no worries. Refactored with some of your nits fixed.

Contributor

jonathandturner commented Aug 12, 2016

@eddyb - hehe, no worries. Refactored with some of your nits fixed.

+ }
+ }
+ } else {
+ format!("&{}", tymut_string)

This comment has been minimized.

@nikomatsakis

nikomatsakis Aug 12, 2016

Contributor

Do we want to print &mut in the case of mutability?

@nikomatsakis

nikomatsakis Aug 12, 2016

Contributor

Do we want to print &mut in the case of mutability?

This comment has been minimized.

@nikomatsakis

nikomatsakis Aug 12, 2016

Contributor

Oh, I see, we do, never mind.

@nikomatsakis

nikomatsakis Aug 12, 2016

Contributor

Oh, I see, we do, never mind.

@nikomatsakis

This comment has been minimized.

Show comment
Hide comment
Contributor

nikomatsakis commented Aug 12, 2016

@bors r+

@bors

This comment has been minimized.

Show comment
Hide comment
@bors

bors Aug 12, 2016

Contributor

📌 Commit 4224737 has been approved by nikomatsakis

Contributor

bors commented Aug 12, 2016

📌 Commit 4224737 has been approved by nikomatsakis

Manishearth added a commit to Manishearth/rust that referenced this pull request Aug 13, 2016

Rollup merge of #35611 - jonathandturner:ptr-helper, r=nikomatsakis
Improve &-ptr printing

This PR replaces printing `&-ptr` with a more readable description.  To do so it uses a few heuristics.

If the name of the type is unknown, too long (longer than just saying "reference"), or too complex (a type with explicit lifetime annotations), it will instead opt to print either "reference" or "mutable reference", depending on the mutability of the type.

Before:

```
error[E0308]: mismatched types
  --> src/test/compile-fail/issue-7061.rs:14:46
   |
14 |     fn foo(&'a mut self) -> Box<BarStruct> { self }
   |                                              ^^^^ expected box, found &-ptr
   |
   = note: expected type `Box<BarStruct>`
   = note:    found type `&'a mut BarStruct`

error: aborting due to previous error
```

After:

```
error[E0308]: mismatched types
  --> src/test/compile-fail/issue-7061.rs:14:46
   |
14 |     fn foo(&'a mut self) -> Box<BarStruct> { self }
   |                                              ^^^^ expected box, found mutable reference
   |
   = note: expected type `Box<BarStruct>`
   = note:    found type `&'a mut BarStruct`

error: aborting due to previous error
```
@eddyb

This comment has been minimized.

Show comment
Hide comment
@eddyb

eddyb Aug 14, 2016

Member

@bors rollup

Member

eddyb commented Aug 14, 2016

@bors rollup

eddyb added a commit to eddyb/rust that referenced this pull request Aug 14, 2016

Rollup merge of #35611 - jonathandturner:ptr-helper, r=nikomatsakis
Improve &-ptr printing

This PR replaces printing `&-ptr` with a more readable description.  To do so it uses a few heuristics.

If the name of the type is unknown, too long (longer than just saying "reference"), or too complex (a type with explicit lifetime annotations), it will instead opt to print either "reference" or "mutable reference", depending on the mutability of the type.

Before:

```
error[E0308]: mismatched types
  --> src/test/compile-fail/issue-7061.rs:14:46
   |
14 |     fn foo(&'a mut self) -> Box<BarStruct> { self }
   |                                              ^^^^ expected box, found &-ptr
   |
   = note: expected type `Box<BarStruct>`
   = note:    found type `&'a mut BarStruct`

error: aborting due to previous error
```

After:

```
error[E0308]: mismatched types
  --> src/test/compile-fail/issue-7061.rs:14:46
   |
14 |     fn foo(&'a mut self) -> Box<BarStruct> { self }
   |                                              ^^^^ expected box, found mutable reference
   |
   = note: expected type `Box<BarStruct>`
   = note:    found type `&'a mut BarStruct`

error: aborting due to previous error
```

@eddyb eddyb referenced this pull request Aug 14, 2016

Closed

Rollup of 33 pull requests #35665

eddyb added a commit to eddyb/rust that referenced this pull request Aug 14, 2016

Rollup merge of #35611 - jonathandturner:ptr-helper, r=nikomatsakis
Improve &-ptr printing

This PR replaces printing `&-ptr` with a more readable description.  To do so it uses a few heuristics.

If the name of the type is unknown, too long (longer than just saying "reference"), or too complex (a type with explicit lifetime annotations), it will instead opt to print either "reference" or "mutable reference", depending on the mutability of the type.

Before:

```
error[E0308]: mismatched types
  --> src/test/compile-fail/issue-7061.rs:14:46
   |
14 |     fn foo(&'a mut self) -> Box<BarStruct> { self }
   |                                              ^^^^ expected box, found &-ptr
   |
   = note: expected type `Box<BarStruct>`
   = note:    found type `&'a mut BarStruct`

error: aborting due to previous error
```

After:

```
error[E0308]: mismatched types
  --> src/test/compile-fail/issue-7061.rs:14:46
   |
14 |     fn foo(&'a mut self) -> Box<BarStruct> { self }
   |                                              ^^^^ expected box, found mutable reference
   |
   = note: expected type `Box<BarStruct>`
   = note:    found type `&'a mut BarStruct`

error: aborting due to previous error
```

@eddyb eddyb referenced this pull request Aug 14, 2016

Merged

Rollup of 30 pull requests #35666

bors added a commit that referenced this pull request Aug 14, 2016

@bors bors merged commit 4224737 into rust-lang:master Aug 15, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details

@jonathandturner jonathandturner deleted the jonathandturner:ptr-helper branch Aug 18, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment