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

Bring back assert!ions lost in ecdc4055234f341eafb22e76e49a638d0f8c5f9c #767

Merged
merged 1 commit into from Nov 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 22 additions & 18 deletions generator/src/generator/namespace/switch.rs
Expand Up @@ -261,6 +261,9 @@ pub(super) fn emit_switch_type(
outln!(out, "}}");
}

let generate_switch_expr_fn =
generate_serialize && switch.cases.iter().all(|case| case.exprs.len() == 1);

if generate_serialize {
if let Some(size) = switch.size() {
emit_fixed_size_switch_serialize(
Expand All @@ -279,14 +282,12 @@ pub(super) fn emit_switch_type(
name,
&case_infos,
switch_expr_type,
generate_switch_expr_fn,
out,
);
}
}

let generate_switch_expr_fn =
generate_serialize && switch.cases.iter().all(|case| case.exprs.len() == 1);

if generate_switch_expr_fn {
outln!(out, "impl {} {{", name);
out.indented(|out| {
Expand Down Expand Up @@ -769,6 +770,7 @@ fn emit_variable_size_switch_serialize(
name: &str,
case_infos: &[CaseInfo],
switch_expr_type: &str,
has_switch_expr_fn: bool,
out: &mut Output,
) {
let external_params = switch.external_params.borrow();
Expand Down Expand Up @@ -808,21 +810,23 @@ fn emit_variable_size_switch_serialize(
ext_params_arg_defs
);
out.indented(|out| {
// TODO: emit an assertion checking that the switch case has
// been set properly
//
// omitted here because some expressions do not have a
// `switch_expr()` function
let _ = switch_expr_type;

// eat the parameters for now
if !external_params.is_empty() {
for external_param in external_params.iter() {
outln!(
out,
"let _ = {};",
to_rust_variable_name(&external_param.name)
);
if has_switch_expr_fn {
serialize::emit_assert_for_switch_serialize(
generator,
switch,
switch_expr_type,
out,
);
} else {
// eat unused parameters
if !external_params.is_empty() {
for external_param in external_params.iter() {
outln!(
out,
"let _ = {};",
to_rust_variable_name(&external_param.name)
);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions x11rb-protocol/src/protocol/render.rs
Expand Up @@ -1820,7 +1820,7 @@ impl CreatePictureAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(repeat) = self.repeat {
u32::from(repeat).serialize_into(bytes);
}
Expand Down Expand Up @@ -2226,7 +2226,7 @@ impl ChangePictureAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(repeat) = self.repeat {
u32::from(repeat).serialize_into(bytes);
}
Expand Down
2 changes: 1 addition & 1 deletion x11rb-protocol/src/protocol/screensaver.rs
Expand Up @@ -727,7 +727,7 @@ impl SetAttributesAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(background_pixmap) = self.background_pixmap {
background_pixmap.serialize_into(bytes);
}
Expand Down
4 changes: 2 additions & 2 deletions x11rb-protocol/src/protocol/sync.rs
Expand Up @@ -1228,7 +1228,7 @@ impl CreateAlarmAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(counter) = self.counter {
counter.serialize_into(bytes);
}
Expand Down Expand Up @@ -1467,7 +1467,7 @@ impl ChangeAlarmAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(counter) = self.counter {
counter.serialize_into(bytes);
}
Expand Down
28 changes: 12 additions & 16 deletions x11rb-protocol/src/protocol/xinput.rs
Expand Up @@ -860,7 +860,7 @@ impl InputInfoInfo {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, class_id: u8) {
let _ = class_id;
assert_eq!(self.switch_expr(), u8::from(class_id), "switch `info` has an inconsistent discriminant");
match self {
InputInfoInfo::Key(key) => key.serialize_into(bytes),
InputInfoInfo::Button(button) => button.serialize_into(bytes),
Expand Down Expand Up @@ -4336,7 +4336,7 @@ impl FeedbackStateData {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, class_id: u8) {
let _ = class_id;
assert_eq!(self.switch_expr(), u8::from(class_id), "switch `data` has an inconsistent discriminant");
match self {
FeedbackStateData::Keyboard(keyboard) => keyboard.serialize_into(bytes),
FeedbackStateData::Pointer(pointer) => pointer.serialize_into(bytes),
Expand Down Expand Up @@ -5248,7 +5248,7 @@ impl FeedbackCtlData {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, class_id: u8) {
let _ = class_id;
assert_eq!(self.switch_expr(), u8::from(class_id), "switch `data` has an inconsistent discriminant");
match self {
FeedbackCtlData::Keyboard(keyboard) => keyboard.serialize_into(bytes),
FeedbackCtlData::Pointer(pointer) => pointer.serialize_into(bytes),
Expand Down Expand Up @@ -6732,7 +6732,7 @@ impl InputStateData {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, class_id: u8) {
let _ = class_id;
assert_eq!(self.switch_expr(), u8::from(class_id), "switch `data` has an inconsistent discriminant");
match self {
InputStateData::Key(key) => key.serialize_into(bytes),
InputStateData::Button(button) => button.serialize_into(bytes),
Expand Down Expand Up @@ -7852,7 +7852,7 @@ impl DeviceStateData {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, control_id: u16) {
let _ = control_id;
assert_eq!(self.switch_expr(), u16::from(control_id), "switch `data` has an inconsistent discriminant");
match self {
DeviceStateData::Resolution(resolution) => resolution.serialize_into(bytes),
DeviceStateData::AbsCalib(abs_calib) => abs_calib.serialize_into(bytes),
Expand Down Expand Up @@ -8673,7 +8673,7 @@ impl DeviceCtlData {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, control_id: u16) {
let _ = control_id;
assert_eq!(self.switch_expr(), u16::from(control_id), "switch `data` has an inconsistent discriminant");
match self {
DeviceCtlData::Resolution(resolution) => resolution.serialize_into(bytes),
DeviceCtlData::AbsCalib(abs_calib) => abs_calib.serialize_into(bytes),
Expand Down Expand Up @@ -9148,8 +9148,7 @@ impl ChangeDevicePropertyAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, format: u8, num_items: u32) {
let _ = format;
let _ = num_items;
assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant");
match self {
ChangeDevicePropertyAux::Data8(data8) => {
assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length");
Expand Down Expand Up @@ -9524,8 +9523,7 @@ impl GetDevicePropertyItems {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, format: u8, num_items: u32) {
let _ = format;
let _ = num_items;
assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant");
match self {
GetDevicePropertyItems::Data8(data8) => {
assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length");
Expand Down Expand Up @@ -10699,7 +10697,7 @@ impl HierarchyChangeData {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, type_: u16) {
let _ = type_;
assert_eq!(self.switch_expr(), u16::from(type_), "switch `data` has an inconsistent discriminant");
match self {
HierarchyChangeData::AddMaster(add_master) => add_master.serialize_into(bytes),
HierarchyChangeData::RemoveMaster(remove_master) => remove_master.serialize_into(bytes),
Expand Down Expand Up @@ -12469,7 +12467,7 @@ impl DeviceClassData {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, type_: u16) {
let _ = type_;
assert_eq!(self.switch_expr(), u16::from(type_), "switch `data` has an inconsistent discriminant");
match self {
DeviceClassData::Key(key) => key.serialize_into(bytes),
DeviceClassData::Button(button) => button.serialize_into(bytes),
Expand Down Expand Up @@ -14162,8 +14160,7 @@ impl XIChangePropertyAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, format: u8, num_items: u32) {
let _ = format;
let _ = num_items;
assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant");
match self {
XIChangePropertyAux::Data8(data8) => {
assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length");
Expand Down Expand Up @@ -14537,8 +14534,7 @@ impl XIGetPropertyItems {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, format: u8, num_items: u32) {
let _ = format;
let _ = num_items;
assert_eq!(self.switch_expr(), u8::from(format), "switch `items` has an inconsistent discriminant");
match self {
XIGetPropertyItems::Data8(data8) => {
assert_eq!(data8.len(), usize::try_from(num_items).unwrap(), "`data8` has an incorrect length");
Expand Down
64 changes: 7 additions & 57 deletions x11rb-protocol/src/protocol/xkb.rs
Expand Up @@ -6442,9 +6442,7 @@ impl SelectEventsAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, affect_which: u16, clear: u16, select_all: u16) {
let _ = affect_which;
let _ = clear;
let _ = select_all;
assert_eq!(self.switch_expr(), u16::from(affect_which) & ((!u16::from(clear)) & (!u16::from(select_all))), "switch `details` has an inconsistent discriminant");
if let Some(ref bitcase1) = self.bitcase1 {
bitcase1.serialize_into(bytes);
}
Expand Down Expand Up @@ -7991,16 +7989,7 @@ impl GetMapMap {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, present: u16, n_types: u8, n_key_syms: u8, n_key_actions: u8, total_actions: u16, total_key_behaviors: u8, virtual_mods: u16, total_key_explicit: u8, total_mod_map_keys: u8, total_v_mod_map_keys: u8) {
let _ = present;
let _ = n_types;
let _ = n_key_syms;
let _ = n_key_actions;
let _ = total_actions;
let _ = total_key_behaviors;
let _ = virtual_mods;
let _ = total_key_explicit;
let _ = total_mod_map_keys;
let _ = total_v_mod_map_keys;
assert_eq!(self.switch_expr(), u16::from(present), "switch `map` has an inconsistent discriminant");
if let Some(ref types_rtrn) = self.types_rtrn {
assert_eq!(types_rtrn.len(), usize::try_from(n_types).unwrap(), "`types_rtrn` has an incorrect length");
types_rtrn.serialize_into(bytes);
Expand Down Expand Up @@ -8327,16 +8316,7 @@ impl SetMapAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, present: u16, n_types: u8, n_key_syms: u8, n_key_actions: u8, total_actions: u16, total_key_behaviors: u8, virtual_mods: u16, total_key_explicit: u8, total_mod_map_keys: u8, total_v_mod_map_keys: u8) {
let _ = present;
let _ = n_types;
let _ = n_key_syms;
let _ = n_key_actions;
let _ = total_actions;
let _ = total_key_behaviors;
let _ = virtual_mods;
let _ = total_key_explicit;
let _ = total_mod_map_keys;
let _ = total_v_mod_map_keys;
assert_eq!(self.switch_expr(), u16::from(present), "switch `values` has an inconsistent discriminant");
if let Some(ref types) = self.types {
assert_eq!(types.len(), usize::try_from(n_types).unwrap(), "`types` has an incorrect length");
types.serialize_into(bytes);
Expand Down Expand Up @@ -9878,14 +9858,7 @@ impl GetNamesValueList {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, which: u32, n_types: u8, indicators: u32, virtual_mods: u16, group_names: u8, n_keys: u8, n_key_aliases: u8, n_radio_groups: u8) {
let _ = which;
let _ = n_types;
let _ = indicators;
let _ = virtual_mods;
let _ = group_names;
let _ = n_keys;
let _ = n_key_aliases;
let _ = n_radio_groups;
assert_eq!(self.switch_expr(), u32::from(which), "switch `value_list` has an inconsistent discriminant");
if let Some(keycodes_name) = self.keycodes_name {
keycodes_name.serialize_into(bytes);
}
Expand Down Expand Up @@ -10251,14 +10224,7 @@ impl SetNamesAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, which: u32, n_types: u8, indicators: u32, virtual_mods: u16, group_names: u8, n_keys: u8, n_key_aliases: u8, n_radio_groups: u8) {
let _ = which;
let _ = n_types;
let _ = indicators;
let _ = virtual_mods;
let _ = group_names;
let _ = n_keys;
let _ = n_key_aliases;
let _ = n_radio_groups;
assert_eq!(self.switch_expr(), u32::from(which), "switch `values` has an inconsistent discriminant");
if let Some(keycodes_name) = self.keycodes_name {
keycodes_name.serialize_into(bytes);
}
Expand Down Expand Up @@ -11242,16 +11208,7 @@ impl GetKbdByNameRepliesTypesMap {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, present: u16, n_types: u8, n_key_syms: u8, n_key_actions: u8, total_actions: u16, total_key_behaviors: u8, virtual_mods: u16, total_key_explicit: u8, total_mod_map_keys: u8, total_v_mod_map_keys: u8) {
let _ = present;
let _ = n_types;
let _ = n_key_syms;
let _ = n_key_actions;
let _ = total_actions;
let _ = total_key_behaviors;
let _ = virtual_mods;
let _ = total_key_explicit;
let _ = total_mod_map_keys;
let _ = total_v_mod_map_keys;
assert_eq!(self.switch_expr(), u16::from(present), "switch `map` has an inconsistent discriminant");
if let Some(ref types_rtrn) = self.types_rtrn {
assert_eq!(types_rtrn.len(), usize::try_from(n_types).unwrap(), "`types_rtrn` has an incorrect length");
types_rtrn.serialize_into(bytes);
Expand Down Expand Up @@ -11751,14 +11708,7 @@ impl GetKbdByNameRepliesKeyNamesValueList {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, which: u32, n_types: u8, indicators: u32, virtual_mods: u16, group_names: u8, n_keys: u8, n_key_aliases: u8, n_radio_groups: u8) {
let _ = which;
let _ = n_types;
let _ = indicators;
let _ = virtual_mods;
let _ = group_names;
let _ = n_keys;
let _ = n_key_aliases;
let _ = n_radio_groups;
assert_eq!(self.switch_expr(), u32::from(which), "switch `value_list` has an inconsistent discriminant");
if let Some(keycodes_name) = self.keycodes_name {
keycodes_name.serialize_into(bytes);
}
Expand Down
12 changes: 6 additions & 6 deletions x11rb-protocol/src/protocol/xproto.rs
Expand Up @@ -6924,7 +6924,7 @@ impl CreateWindowAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(background_pixmap) = self.background_pixmap {
background_pixmap.serialize_into(bytes);
}
Expand Down Expand Up @@ -7477,7 +7477,7 @@ impl ChangeWindowAttributesAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(background_pixmap) = self.background_pixmap {
background_pixmap.serialize_into(bytes);
}
Expand Down Expand Up @@ -8935,7 +8935,7 @@ impl ConfigureWindowAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u16) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u16::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(x) = self.x {
x.serialize_into(bytes);
}
Expand Down Expand Up @@ -16537,7 +16537,7 @@ impl CreateGCAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(function) = self.function {
u32::from(function).serialize_into(bytes);
}
Expand Down Expand Up @@ -17174,7 +17174,7 @@ impl ChangeGCAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(function) = self.function {
u32::from(function).serialize_into(bytes);
}
Expand Down Expand Up @@ -23393,7 +23393,7 @@ impl ChangeKeyboardControlAux {
result
}
fn serialize_into(&self, bytes: &mut Vec<u8>, value_mask: u32) {
let _ = value_mask;
assert_eq!(self.switch_expr(), u32::from(value_mask), "switch `value_list` has an inconsistent discriminant");
if let Some(key_click_percent) = self.key_click_percent {
key_click_percent.serialize_into(bytes);
}
Expand Down