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

Omit version constraints if the surrounding scope already guards these #1080

Merged
merged 4 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/codegen/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {

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());
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
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());
Expand Down Expand Up @@ -138,6 +138,7 @@ fn generate_enum(
env,
func_analysis,
Some(&analysis.specials),
enum_.version,
false,
false,
1,
Expand All @@ -153,6 +154,7 @@ fn generate_enum(
&analysis.functions,
&analysis.specials,
None,
None,
)?;

writeln!(w)?;
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
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());
Expand Down Expand Up @@ -106,6 +106,7 @@ fn generate_flags(
env,
func_analysis,
Some(&analysis.specials),
flags.version,
false,
false,
1,
Expand All @@ -121,6 +122,7 @@ fn generate_flags(
&analysis.functions,
&analysis.specials,
None,
None,
)?;

writeln!(w)?;
Expand Down
9 changes: 6 additions & 3 deletions src/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +33,7 @@ pub fn generate(
env: &Env,
analysis: &analysis::functions::Info,
special_functions: Option<&analysis::special_functions::Infos>,
scope_version: Option<Version>,
in_trait: bool,
only_declaration: bool,
indent: usize,
Expand All @@ -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(());
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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!(
Expand Down
4 changes: 2 additions & 2 deletions src/codegen/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ pub fn generate(env: &Env, root_path: &Path, mod_rs: &mut Vec<String>) {
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(())
Expand Down
10 changes: 8 additions & 2 deletions src/codegen/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Version>,
) -> Result<()> {
writeln!(w)?;
for (name, ref scope) in imports.iter() {
if !scope.constraints.is_empty() {
Expand All @@ -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)?;
}

Expand Down
8 changes: 7 additions & 1 deletion src/codegen/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,6 +42,7 @@ pub fn generate(
env,
func_analysis,
Some(&analysis.specials),
analysis.version,
false,
false,
1,
Expand All @@ -55,6 +56,7 @@ pub fn generate(
env,
func_analysis,
Some(&analysis.specials),
analysis.version,
false,
false,
1,
Expand All @@ -76,6 +78,7 @@ pub fn generate(
env,
func_analysis,
Some(&analysis.specials),
analysis.version,
false,
false,
1,
Expand Down Expand Up @@ -108,6 +111,7 @@ pub fn generate(
} else {
None
},
analysis.version,
)?;

if !analysis.builder_properties.is_empty() {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/codegen/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions src/codegen/special_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io::{Result, Write};

use crate::{
analysis::{self, functions::Visibility, special_functions::FunctionType},
version::Version,
Env,
};

Expand All @@ -12,10 +13,13 @@ pub(super) fn generate(
env: &Env,
function: &analysis::functions::Info,
specials: &analysis::special_functions::Infos,
scope_version: Option<Version>,
) -> Result<bool> {
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 {
Expand All @@ -27,9 +31,11 @@ pub(super) fn generate_static_to_str(
w: &mut dyn Write,
env: &Env,
function: &analysis::functions::Info,
scope_version: Option<Version>,
) -> 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 ",
Expand Down
Loading