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

config/codegen: Stop generating useless Display impls #1502

Merged
merged 4 commits into from
Aug 24, 2023
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
6 changes: 0 additions & 6 deletions book/src/config/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ deprecate_by_min_version = true
# can also take path to the directory for saving "versions.txt" or filename with extension.
# Relative to target_path
single_version_file = true
# Generation of Display trait enabled for all enums, classes, etc.,
# which do not have an override for `generate_display_trait`
# (defaults to "true")
generate_display_trait = true
# Trust the nullability information about return values. If this is disabled
# then any pointer return type is assumed to be nullable unless there is an
# explicit override for it.
Expand Down Expand Up @@ -116,8 +112,6 @@ module_name = "soome_class"
version = "3.12"
# prefixed object in mod.rs with #[cfg(mycond)]
cfg_condition = "mycond"
# if you want to override default option Ex. for write your own Display implementation
generate_display_trait = false
# if you want to generate builder with name SomeClassBuilder
generate_builder = true
# trust return value nullability annotations for this specific type.
Expand Down
4 changes: 0 additions & 4 deletions src/analysis/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ pub fn new(env: &Env, obj: &GObject, imports: &mut Imports) -> Option<Info> {
if has_get_type {
imports.add("glib::prelude::*");
}

if obj.generate_display_trait {
imports.add("std::fmt");
}
}

let mut functions = functions::analyze(
Expand Down
4 changes: 0 additions & 4 deletions src/analysis/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ pub fn new(env: &Env, obj: &GObject, imports: &mut Imports) -> Option<Info> {
if has_get_type {
imports.add("glib::prelude::*");
}

if obj.generate_display_trait {
imports.add("std::fmt");
}
}

let mut functions = functions::analyze(
Expand Down
15 changes: 0 additions & 15 deletions src/analysis/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,24 +849,9 @@ fn analyze_function(
func.c_identifier.as_ref().unwrap_or(&func.name)
);
commented = true;
} else if status.need_generate() && !commented {
if !outs.is_empty() {
out_parameters::analyze_imports(env, &func.parameters, imports);
}
if let Some(AsyncTrampoline {
ref output_params, ..
}) = trampoline
{
out_parameters::analyze_imports(
env,
output_params.iter().map(|out| &out.lib_par),
imports,
);
}
}

if r#async && status.need_generate() && !commented {
imports.add("std::ptr");
imports.add("std::boxed::Box as Box_");
imports.add("std::pin::Pin");

Expand Down
6 changes: 0 additions & 6 deletions src/analysis/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,6 @@ pub fn class(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<Info>
let deprecated_version = klass.deprecated_version;

let mut imports = Imports::with_defined(&env.library, &name);
if obj.generate_display_trait {
imports.add("std::fmt");
}

let is_fundamental = obj.fundamental_type.unwrap_or(klass.is_fundamental);
let supertypes = supertypes::analyze(env, class_tid, version, &mut imports, is_fundamental);
Expand Down Expand Up @@ -377,9 +374,6 @@ pub fn interface(env: &Env, obj: &GObject, deps: &[library::TypeId]) -> Option<I

let mut imports = Imports::with_defined(&env.library, &name);
imports.add("glib::prelude::*");
if obj.generate_display_trait {
imports.add("std::fmt");
}

let supertypes = supertypes::analyze(env, iface_tid, version, &mut imports, false);
let supertypes_properties = supertypes
Expand Down
33 changes: 1 addition & 32 deletions src/analysis/out_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use log::error;
use crate::{
analysis::{
self, conversion_type::ConversionType, function_parameters::CParameter,
functions::is_carray_with_direct_elements, imports::Imports, return_value,
rust_type::RustType,
functions::is_carray_with_direct_elements, return_value, rust_type::RustType,
},
config::{self, parameter_matchable::ParameterMatchable},
env::Env,
Expand Down Expand Up @@ -126,36 +125,6 @@ pub fn analyze(
(info, unsupported_outs)
}

pub fn analyze_imports<'a>(
env: &Env,
parameters: impl IntoIterator<Item = &'a library::Parameter>,
imports: &mut Imports,
) {
for par in parameters {
if par.direction == ParameterDirection::Out {
analyze_type_imports(env, par.typ, par.caller_allocates, imports);
}
}
}

fn analyze_type_imports(env: &Env, typ: TypeId, caller_allocates: bool, imports: &mut Imports) {
match env.library.type_(typ) {
Type::Alias(alias) => analyze_type_imports(env, alias.typ, caller_allocates, imports),
Type::Bitfield(..) | Type::Enumeration(..) => imports.add("std::mem"),
Type::Basic(fund) if !matches!(fund, Basic::Utf8 | Basic::OsString | Basic::Filename) => {
imports.add("std::mem");
}
_ if !caller_allocates => match ConversionType::of(env, typ) {
ConversionType::Direct
| ConversionType::Scalar
| ConversionType::Option
| ConversionType::Result { .. } => (),
_ => imports.add("std::ptr"),
},
_ => (),
}
}

pub fn can_as_return(env: &Env, par: &library::Parameter) -> bool {
use super::conversion_type::ConversionType::*;
match ConversionType::of(env, par.typ) {
Expand Down
1 change: 0 additions & 1 deletion src/analysis/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ fn analyze_property(
imports.add("glib::prelude::*");
}
imports.add("glib::signal::{connect_raw, SignalHandlerId}");
imports.add("std::mem::transmute");
imports.add("std::boxed::Box as Box_");

Some(signals::Info {
Expand Down
1 change: 0 additions & 1 deletion src/analysis/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ fn analyze_signal(
imports.add_used_types(&used_types);
imports.add("glib::prelude::*");
imports.add("glib::signal::{connect_raw, SignalHandlerId}");
imports.add("std::mem::transmute");
imports.add("std::boxed::Box as Box_");
}
let generate_doc = configured_signals.iter().all(|f| f.generate_doc);
Expand Down
3 changes: 0 additions & 3 deletions src/analysis/special_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,8 @@ pub fn analyze_imports(specials: &Infos, imports: &mut Imports) {
imports.add_with_version("glib::translate::*", info.version);
}
Compare => {
imports.add_with_version("std::cmp", info.version);
imports.add_with_version("glib::translate::*", info.version);
}
Display => imports.add_with_version("std::fmt", info.version),
Hash => imports.add_with_version("std::hash", info.version),
Equal => imports.add_with_version("glib::translate::*", info.version),
_ => {}
}
Expand Down
31 changes: 1 addition & 30 deletions src/codegen/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use super::{function, trait_impls};
use crate::{
analysis::{enums::Info, special_functions::Type},
analysis::enums::Info,
codegen::{
general::{
self, allow_deprecated, cfg_condition, cfg_condition_no_doc, cfg_condition_string,
Expand Down Expand Up @@ -226,35 +226,6 @@ fn generate_enum(

writeln!(w)?;

if config.generate_display_trait && !analysis.specials.has_trait(Type::Display) {
// Generate Display trait implementation.
version_condition(w, env, None, enum_.version, false, 0)?;
cfg_condition_no_doc(w, config.cfg_condition.as_ref(), false, 0)?;
allow_deprecated(w, any_deprecated_version, false, 0)?;
writeln!(
w,
"impl fmt::Display for {0} {{\n\
\tfn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{\n\
\t\twrite!(f, \"{0}::{{}}\", match *self {{",
enum_.name
)?;
for member in &members {
version_condition_no_doc(w, env, None, member.version, false, 3)?;
cfg_condition_no_doc(w, member.cfg_condition.as_ref(), false, 3)?;
writeln!(w, "\t\t\tSelf::{0} => \"{0}\",", member.name)?;
}

if !config.exhaustive {
writeln!(
w,
"\t\t\t_ => \"Unknown\",\n\
\t\t}})\n\
\t}}\n\
}}\n"
)?;
}
}

// Only inline from_glib / into_glib implementations if there are not many enums members
let maybe_inline = if members.len() <= 12 || config.exhaustive {
"#[inline]\n"
Expand Down
17 changes: 1 addition & 16 deletions src/codegen/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use super::{function, general::allow_deprecated, trait_impls};
use crate::{
analysis::{flags::Info, special_functions::Type},
analysis::flags::Info,
codegen::{
general::{
self, cfg_condition, cfg_condition_doc, cfg_condition_no_doc, cfg_condition_string,
Expand Down Expand Up @@ -180,21 +180,6 @@ fn generate_flags(

writeln!(w)?;

if config.generate_display_trait && !analysis.specials.has_trait(Type::Display) {
// Generate Display trait implementation.
version_condition(w, env, None, flags.version, false, 0)?;
cfg_condition_no_doc(w, config.cfg_condition.as_ref(), false, 0)?;
allow_deprecated(w, flags.deprecated_version, false, 0)?;
writeln!(
w,
"impl fmt::Display for {0} {{\n\
\tfn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{\n\
\t\t<Self as fmt::Debug>::fmt(self, f)\n\
\t}}\n\
}}\n",
flags.name
)?;
}
generate_default_impl(
w,
env,
Expand Down
23 changes: 2 additions & 21 deletions src/codegen/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,15 @@ use super::{
use crate::{
analysis::{
self, bounds::BoundType, object::has_builder_properties, record_type::RecordType,
ref_mode::RefMode, rust_type::RustType, special_functions::Type,
ref_mode::RefMode, rust_type::RustType,
},
env::Env,
library::{self, Nullable},
nameutil,
traits::IntoString,
};

pub fn generate(
w: &mut dyn Write,
env: &Env,
analysis: &analysis::object::Info,
generate_display_trait: bool,
) -> Result<()> {
pub fn generate(w: &mut dyn Write, env: &Env, analysis: &analysis::object::Info) -> Result<()> {
general::start_comments(w, &env.config)?;
if analysis
.functions
Expand Down Expand Up @@ -330,20 +325,6 @@ pub fn generate(
writeln!(w)?;
generate_trait(w, env, analysis)?;
}

if generate_display_trait && !analysis.specials.has_trait(Type::Display) {
writeln!(w, "\nimpl fmt::Display for {} {{", analysis.name,)?;
// Generate Display trait implementation.
writeln!(
w,
"\tfn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{\n\
\t\tf.write_str(\"{}\")\n\
\t}}\n\
}}",
analysis.name
)?;
}

Ok(())
}

Expand Down
3 changes: 1 addition & 2 deletions src/codegen/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ pub fn generate(
.clone()
.unwrap_or_else(|| module_name(split_namespace_name(&class_analysis.full_name).1));

let generate_display_trait = obj.generate_display_trait;
let mut path = root_path.join(&mod_name);
path.set_extension("rs");
info!("Generating file {:?}", path);

save_to_file(path, env.config.make_backup, |w| {
super::object::generate(w, env, class_analysis, generate_display_trait)
super::object::generate(w, env, class_analysis)
});

super::object::generate_reexports(env, class_analysis, &mod_name, mod_rs, traits, builders);
Expand Down
14 changes: 7 additions & 7 deletions src/codegen/trait_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ fn generate_display(
writeln!(
w,
"\
impl fmt::Display for {type_name} {{
impl std::fmt::Display for {type_name} {{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {{
{body}
}}
}}"
Expand All @@ -168,10 +168,10 @@ fn generate_hash(
writeln!(
w,
"\
impl hash::Hash for {type_name} {{
impl std::hash::Hash for {type_name} {{
#[inline]
fn hash<H>(&self, state: &mut H) where H: hash::Hasher {{
hash::Hash::hash(&{call}, state)
fn hash<H>(&self, state: &mut H) where H: std::hash::Hasher {{
std::hash::Hash::hash(&{call}, state)
}}
}}"
)
Expand Down Expand Up @@ -258,14 +258,14 @@ fn generate_ord(
"\
impl PartialOrd for {type_name} {{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {{
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {{
{call}.partial_cmp(&0)
}}
}}

impl Ord for {type_name} {{
#[inline]
fn cmp(&self, other: &Self) -> cmp::Ordering {{
fn cmp(&self, other: &Self) -> std::cmp::Ordering {{
{call}.cmp(&0)
}}
}}"
Expand Down
9 changes: 0 additions & 9 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ pub struct Config {
pub show_statistics: bool,
pub concurrency: library::Concurrency,
pub single_version_file: Option<PathBuf>,
pub generate_display_trait: bool,
pub trust_return_value_nullability: bool,
pub disable_format: bool,
pub split_build_rs: bool,
Expand Down Expand Up @@ -253,11 +252,6 @@ impl Config {
None => Default::default(),
};

let generate_display_trait = match toml.lookup("options.generate_display_trait") {
Some(v) => v.as_result_bool("options.generate_display_trait")?,
None => true,
};

let trust_return_value_nullability =
match toml.lookup("options.trust_return_value_nullability") {
Some(v) => v.as_result_bool("options.trust_return_value_nullability")?,
Expand All @@ -272,7 +266,6 @@ impl Config {
gobjects::parse_toml(
t,
concurrency,
generate_display_trait,
generate_builder,
trust_return_value_nullability,
)
Expand All @@ -282,7 +275,6 @@ impl Config {
&mut objects,
&toml,
concurrency,
generate_display_trait,
generate_builder,
trust_return_value_nullability,
);
Expand Down Expand Up @@ -359,7 +351,6 @@ impl Config {
show_statistics,
concurrency,
single_version_file,
generate_display_trait,
trust_return_value_nullability,
disable_format,
split_build_rs,
Expand Down
Loading
Loading