Skip to content

Commit

Permalink
codegen: Omit version constraints on imports if scope already guards it
Browse files Browse the repository at this point in the history
This removes unnecessary (duplicate) `#[cfg]` attributes on `use`
imports where the surrounding `mod` (when in a file that fully defines a
`struct`) already declares this constraint.  This does not suffice for
`flags`/`enums` where multiple types live in a single shared file
without surrounding constraint.  When the feature requirement is already
described it is superfluous (and noisy) to specify it again.
  • Loading branch information
MarijnS95 committed Mar 26, 2021
1 parent 28bed3d commit 71e84df
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 8 deletions.
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
2 changes: 1 addition & 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
2 changes: 1 addition & 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
2 changes: 1 addition & 1 deletion src/codegen/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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)?;

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
2 changes: 1 addition & 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 Down
2 changes: 1 addition & 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

0 comments on commit 71e84df

Please sign in to comment.