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

Rework lookup of sub-elements with regard to special accepted elements #7436

Merged
merged 1 commit into from
Jan 23, 2025
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
88 changes: 45 additions & 43 deletions internal/compiler/langtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,27 +496,32 @@ impl ElementType {
}

/// This function looks at the element and checks whether it can have Elements of type `name` as children.
/// It returns an Error if that is not possible or an Option of the ElementType if it is.
/// The option is unset when the compiler does not know the type well enough to avoid further
/// probing.
pub fn accepts_child_element(
/// In addition to what `accepts_child_element` does, this method also probes the type of `name`.
/// It returns an Error if that is not possible or an `ElementType` if it is.
pub fn lookup_type_for_child_element(
&self,
name: &str,
tr: &TypeRegister,
) -> Result<Option<ElementType>, String> {
) -> Result<ElementType, String> {
match self {
Self::Component(component) if component.child_insertion_point.borrow().is_none() => {
let base_type = component.root_element.borrow().base_type.clone();
if base_type == tr.empty_type() {
return Err(format!("'{}' cannot have children. Only components with @children can have children", component.id));
}
return base_type.accepts_child_element(name, tr);
Self::Component(component) => {
let base_type = match &*component.child_insertion_point.borrow() {
Some(insert_in) => insert_in.0.borrow().base_type.clone(),
None => {
let base_type = component.root_element.borrow().base_type.clone();
if base_type == tr.empty_type() {
return Err(format!("'{}' cannot have children. Only components with @children can have children", component.id));
}
base_type
}
};
return base_type.lookup_type_for_child_element(name, tr);
}
Self::Builtin(builtin) => {
if let Some(child_type) = builtin.additional_accepted_child_types.get(name) {
return Ok(Some(child_type.clone()));
}
if builtin.disallow_global_types_as_child_elements {
if let Some(child_type) = builtin.additional_accepted_child_types.get(name) {
return Ok(child_type.clone());
}
let mut valid_children: Vec<_> =
builtin.additional_accepted_child_types.keys().cloned().collect();
valid_children.sort();
Expand All @@ -531,39 +536,36 @@ impl ElementType {
valid_children.join(" ")
)
};

return Err(err);
}
let err = match tr.lookup_element(name) {
Err(e) => e,
Ok(t) => {
if !tr.expose_internal_types
&& matches!(&t, Self::Builtin(e) if e.is_internal)
{
format!("Unknown element '{}'. (The type exist as an internal type, but cannot be accessed in this scope)", name)
} else {
return Ok(t);
}
}
};
if let Some(child_type) = builtin.additional_accepted_child_types.get(name) {
return Ok(child_type.clone());
}
match tr.lookup(name) {
Type::Invalid => Err(err),
ty => Err(format!("'{ty}' cannot be used as an element")),
}
}
_ => {}
};
Ok(None)
}

/// This function looks at the element and checks whether it can have Elements of type `name` as children.
/// In addition to what `accepts_child_element` does, this method also probes the type of `name`.
/// It returns an Error if that is not possible or an `ElementType` if it is.
pub fn lookup_type_for_child_element(
&self,
name: &str,
tr: &TypeRegister,
) -> Result<ElementType, String> {
if let Some(ct) = self.accepts_child_element(name, tr)? {
return Ok(ct);
_ => tr.lookup_element(name).and_then(|t| {
if !tr.expose_internal_types && matches!(&t, Self::Builtin(e) if e.is_internal) {
Err(format!("Unknown element '{}'. (The type exist as an internal type, but cannot be accessed in this scope)", name))
} else {
Ok(t)
}
})
}

tr.lookup_element(name).and_then(|t| {
if !tr.expose_internal_types && matches!(&t, Self::Builtin(e) if e.is_internal) {
Err(format!("Unknown element '{}'. (The type exist as an internal type, but cannot be accessed in this scope)", name))
} else {
Ok(t)
}
}).map_err(|s| {
match tr.lookup(name) {
Type::Invalid => s,
ty => format!("'{ty}' cannot be used as an element")
}
})
}

pub fn lookup_member_function(&self, name: &str) -> Option<BuiltinFunction> {
Expand Down
18 changes: 18 additions & 0 deletions internal/compiler/tests/syntax/elements/menubar-lookup.slint
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

export component MenuBar {
in property <int> foobar;
}


export component A inherits Window {
// This should lookup the re-defined menubar type and not the builtin one
MenuBar {
foobar: 42;
entries: [];
// ^error{Unknown property entries in MenuBar}
}
}


6 changes: 4 additions & 2 deletions tests/cases/children/children_placeholder_three_levels.slint
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ component Cde inherits HorizontalLayout {

component Fgh inherits HorizontalLayout {
Abc {}
Rectangle { background: yellow; @children}
Rectangle { background: yellow; GridLayout { @children } }
Rectangle { background: gray; }
}

Expand All @@ -38,7 +38,9 @@ export component TestCase inherits Window {

Ijk {
// Same as r2
r3 := Rectangle { background: brown; }
Row {
r3 := Rectangle { background: brown; }
}
}

out property r1_pos <=> r1.absolute-position;
Expand Down
Loading