Skip to content

Commit f9a0f2b

Browse files
committed
Don't include MainThreadMarker in methods that do not need it
1 parent a39aadb commit f9a0f2b

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

crates/header-translator/src/cache.rs

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -105,45 +105,46 @@ impl<'a> Cache<'a> {
105105
}
106106
});
107107

108-
match (method.is_class, self.mainthreadonly_classes.contains(id)) {
109-
// MainThreadOnly class with static method
110-
(true, true) => {
111-
// Assume the method needs main thread
112-
result_type_contains_mainthreadonly = true;
113-
}
114-
// Class with static method
115-
(true, false) => {
116-
// Continue with the normal check
117-
}
118-
// MainThreadOnly class with non-static method
119-
(false, true) => {
120-
// Method is already required to run on main
121-
// thread, so no need to add MainThreadMarker
122-
continue;
123-
}
124-
// Class with non-static method
125-
(false, false) => {
126-
// Continue with the normal check
127-
}
108+
let mut any_argument_contains_mainthreadonly: bool = false;
109+
for (_, argument) in method.arguments.iter() {
110+
// Important: We only visit the top-level types, to not
111+
// include optional arguments like `Option<&NSView>` or
112+
// `&NSArray<NSView>`.
113+
argument.visit_toplevel_types(&mut |id| {
114+
if self.mainthreadonly_classes.contains(id) {
115+
any_argument_contains_mainthreadonly = true;
116+
}
117+
});
128118
}
129119

130-
if result_type_contains_mainthreadonly {
131-
let mut any_argument_contains_mainthreadonly: bool = false;
132-
for (_, argument) in method.arguments.iter() {
133-
// Important: We only visit the top-level types, to not
134-
// include e.g. `Option<&NSView>` or `&NSArray<NSView>`.
135-
argument.visit_toplevel_types(&mut |id| {
136-
if self.mainthreadonly_classes.contains(id) {
137-
any_argument_contains_mainthreadonly = true;
138-
}
139-
});
120+
if self.mainthreadonly_classes.contains(id) {
121+
if method.is_class {
122+
// Assume the method needs main thread if it's
123+
// declared on a main thread only class.
124+
result_type_contains_mainthreadonly = true;
125+
} else {
126+
// Method takes `&self` or `&mut self`, or is
127+
// an initialization method, all of which
128+
// already require the main thread.
129+
//
130+
// Note: Initialization methods can be passed
131+
// `None`, but in that case the return will
132+
// always be NULL.
133+
any_argument_contains_mainthreadonly = true;
140134
}
135+
}
141136

142-
// Apply main thread only, unless a (required)
143-
// argument was main thread only.
144-
if !any_argument_contains_mainthreadonly {
145-
method.mainthreadonly = true;
146-
}
137+
if any_argument_contains_mainthreadonly {
138+
// MainThreadMarker can be retrieved from
139+
// `MainThreadMarker::from` inside these methods,
140+
// and hence passing it is redundant.
141+
method.mainthreadonly = false;
142+
} else if result_type_contains_mainthreadonly {
143+
method.mainthreadonly = true;
144+
} else {
145+
// If neither, then we respect any annotation
146+
// the method may have had before
147+
// method.mainthreadonly = method.mainthreadonly;
147148
}
148149
}
149150
}

crates/icrate/src/generated

0 commit comments

Comments
 (0)