-
Notifications
You must be signed in to change notification settings - Fork 183
.chalk file syntax writer #430
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
Changes from all commits
Commits
Show all changes
70 commits
Select commit
Hold shift + click to select a range
8cced0f
Initial implementation of the Program Writer
jackh726 aa91bba
Small cleanup
super-tuple bcfecd2
Add reparse tests for ProgramWriter
super-tuple 096e10c
Fix associated ty parameter names
super-tuple ee8f073
Refactor empty env code + tests
super-tuple 0b2fefe
Implement associated ty value rendering in impl
super-tuple de6924e
Implement RenderAsRust for various ID structs
super-tuple b92e532
Handle Self, trait polarity & trait attributes
daboross 075c76f
Clean up extraneous <> and add basic indentation
daboross 4a0748d
Get recompiling after rebase
super-tuple 9f45a1e
Get recompiling after rebase
super-tuple 40f3c3b
Remove WriteProgram in preparation for moving tests
super-tuple b889edb
Add RustIrDatabase debug wrappers
super-tuple 47d3477
Add AssocTyId-specific name retrieval method
super-tuple fc7c6a7
Implement scalar types rendering
super-tuple 8aea128
Fix missing add_debrujin_index cal
super-tuple 21556f8
Add opaque type definition rendering
super-tuple e6410cb
De-duplicated split_associated_ty_parameters
super-tuple ae486ad
Add tuple type rendering
super-tuple f4a4672
Fix rendered chalk syntax formatting
super-tuple 56f6378
Move program writing tests into tests crate
super-tuple d9ab611
Add reference type rendering
super-tuple 6e9a020
Add str type rendering
super-tuple fcffabd
Add slice type rendering
super-tuple e18d343
Add trait impl for built in tests
super-tuple 2fe84d0
Add impl for generic test
super-tuple c6d3de5
Test and fix formatting
super-tuple 884ebfe
Test and fix assoc where clause formatting
super-tuple cd9df5c
Rename logging_db tests directory to display
super-tuple 10d1009
Implement name disambiguation
super-tuple d5b395c
Fix renaming errors from rebase
super-tuple 62b40fd
Add smart pointer support to LoggingDatabase
super-tuple 0439683
Add test harness for LoggingRustIrDatabase
super-tuple e757cc6
Implement RenderAsRust for Const types
super-tuple 249d49a
Add logging db tests
super-tuple 4f87639
Add reparse test macro
super-tuple 88d244d
Fix unused variable warning
super-tuple 11ca42f
Refactor display logic into modules
super-tuple dec0165
Add linebreaks and fix imports
super-tuple 485d363
Remove unused dbg!()
super-tuple 4aeb6f4
Add comment justifying manual reparse goal
super-tuple 4a349e8
Rename write_program to write_items
super-tuple d0ddb6b
Ignore TyKind data
super-tuple 4083532
Update usage of hidden opaque types
super-tuple c54aa5a
Implement RenderAsRust for LifetimeOutlives
super-tuple 24cb65b
Test and fix const generics
super-tuple 53f2e51
Add display for array types
super-tuple 58001df
Clean up tests
super-tuple 40cfff1
Misc cleanup
super-tuple e458772
Implment FnDef rendering
super-tuple 427b948
Add todo in LoggingDB for closures
super-tuple 63a3955
Add return type rendering for fn types
super-tuple 934445b
Optimize joining iterators with Itertools::format
super-tuple 1858d6a
Add dyn trait lifetime bounds rendering
super-tuple 68210e0
Implement stub items in logging_db
super-tuple e3cce45
Fix missing names from assoc type bounds
super-tuple fa55728
Add adt_repr to Logging DB
super-tuple 35fc303
Fix logging db missing assoc ty value names
super-tuple d9b4e48
Refactor flag macro to error on missing flags
super-tuple 83304fa
Add struct flag rendering
super-tuple 949c75a
Add impl type flag rendering
super-tuple 06d5e4b
Document display sub modules
super-tuple e656484
Add struct repr rendering
super-tuple 750733f
Implement rendering for well known traits
super-tuple 68263e8
Add object_safe trait flag rendering
super-tuple a5c160e
Improve LoggingRustIrDatabase tests
super-tuple 024a8a4
Add opaque type Ty rendering
super-tuple e0f325e
Fix missing opaque ty stubs
super-tuple 20fe0c6
Refactor stub code into RustIrDatabase wrapper
super-tuple 232823b
Implement opaque_ty id collection
super-tuple File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,213 @@ | ||
use std::{ | ||
fmt::{Display, Result}, | ||
sync::Arc, | ||
}; | ||
|
||
use crate::rust_ir::*; | ||
use chalk_ir::{interner::Interner, *}; | ||
use itertools::Itertools; | ||
|
||
use crate::{logging_db::RecordedItemId, split::Split, RustIrDatabase}; | ||
|
||
#[macro_use] | ||
mod utils; | ||
|
||
mod bounds; | ||
mod identifiers; | ||
mod items; | ||
mod render_trait; | ||
mod state; | ||
mod stub; | ||
mod ty; | ||
|
||
pub use self::render_trait::*; | ||
pub use self::state::*; | ||
|
||
use self::utils::as_display; | ||
|
||
pub fn write_item<F, I, T>(f: &mut F, ws: &WriterState<'_, I>, v: &T) -> Result | ||
where | ||
F: std::fmt::Write + ?Sized, | ||
I: Interner, | ||
T: RenderAsRust<I>, | ||
{ | ||
write!(f, "{}\n", v.display(ws)) | ||
} | ||
|
||
/// Writes stubs for items which were referenced by name, but for which we | ||
/// didn't directly access. For instance, traits mentioned in where bounds which | ||
/// are only usually checked during well-formedness, when we weren't recording | ||
/// well-formedness. | ||
/// | ||
/// The "stub" nature of this means it writes output with the right names and | ||
/// the right number of generics, but nothing else. Where clauses, bounds, and | ||
/// fields are skipped. Associated types are ???? skipped. | ||
/// | ||
/// `RecordedItemId::Impl` is not supported. | ||
pub fn write_stub_items<F, I, DB, T>(f: &mut F, db: &DB, ids: T) -> Result | ||
where | ||
F: std::fmt::Write + ?Sized, | ||
I: Interner, | ||
DB: RustIrDatabase<I>, | ||
T: IntoIterator<Item = RecordedItemId<I>>, | ||
{ | ||
write_items(f, &stub::StubWrapper::new(db), ids) | ||
} | ||
|
||
/// Writes out each item recorded by a [`LoggingRustIrDatabase`]. | ||
/// | ||
/// [`LoggingRustIrDatabase`]: crate::logging_db::LoggingRustIrDatabase | ||
pub fn write_items<F, I, DB, T>(f: &mut F, db: &DB, ids: T) -> Result | ||
where | ||
F: std::fmt::Write + ?Sized, | ||
I: Interner, | ||
DB: RustIrDatabase<I>, | ||
T: IntoIterator<Item = RecordedItemId<I>>, | ||
{ | ||
let ws = &WriterState::new(db); | ||
for id in ids { | ||
match id { | ||
RecordedItemId::Impl(id) => { | ||
let v = db.impl_datum(id); | ||
write_item(f, ws, &*v)?; | ||
} | ||
RecordedItemId::Adt(id) => { | ||
let v = db.adt_datum(id); | ||
write_item(f, ws, &*v)?; | ||
} | ||
RecordedItemId::Trait(id) => { | ||
let v = db.trait_datum(id); | ||
write_item(f, ws, &*v)?; | ||
} | ||
RecordedItemId::OpaqueTy(id) => { | ||
let v = db.opaque_ty_data(id); | ||
write_item(f, ws, &*v)?; | ||
} | ||
RecordedItemId::FnDef(id) => { | ||
let v = db.fn_def_datum(id); | ||
write_item(f, ws, &*v)?; | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
/// Displays a set of bounds, all targeting `Self`, as just the trait names, | ||
/// separated by `+`. | ||
/// | ||
/// For example, a list of quantified where clauses which would normally be | ||
/// displayed as: | ||
/// | ||
/// ```notrust | ||
/// Self: A, Self: B, Self: C | ||
/// ``` | ||
/// | ||
/// Is instead displayed by this function as: | ||
/// | ||
/// ```notrust | ||
/// A + B + C | ||
/// ``` | ||
/// | ||
/// Shared between the `Trait` in `dyn Trait` and [`OpaqueTyDatum`] bounds. | ||
fn display_self_where_clauses_as_bounds<'a, I: Interner>( | ||
s: &'a WriterState<'a, I>, | ||
bounds: &'a [QuantifiedWhereClause<I>], | ||
) -> impl Display + 'a { | ||
as_display(move |f| { | ||
let interner = s.db.interner(); | ||
write!( | ||
f, | ||
"{}", | ||
bounds | ||
.iter() | ||
.map(|bound| { | ||
as_display(|f| { | ||
// each individual trait can have a forall | ||
let s = &s.add_debrujin_index(None); | ||
if !bound.binders.is_empty(interner) { | ||
write!( | ||
f, | ||
"forall<{}> ", | ||
s.binder_var_display(&bound.binders) | ||
.collect::<Vec<_>>() | ||
.join(", ") | ||
)?; | ||
} | ||
match &bound.skip_binders() { | ||
WhereClause::Implemented(trait_ref) => display_type_with_generics( | ||
s, | ||
trait_ref.trait_id, | ||
&trait_ref.substitution.parameters(interner)[1..], | ||
) | ||
.fmt(f), | ||
WhereClause::AliasEq(alias_eq) => match &alias_eq.alias { | ||
AliasTy::Projection(projection_ty) => { | ||
let (assoc_ty_datum, trait_params, assoc_type_params) = | ||
s.db.split_projection(&projection_ty); | ||
display_trait_with_assoc_ty_value( | ||
s, | ||
assoc_ty_datum, | ||
&trait_params[1..], | ||
assoc_type_params, | ||
&alias_eq.ty, | ||
) | ||
.fmt(f) | ||
} | ||
AliasTy::Opaque(_opaque) => todo!("opaque type AliasTy"), | ||
}, | ||
WhereClause::LifetimeOutlives(lifetime) => lifetime.display(s).fmt(f), | ||
} | ||
}) | ||
.to_string() | ||
}) | ||
.format(" + ") | ||
) | ||
}) | ||
} | ||
|
||
/// Displays a type with its parameters - something like `AsRef<T>`, | ||
/// OpaqueTyName<U>, or `AdtName<Value>`. | ||
/// | ||
/// This is shared between where bounds, OpaqueTy, & dyn Trait. | ||
fn display_type_with_generics<'a, I: Interner>( | ||
s: &'a WriterState<'a, I>, | ||
trait_name: impl RenderAsRust<I> + 'a, | ||
trait_params: impl IntoIterator<Item = &'a GenericArg<I>> + 'a, | ||
) -> impl Display + 'a { | ||
use std::fmt::Write; | ||
let trait_params = trait_params.into_iter().map(|param| param.display(s)); | ||
let mut trait_params_str = String::new(); | ||
write_joined_non_empty_list!(trait_params_str, "<{}>", trait_params, ", ").unwrap(); | ||
as_display(move |f| write!(f, "{}{}", trait_name.display(s), trait_params_str)) | ||
} | ||
|
||
/// Displays a trait with its parameters and a single associated type - | ||
/// something like `IntoIterator<Item=T>`. | ||
/// | ||
/// This is shared between where bounds & dyn Trait. | ||
fn display_trait_with_assoc_ty_value<'a, I: Interner>( | ||
s: &'a WriterState<'a, I>, | ||
assoc_ty_datum: Arc<AssociatedTyDatum<I>>, | ||
trait_params: &'a [GenericArg<I>], | ||
assoc_ty_params: &'a [GenericArg<I>], | ||
assoc_ty_value: &'a Ty<I>, | ||
) -> impl Display + 'a { | ||
as_display(move |f| { | ||
write!(f, "{}<", assoc_ty_datum.trait_id.display(s))?; | ||
write_joined_non_empty_list!( | ||
f, | ||
"{}, ", | ||
trait_params.iter().map(|param| param.display(s)), | ||
", " | ||
)?; | ||
write!(f, "{}", assoc_ty_datum.id.display(s))?; | ||
write_joined_non_empty_list!( | ||
f, | ||
"<{}>", | ||
assoc_ty_params.iter().map(|param| param.display(s)), | ||
", " | ||
)?; | ||
write!(f, "={}>", assoc_ty_value.display(s))?; | ||
Ok(()) | ||
}) | ||
} |
Oops, something went wrong.
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 probably doesn't have to be
pub
.