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

Add T to PhantomData impl Debug #99099

Merged
merged 1 commit into from
Oct 4, 2022
Merged

Conversation

Stargateur
Copy link
Contributor

@Stargateur Stargateur commented Jul 9, 2022

This add debug information for PhantomData, I believe it's make sense to add this to debug impl of PhantomData since T is what define what is the PhantomData just write "PhantomData" is not very useful for debugging.

Alternative:

  • PhantomData::<{}>
  • PhantomData { t: "str_type" }

@rustbot label +T-libs-api -T-libs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Jul 9, 2022
@rust-highfive
Copy link
Collaborator

r? @Mark-Simulacrum

(rust-highfive has picked a reviewer for you, use r? to override)

@rustbot
Copy link
Collaborator

rustbot commented Jul 9, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 9, 2022
@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 9, 2022
@bjorn3
Copy link
Member

bjorn3 commented Jul 10, 2022

A counter argument to this change is that we don't include T for None either.

@Stargateur
Copy link
Contributor Author

Stargateur commented Jul 10, 2022

A counter argument to this change is that we don't include T for None either.

We never include generic type in any implementation of Debug however PhantomData is very special, I don't think we can compare it to Option, when you have None you have no value so you shouldn't care of T, PhantomData represent a "linked" type, it's always here, but PhantomData doesn't have a field so we don't show it.

A use case of PhantomData I often use is to implement different behavior based on T. For example:

#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct First;

#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Last;

#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct OneContext<Behavior, Context> {
  behavior: PhantomData<Behavior>,
  context: Context,
}

impl<Context> BitOr for OneContext<First, Context> {
  type Output = Self;

  fn bitor(self, _rhs: Self) -> Self::Output {
    self
  }
}

impl<Context> BitOr for OneContext<Last, Context> {
  type Output = Self;

  fn bitor(self, rhs: Self) -> Self::Output {
    rhs
  }
}

Here First and Last represent a behavior I want to implement, either keep the initial value or the lastest value given to bitor. While you print debug of OneContext you only have PhantomData, it doesn't allow to know what is the behavior whereas PhantomData<First> would allow it.

This kind of use of PhantomData is common and I think show the type of T is useful for it.

@Mark-Simulacrum
Copy link
Member

Hm, I have somewhat mixed feelings on a change like this. I think it's true that this information can be useful, but it's also somewhat of an unusual thing for us to do -- most generic types don't include generic information in their print out.

I think overall I'm net-positive on this change, and it seems like something that we could revert in the future, but I'd like to get another set of eyes on it. Let's r? @joshtriplett

@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 13, 2022
@joshtriplett
Copy link
Member

This seems useful to me, to the extent that it's useful for PhantomData to show up in Debug at all.

On the other hand, I suspect that PhantomData is also a type that many structures would want to skip in Debug given an attribute allowing the #[derive(Debug)] to do so.

@joshtriplett
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Oct 3, 2022

📌 Commit eac1e30 has been approved by joshtriplett

It is now in the queue for this repository.

@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 Oct 3, 2022
@bors
Copy link
Contributor

bors commented Oct 4, 2022

⌛ Testing commit eac1e30 with merge 1f1defc...

@bors
Copy link
Contributor

bors commented Oct 4, 2022

☀️ Test successful - checks-actions
Approved by: joshtriplett
Pushing 1f1defc to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 4, 2022
@bors bors merged commit 1f1defc into rust-lang:master Oct 4, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 4, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1f1defc): comparison URL.

Overall result: ❌ regressions - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
0.7% [0.7%, 0.7%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.7% [0.7%, 0.7%] 1

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.4% [2.2%, 6.3%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.0% [-2.8%, -1.3%] 4
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Footnotes

  1. the arithmetic mean of the percent change 2

  2. number of relevant changes 2

Skeletonxf referenced this pull request in Skeletonxf/easy-ml Jan 4, 2023
…sion of Rust due to PhantomData debug now including type info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants