Skip to content

Commit

Permalink
Merge pull request #45 from joshua-maros/forego-aliased-in-check-fn
Browse files Browse the repository at this point in the history
Forego aliased in check fn
  • Loading branch information
someguynamedjosh authored Oct 18, 2021
2 parents 7d1efa9 + 885fcf9 commit 20d8de8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/src/fail_tests/borrowchk_custom_drop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ error[E0597]: `bar` does not live long enough
| ^^^^^^^^^^^^^^^^^^-
| | |
| | `bar` dropped here while still borrowed
| | borrow might be used here, when `bar` is dropped and runs the `Drop` code for type `AliasableBox`
| | borrow might be used here, when `bar` is dropped and runs the `Drop` code for type `Bar`
| borrowed value does not live long enough
|
= note: this error originates in the attribute macro `self_referencing` (in Nightly builds, run with -Z macro-backtrace for more info)
41 changes: 21 additions & 20 deletions ouroboros_macro/src/covariance_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,13 @@ pub fn type_is_covariant_over_this_lifetime(ty: &syn::Type) -> Option<bool> {
match ty {
Array(arr) => type_is_covariant_over_this_lifetime(&*arr.elem),
BareFn(f) => {
for arg in f.inputs.iter() {
if uses_this_lifetime(arg.ty.to_token_stream()) {
return None;
}
}
if let syn::ReturnType::Type(_, ty) = &f.output {
if uses_this_lifetime(ty.to_token_stream()) {
return None;
}
}
Some(true)
debug_assert!(uses_this_lifetime(f.to_token_stream()));
None
}
Group(ty) => type_is_covariant_over_this_lifetime(&ty.elem),
ImplTrait(..) => None, // Unusable in struct definition.
Infer(..) => None, // Unusable in struct definition.
Macro(..) => None, // Assume false since we don't know.
Macro(..) => None, // We don't know what the macro will resolve to.
Never(..) => None,
Paren(ty) => type_is_covariant_over_this_lifetime(&ty.elem),
Path(path) => {
Expand Down Expand Up @@ -115,27 +106,37 @@ pub fn type_is_covariant_over_this_lifetime(ty: &syn::Type) -> Option<bool> {
}
Some(true)
}
Ptr(ptr) => type_is_covariant_over_this_lifetime(&ptr.elem),
Ptr(ptr) => {
if ptr.mutability.is_some() {
Some(false)
} else {
type_is_covariant_over_this_lifetime(&ptr.elem)
}
}
// Ignore the actual lifetime of the reference because Rust can automatically convert those.
Reference(rf) => {
if rf.mutability.is_some() {
Some(!uses_this_lifetime(rf.elem.clone().into_token_stream()))
Some(!uses_this_lifetime(rf.elem.to_token_stream()))
} else {
type_is_covariant_over_this_lifetime(&rf.elem)
}
}
Slice(sl) => type_is_covariant_over_this_lifetime(&sl.elem),
TraitObject(..) => None,
Tuple(tup) => {
let mut result = Some(true);
for ty in tup.elems.iter() {
if !type_is_covariant_over_this_lifetime(ty)? {
return Some(false)
match type_is_covariant_over_this_lifetime(ty) {
Some(true) => (),
Some(false) => return Some(false),
None => result = None,
}
}
Some(true)
result
}
// As of writing this, syn parses all the types we could need.
Verbatim(..) => unimplemented!(),
_ => unimplemented!(),
// As of writing this, syn parses all the types we could need. However,
// just to be safe, return that we don't know if it's covariant.
Verbatim(..) => None,
_ => None,
}
}
2 changes: 1 addition & 1 deletion ouroboros_macro/src/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod constructor;
pub mod derives;
pub mod into_heads;
pub mod struc;
pub mod summon_borrowchk;
pub mod summon_checker;
pub mod try_constructor;
pub mod type_asserts;
pub mod with_all;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use syn::Error;

use crate::info_structures::{ArgType, StructInfo};

pub fn generate_borrowchk_summoner(info: &StructInfo) -> Result<TokenStream, Error> {
pub fn generate_checker_summoner(info: &StructInfo) -> Result<TokenStream, Error> {
let mut code: Vec<TokenStream> = Vec::new();
let mut params: Vec<TokenStream> = Vec::new();
let mut value_consumers: Vec<TokenStream> = Vec::new();
Expand Down Expand Up @@ -33,11 +33,10 @@ pub fn generate_borrowchk_summoner(info: &StructInfo) -> Result<TokenStream, Err
}
code.push(quote! { let #field_name = #builder_name (#(#builder_args),*); });
}
if field.is_borrowed() {
let boxed = field.boxed();
code.push(quote! { let mut #field_name = #boxed; });
};
if !field.is_mutably_borrowed() {
if field.is_mutably_borrowed() {
code.push(quote! { let mut #field_name = #field_name; });
} else {
code.push(quote! { let #field_name = #field_name; });
value_consumers.push(quote! { #field_name: &#field_name });
}
}
Expand All @@ -47,7 +46,7 @@ pub fn generate_borrowchk_summoner(info: &StructInfo) -> Result<TokenStream, Err
let generic_params = info.generic_params();
let where_clause = &info.generics.where_clause;
Ok(quote! {
fn check_if_okay_according_to_borrow_checker<#generic_params>(
fn check_if_okay_according_to_checkers<#generic_params>(
#(#params,)*
)
#where_clause
Expand Down
4 changes: 2 additions & 2 deletions ouroboros_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
generate::{
constructor::create_builder_and_constructor, derives::create_derives,
into_heads::make_into_heads, struc::create_actual_struct_def,
summon_borrowchk::generate_borrowchk_summoner,
summon_checker::generate_checker_summoner,
try_constructor::create_try_builder_and_constructor, type_asserts::make_type_asserts,
with_all::make_with_all_function, with_each::make_with_functions,
},
Expand All @@ -37,7 +37,7 @@ fn self_referencing_impl(

let actual_struct_def = create_actual_struct_def(&info)?;

let borrowchk_summoner = generate_borrowchk_summoner(&info)?;
let borrowchk_summoner = generate_checker_summoner(&info)?;

let (builder_struct_name, builder_def, constructor_def) =
create_builder_and_constructor(&info, options, false)?;
Expand Down

0 comments on commit 20d8de8

Please sign in to comment.