diff --git a/Cargo.lock b/Cargo.lock index 2f620ddf7..b0ba5c3b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,9 +85,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.6.1" +version = "1.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b" +checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" dependencies = [ "autocfg", "hashbrown", @@ -119,27 +119,26 @@ checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" [[package]] name = "once_cell" -version = "1.5.2" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13bd41f508810a131401606d54ac32a467c97172d74ba7662562ebba5ad07fa0" +checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" [[package]] name = "regex" -version = "1.4.3" +version = "1.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" +checksum = "957056ecddbeba1b26965114e191d2e8589ce74db242b6ea25fc4062427a5c19" dependencies = [ "aho-corasick", "memchr", "regex-syntax", - "thread_local", ] [[package]] name = "regex-syntax" -version = "0.6.22" +version = "0.6.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581" +checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548" [[package]] name = "rustdoc-stripper" @@ -148,18 +147,9 @@ source = "git+https://github.com/GuillaumeGomez/rustdoc-stripper#9c23f667610ccd5 [[package]] name = "serde" -version = "1.0.123" +version = "1.0.125" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" - -[[package]] -name = "thread_local" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd" -dependencies = [ - "once_cell", -] +checksum = "558dc50e1a5a5fa7112ca2ce4effcb321b0300c0d4ccf0776a9f60cd89031171" [[package]] name = "toml" diff --git a/src/codegen/constants.rs b/src/codegen/constants.rs index 133e51325..b38d12f19 100644 --- a/src/codegen/constants.rs +++ b/src/codegen/constants.rs @@ -21,7 +21,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec) { file_saver::save_to_file(path, env.config.make_backup, |w| { general::start_comments(w, &env.config)?; - general::uses(w, env, &imports)?; + general::uses(w, env, &imports, None)?; writeln!(w)?; mod_rs.push("\nmod constants;".into()); diff --git a/src/codegen/enums.rs b/src/codegen/enums.rs index 7df10f950..b92429037 100644 --- a/src/codegen/enums.rs +++ b/src/codegen/enums.rs @@ -28,7 +28,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec) { let path = root_path.join("enums.rs"); file_saver::save_to_file(path, env.config.make_backup, |w| { general::start_comments(w, &env.config)?; - general::uses(w, env, &env.analysis.enum_imports)?; + general::uses(w, env, &env.analysis.enum_imports, None)?; writeln!(w)?; mod_rs.push("\nmod enums;".into()); @@ -138,6 +138,7 @@ fn generate_enum( env, func_analysis, Some(&analysis.specials), + enum_.version, false, false, 1, @@ -153,6 +154,7 @@ fn generate_enum( &analysis.functions, &analysis.specials, None, + None, )?; writeln!(w)?; diff --git a/src/codegen/flags.rs b/src/codegen/flags.rs index 19670a223..6b7b73ef4 100644 --- a/src/codegen/flags.rs +++ b/src/codegen/flags.rs @@ -25,7 +25,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec) { let path = root_path.join("flags.rs"); file_saver::save_to_file(path, env.config.make_backup, |w| { general::start_comments(w, &env.config)?; - general::uses(w, env, &env.analysis.flags_imports)?; + general::uses(w, env, &env.analysis.flags_imports, None)?; writeln!(w)?; mod_rs.push("\nmod flags;".into()); @@ -106,6 +106,7 @@ fn generate_flags( env, func_analysis, Some(&analysis.specials), + flags.version, false, false, 1, @@ -121,6 +122,7 @@ fn generate_flags( &analysis.functions, &analysis.specials, None, + None, )?; writeln!(w)?; diff --git a/src/codegen/function.rs b/src/codegen/function.rs index 829569f93..6562d392d 100644 --- a/src/codegen/function.rs +++ b/src/codegen/function.rs @@ -18,6 +18,7 @@ use crate::{ chunk::{ffi_function_todo, Chunk}, env::Env, library, + version::Version, writer::{primitives::tabs, safety_assertion_mode_to_str, ToCode}, }; use log::warn; @@ -32,6 +33,7 @@ pub fn generate( env: &Env, analysis: &analysis::functions::Info, special_functions: Option<&analysis::special_functions::Infos>, + scope_version: Option, in_trait: bool, only_declaration: bool, indent: usize, @@ -45,7 +47,7 @@ pub fn generate( } if let Some(special_functions) = special_functions { - if special_functions::generate(w, env, analysis, special_functions)? { + if special_functions::generate(w, env, analysis, special_functions, scope_version)? { return Ok(()); } } @@ -81,7 +83,8 @@ pub fn generate( cfg_deprecated(w, env, analysis.deprecated_version, commented, indent)?; } cfg_condition(w, &analysis.cfg_condition, commented, indent)?; - version_condition(w, env, analysis.version, commented, indent)?; + let version = Version::if_stricter_than(analysis.version, scope_version); + version_condition(w, env, version, commented, indent)?; not_version_condition(w, analysis.not_version, commented, indent)?; doc_hidden(w, analysis.doc_hidden, comment_prefix, indent)?; if !in_trait || only_declaration { @@ -116,7 +119,7 @@ pub fn generate( writeln!(w, "{}{}", tabs(indent), comment_prefix)?; cfg_condition(w, &analysis.cfg_condition, commented, indent)?; - version_condition(w, env, analysis.version, commented, indent)?; + version_condition(w, env, version, commented, indent)?; not_version_condition(w, analysis.not_version, commented, indent)?; doc_hidden(w, analysis.doc_hidden, comment_prefix, indent)?; writeln!( diff --git a/src/codegen/functions.rs b/src/codegen/functions.rs index 14cd75a15..fec4668bb 100644 --- a/src/codegen/functions.rs +++ b/src/codegen/functions.rs @@ -17,14 +17,14 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec) { let path = root_path.join("functions.rs"); file_saver::save_to_file(path, env.config.make_backup, |w| { general::start_comments(w, &env.config)?; - general::uses(w, env, &functions.imports)?; + general::uses(w, env, &functions.imports, None)?; writeln!(w)?; mod_rs.push("\npub mod functions;".into()); for func_analysis in &functions.functions { - function::generate(w, env, func_analysis, None, false, false, 0)?; + function::generate(w, env, func_analysis, None, None, false, false, 0)?; } Ok(()) diff --git a/src/codegen/general.rs b/src/codegen/general.rs index dc58c4929..ac623b164 100644 --- a/src/codegen/general.rs +++ b/src/codegen/general.rs @@ -65,7 +65,12 @@ pub fn single_version_file(w: &mut dyn Write, conf: &Config, prefix: &str) -> Re ) } -pub fn uses(w: &mut dyn Write, env: &Env, imports: &Imports) -> Result<()> { +pub fn uses( + w: &mut dyn Write, + env: &Env, + imports: &Imports, + outer_version: Option, +) -> Result<()> { writeln!(w)?; for (name, ref scope) in imports.iter() { if !scope.constraints.is_empty() { @@ -80,8 +85,9 @@ pub fn uses(w: &mut dyn Write, env: &Env, imports: &Imports) -> Result<()> { scope.constraints.join(", ") )?; } + let version = Version::if_stricter_than(scope.version, outer_version); - version_condition(w, env, scope.version, false, 0)?; + version_condition(w, env, version, false, 0)?; writeln!(w, "use {};", name)?; } diff --git a/src/codegen/object.rs b/src/codegen/object.rs index 0369623fd..b3fb3353d 100644 --- a/src/codegen/object.rs +++ b/src/codegen/object.rs @@ -20,7 +20,7 @@ pub fn generate( generate_display_trait: bool, ) -> Result<()> { general::start_comments(w, &env.config)?; - general::uses(w, env, &analysis.imports)?; + general::uses(w, env, &analysis.imports, analysis.version)?; general::define_object_type( w, @@ -42,6 +42,7 @@ pub fn generate( env, func_analysis, Some(&analysis.specials), + analysis.version, false, false, 1, @@ -55,6 +56,7 @@ pub fn generate( env, func_analysis, Some(&analysis.specials), + analysis.version, false, false, 1, @@ -76,6 +78,7 @@ pub fn generate( env, func_analysis, Some(&analysis.specials), + analysis.version, false, false, 1, @@ -108,6 +111,7 @@ pub fn generate( } else { None }, + analysis.version, )?; if !analysis.builder_properties.is_empty() { @@ -302,6 +306,7 @@ fn generate_trait(w: &mut dyn Write, env: &Env, analysis: &analysis::object::Inf env, func_analysis, Some(&analysis.specials), + analysis.version, true, true, 1, @@ -335,6 +340,7 @@ fn generate_trait(w: &mut dyn Write, env: &Env, analysis: &analysis::object::Inf env, func_analysis, Some(&analysis.specials), + analysis.version, true, false, 1, diff --git a/src/codegen/record.rs b/src/codegen/record.rs index 279a5a326..d00a5afdd 100644 --- a/src/codegen/record.rs +++ b/src/codegen/record.rs @@ -10,7 +10,7 @@ pub fn generate(w: &mut dyn Write, env: &Env, analysis: &analysis::record::Info) let type_ = analysis.type_(&env.library); general::start_comments(w, &env.config)?; - general::uses(w, env, &analysis.imports)?; + general::uses(w, env, &analysis.imports, type_.version)?; if analysis.is_boxed { if let Some((ref glib_get_type, _)) = analysis.glib_get_type { @@ -94,6 +94,7 @@ pub fn generate(w: &mut dyn Write, env: &Env, analysis: &analysis::record::Info) env, func_analysis, Some(&analysis.specials), + analysis.version, false, false, 1, @@ -112,6 +113,7 @@ pub fn generate(w: &mut dyn Write, env: &Env, analysis: &analysis::record::Info) &analysis.functions, &analysis.specials, None, + analysis.version, )?; if analysis.concurrency != library::Concurrency::None { diff --git a/src/codegen/special_functions.rs b/src/codegen/special_functions.rs index 8aa0e2cc8..d6a27315c 100644 --- a/src/codegen/special_functions.rs +++ b/src/codegen/special_functions.rs @@ -2,6 +2,7 @@ use std::io::{Result, Write}; use crate::{ analysis::{self, functions::Visibility, special_functions::FunctionType}, + version::Version, Env, }; @@ -12,10 +13,13 @@ pub(super) fn generate( env: &Env, function: &analysis::functions::Info, specials: &analysis::special_functions::Infos, + scope_version: Option, ) -> Result { if let Some(special) = specials.functions().get(&function.glib_name) { match special.type_ { - FunctionType::StaticStringify => generate_static_to_str(w, env, function), + FunctionType::StaticStringify => { + generate_static_to_str(w, env, function, scope_version) + } } .map(|()| true) } else { @@ -27,9 +31,11 @@ pub(super) fn generate_static_to_str( w: &mut dyn Write, env: &Env, function: &analysis::functions::Info, + scope_version: Option, ) -> Result<()> { writeln!(w)?; - version_condition(w, env, function.version, false, 1)?; + let version = Version::if_stricter_than(function.version, scope_version); + version_condition(w, env, version, false, 1)?; let visibility = match function.visibility { Visibility::Public => "pub ", diff --git a/src/codegen/trait_impls.rs b/src/codegen/trait_impls.rs index 122e15640..c338a43cb 100644 --- a/src/codegen/trait_impls.rs +++ b/src/codegen/trait_impls.rs @@ -1,9 +1,10 @@ -use super::general::version_condition; use crate::{ analysis::{ functions::Info, special_functions::{Infos, Type}, }, + codegen::general::version_condition, + version::Version, Env, }; use std::io::{Result, Write}; @@ -15,21 +16,24 @@ pub fn generate( functions: &[Info], specials: &Infos, trait_name: Option<&str>, + scope_version: Option, ) -> Result<()> { for (type_, special_info) in specials.traits().iter() { if let Some(info) = lookup(functions, &special_info.glib_name) { match *type_ { Type::Compare => { if !specials.has_trait(Type::Equal) { - generate_eq_compare(w, env, type_name, info, trait_name)?; + generate_eq_compare(w, env, type_name, info, trait_name, scope_version)?; } - generate_ord(w, env, type_name, info, trait_name)?; + generate_ord(w, env, type_name, info, trait_name, scope_version)?; } Type::Equal => { - generate_eq(w, env, type_name, info, trait_name)?; + generate_eq(w, env, type_name, info, trait_name, scope_version)?; } - Type::Display => generate_display(w, env, type_name, info, trait_name)?, - Type::Hash => generate_hash(w, env, type_name, info, trait_name)?, + Type::Display => { + generate_display(w, env, type_name, info, trait_name, scope_version)? + } + Type::Hash => generate_hash(w, env, type_name, info, trait_name, scope_version)?, _ => {} } } @@ -80,9 +84,11 @@ fn generate_display( type_name: &str, func: &Info, trait_name: Option<&str>, + scope_version: Option, ) -> Result<()> { writeln!(w)?; - version_condition(w, env, func.version, false, 0)?; + let version = Version::if_stricter_than(func.version, scope_version); + version_condition(w, env, version, false, 0)?; use crate::analysis::out_parameters::Mode; @@ -121,9 +127,11 @@ fn generate_hash( type_name: &str, func: &Info, trait_name: Option<&str>, + scope_version: Option, ) -> Result<()> { writeln!(w)?; - version_condition(w, env, func.version, false, 0)?; + let version = Version::if_stricter_than(func.version, scope_version); + version_condition(w, env, version, false, 0)?; let call = generate_call(&func.name, &[], trait_name); @@ -147,9 +155,11 @@ fn generate_eq( type_name: &str, func: &Info, trait_name: Option<&str>, + scope_version: Option, ) -> Result<()> { writeln!(w)?; - version_condition(w, env, func.version, false, 0)?; + let version = Version::if_stricter_than(func.version, scope_version); + version_condition(w, env, version, false, 0)?; let call = generate_call(&func.name, &["other"], trait_name); @@ -175,9 +185,11 @@ fn generate_eq_compare( type_name: &str, func: &Info, trait_name: Option<&str>, + scope_version: Option, ) -> Result<()> { writeln!(w)?; - version_condition(w, env, func.version, false, 0)?; + let version = Version::if_stricter_than(func.version, scope_version); + version_condition(w, env, version, false, 0)?; let call = generate_call(&func.name, &["other"], trait_name); @@ -203,9 +215,11 @@ fn generate_ord( type_name: &str, func: &Info, trait_name: Option<&str>, + scope_version: Option, ) -> Result<()> { writeln!(w)?; - version_condition(w, env, func.version, false, 0)?; + let version = Version::if_stricter_than(func.version, scope_version); + version_condition(w, env, version, false, 0)?; let call = generate_call(&func.name, &["other"], trait_name); diff --git a/src/version.rs b/src/version.rs index 03a8a8fef..9276445f2 100644 --- a/src/version.rs +++ b/src/version.rs @@ -23,6 +23,17 @@ impl Version { Short(major) => format!("v{}", major), } } + + /// Returns `inner_version` if it is stricter than `outer_version`, `None` otherwise + pub fn if_stricter_than( + inner_version: Option, + outer_version: Option, + ) -> Option { + match (inner_version, outer_version) { + (Some(inner_version), Some(outer_version)) if inner_version <= outer_version => None, + (inner_version, _) => inner_version, + } + } } impl FromStr for Version {