Skip to content
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
26 changes: 18 additions & 8 deletions crates/wit-parser/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2791,6 +2791,20 @@ fn apply_map<T>(map: &[Option<Id<T>>], id: Id<T>, desc: &str, span: Option<Span>
}
}

fn rename(original_name: &str, include_name: &IncludeName) -> Option<String> {
if original_name == include_name.name {
return Some(include_name.as_.to_string());
}
let (kind, rest) = original_name.split_once(']')?;
match rest.split_once('.') {
Some((name, rest)) if name == include_name.name => {
Some(format!("{kind}]{}.{rest}", include_name.as_))
}
_ if rest == include_name.name => Some(format!("{kind}]{}", include_name.as_)),
_ => None,
}
}

impl Remap {
pub fn map_type(&self, id: TypeId, span: Option<Span>) -> Result<TypeId> {
apply_map(&self.types, id, "type", span)
Expand Down Expand Up @@ -3628,14 +3642,10 @@ impl Remap {
) -> Result<()> {
match item.0 {
WorldKey::Name(n) => {
let n = if let Some(found) = names
let n = names
.into_iter()
.find(|include_name| include_name.name == n.clone())
{
found.as_.clone()
} else {
n.clone()
};
.find_map(|include_name| rename(n, include_name))
.unwrap_or(n.clone());

// When the `with` option to the `include` directive is
// specified and is used to rename a function that means that
Expand Down Expand Up @@ -3690,7 +3700,7 @@ impl Remap {
fn remove_matching_name(&self, item: (&WorldKey, &WorldItem), names: &mut Vec<IncludeName>) {
match item.0 {
WorldKey::Name(n) => {
names.retain(|name| name.name != n.clone());
names.retain(|name| rename(n, name).is_none());
}
_ => {}
}
Expand Down
15 changes: 15 additions & 0 deletions crates/wit-parser/tests/ui/with-resource-as.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package foo:foo;

world a {
resource r {
constructor();
m: func();
sm: static func();
}
import f: func(x: r) -> option<r>;
export g: func(x: r) -> option<r>;
}

world b {
include a with { r as s }
}
221 changes: 221 additions & 0 deletions crates/wit-parser/tests/ui/with-resource-as.wit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
{
"worlds": [
{
"name": "a",
"imports": {
"r": {
"type": 0
},
"f": {
"function": {
"name": "f",
"kind": "freestanding",
"params": [
{
"name": "x",
"type": 2
}
],
"result": 3
}
},
"[constructor]r": {
"function": {
"name": "[constructor]r",
"kind": {
"constructor": 0
},
"params": [],
"result": 2
}
},
"[method]r.m": {
"function": {
"name": "[method]r.m",
"kind": {
"method": 0
},
"params": [
{
"name": "self",
"type": 1
}
]
}
},
"[static]r.sm": {
"function": {
"name": "[static]r.sm",
"kind": {
"static": 0
},
"params": []
}
}
},
"exports": {
"g": {
"function": {
"name": "g",
"kind": "freestanding",
"params": [
{
"name": "x",
"type": 2
}
],
"result": 3
}
}
},
"package": 0
},
{
"name": "b",
"imports": {
"s": {
"type": 4
},
"f": {
"function": {
"name": "f",
"kind": "freestanding",
"params": [
{
"name": "x",
"type": 5
}
],
"result": 6
}
},
"[constructor]s": {
"function": {
"name": "[constructor]s",
"kind": {
"constructor": 4
},
"params": [],
"result": 5
}
},
"[method]s.m": {
"function": {
"name": "[method]s.m",
"kind": {
"method": 4
},
"params": [
{
"name": "self",
"type": 7
}
]
}
},
"[static]s.sm": {
"function": {
"name": "[static]s.sm",
"kind": {
"static": 4
},
"params": []
}
}
},
"exports": {
"g": {
"function": {
"name": "g",
"kind": "freestanding",
"params": [
{
"name": "x",
"type": 5
}
],
"result": 6
}
}
},
"package": 0
}
],
"interfaces": [],
"types": [
{
"name": "r",
"kind": "resource",
"owner": {
"world": 0
}
},
{
"name": null,
"kind": {
"handle": {
"borrow": 0
}
},
"owner": null
},
{
"name": null,
"kind": {
"handle": {
"own": 0
}
},
"owner": null
},
{
"name": null,
"kind": {
"option": 2
},
"owner": null
},
{
"name": "s",
"kind": "resource",
"owner": {
"world": 1
}
},
{
"name": null,
"kind": {
"handle": {
"own": 4
}
},
"owner": null
},
{
"name": null,
"kind": {
"option": 5
},
"owner": null
},
{
"name": null,
"kind": {
"handle": {
"borrow": 4
}
},
"owner": null
}
],
"packages": [
{
"name": "foo:foo",
"interfaces": {},
"worlds": {
"a": 0,
"b": 1
}
}
]
}