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
Automatic Generic-Derived Qualifiers #145
Closed
Dessix
wants to merge
8
commits into
dtolnay:master
from
Dessix:thiserror-79-error-constraint-detection
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8d2c7d4
Initial rough-out for enum generic handling
Dessix fedb020
Add visitor which marks provided generics by name in variants
Dessix 64b2a0d
Support `#[from]` usage parsing using GenericUsageVisitor
Dessix f9ca456
Add support for generic structs
Dessix 28751e0
Fully functional. Format literal parsing is present yet atrocious.
Dessix 268352c
Merge remote-tracking branch 'origin/master' into thiserror-79-error-…
Dessix 5de9589
Fix compatibility with rust 1.31.0 and 1.36.0
Dessix 377de48
Apparently `matches` is newer than Rust 1.36.0
Dessix File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
#![deny(clippy::all, clippy::pedantic)] | ||
#![allow( | ||
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422 | ||
clippy::nonstandard_macro_braces, | ||
)] | ||
#![allow(dead_code)] | ||
|
||
use std::fmt::{Debug, Display}; | ||
|
||
use thiserror::Error; | ||
|
||
struct NoFormattingType; | ||
|
||
struct DisplayType; | ||
|
||
impl Display for DisplayType { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, stringify!(DisplayType)) | ||
} | ||
} | ||
|
||
impl Debug for DisplayType { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, stringify!(DisplayType)) | ||
} | ||
} | ||
|
||
struct DebugType; | ||
|
||
impl Debug for DebugType { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, stringify!(DebugType)) | ||
} | ||
} | ||
|
||
/// Direct embedding of a generic in a field | ||
/// | ||
/// Should produce the following instances: | ||
/// | ||
/// ```rust | ||
/// impl<Embedded> Display for DirectEmbedding<Embedded> | ||
/// where | ||
/// Embedded: Debug; | ||
/// | ||
/// impl<Embedded> Error for DirectEmbedding<Embedded> | ||
/// where | ||
/// Self: Debug + Display; | ||
/// ``` | ||
#[derive(Error, Debug)] | ||
enum DirectEmbedding<Embedded> { | ||
#[error("{0:?}")] | ||
FatalError(Embedded), | ||
} | ||
|
||
/// #[from] handling but no Debug usage of the generic | ||
/// | ||
/// Should produce the following instances: | ||
/// | ||
/// ```rust | ||
/// impl<Indirect> Display for FromGenericError<Indirect>; | ||
/// | ||
/// impl<Indirect> Error for FromGenericError<Indirect> | ||
/// where | ||
/// SourceGenericError<Indirect>: Error, | ||
/// Indirect: 'static, | ||
/// Self: Debug + Display; | ||
/// ``` | ||
// TODO: Parse generic parameters contained in #[from] usages and add them to Error impl | ||
// #[derive(Error, Debug)] | ||
// enum FromGenericError<Indirect> { | ||
// #[error("Tadah")] | ||
// SourceEmbedded(#[from] DirectEmbedding<Indirect>), | ||
// } | ||
|
||
/// Direct embedding of a generic in a field | ||
/// | ||
/// Should produce the following instances: | ||
/// | ||
/// ```rust | ||
/// impl<HasDisplay, HasDebug> Display for DirectEmbedding<HasDisplay, HasDebug> | ||
/// where | ||
/// HasDisplay: Display, | ||
/// HasDebug: Debug; | ||
/// | ||
/// impl<HasDisplay, HasDebug> Error for DirectEmbedding<HasDisplay, HasDebug> | ||
/// where | ||
/// Self: Debug + Display; | ||
/// ``` | ||
#[derive(Error)] | ||
enum HybridDisplayType<HasDisplay, HasDebug, HasNeither> { | ||
#[error("{0} : {1:?}")] | ||
HybridDisplayCase(HasDisplay, HasDebug), | ||
#[error("{0}")] | ||
DisplayCase(HasDisplay, HasNeither), | ||
#[error("{1:?}")] | ||
DebugCase(HasNeither, HasDebug), | ||
} | ||
|
||
impl<HasDisplay, HasDebug, HasNeither> Debug | ||
for HybridDisplayType<HasDisplay, HasDebug, HasNeither> | ||
{ | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "HybridDisplayType") | ||
} | ||
} | ||
|
||
fn display_hybrid_display_type( | ||
instance: HybridDisplayType<DisplayType, DebugType, NoFormattingType>, | ||
f: &mut std::fmt::Formatter<'_>, | ||
) -> std::fmt::Result { | ||
Debug::fmt(&instance, f) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not reachable — except maybe in contrived code like
field: [T; { fn f<T>() {}; 0 }]
where you actually don't want the behavior here of considering the innerT
TypeParam as being a use of the outerT
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used this to deep-search for generic usages because- when a generic wasn't used- I was running into circumstances where fixed types would succeed in compilation where they should not, because it generated impossible/unlikely bounds, such as
PathBuf: Display
, which you seem to have resolved using thePathAsDisplay
trait in the past. My solution was to deep-scan for generics in types, and only add bounds for fields which used at least one generic.