Skip to content

Commit

Permalink
Remove use of the obsolete handle keyword (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
kulakowski-wasm authored Oct 3, 2022
1 parent 621f0ed commit 96b4e3f
Show file tree
Hide file tree
Showing 19 changed files with 59 additions and 98 deletions.
7 changes: 1 addition & 6 deletions crates/wit-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ enum Type<'a> {
Float64,
Char,
String,
Handle(Id<'a>),
Name(Id<'a>),
List(Box<Type<'a>>),
Record(Record<'a>),
Expand Down Expand Up @@ -494,10 +493,6 @@ impl<'a> Type<'a> {
Some((_span, Token::Float32)) => Ok(Type::Float32),
Some((_span, Token::Float64)) => Ok(Type::Float64),
Some((_span, Token::Char)) => Ok(Type::Char),
Some((_span, Token::Handle)) => {
let name = parse_id(tokens)?;
Ok(Type::Handle(name))
}

// tuple<T, U, ...>
Some((_span, Token::Tuple)) => {
Expand Down Expand Up @@ -592,7 +587,7 @@ impl<'a> Type<'a> {
name: tokens.parse_id(span)?.into(),
span,
})),
// `@foo`
// `%foo`
Some((span, Token::ExplicitId)) => Ok(Type::Name(Id {
name: tokens.parse_explicit_id(span)?.into(),
span,
Expand Down
3 changes: 0 additions & 3 deletions crates/wit-parser/src/ast/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub enum Token {
Float32,
Float64,
Char,
Handle,
Record,
Flags,
Variant,
Expand Down Expand Up @@ -250,7 +249,6 @@ impl<'a> Tokenizer<'a> {
"float32" => Float32,
"float64" => Float64,
"char" => Char,
"handle" => Handle,
"record" => Record,
"flags" => Flags,
"variant" => Variant,
Expand Down Expand Up @@ -513,7 +511,6 @@ impl Token {
Float32 => "keyword `float32`",
Float64 => "keyword `float64`",
Char => "keyword `char`",
Handle => "keyword `handle`",
Record => "keyword `record`",
Flags => "keyword `flags`",
Variant => "keyword `variant`",
Expand Down
40 changes: 17 additions & 23 deletions crates/wit-parser/src/ast/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,31 +379,25 @@ impl Resolver {
super::Type::Float64 => TypeDefKind::Type(Type::Float64),
super::Type::Char => TypeDefKind::Type(Type::Char),
super::Type::String => TypeDefKind::Type(Type::String),
super::Type::Handle(resource) => {
let id = match self.resource_lookup.get(&*resource.name) {
Some(id) => *id,
None => {
return Err(Error {
span: resource.span,
msg: format!("no resource named `{}`", resource.name),
}
.into())
}
};
TypeDefKind::Type(Type::Handle(id))
}
super::Type::Name(name) => {
let id = match self.type_lookup.get(&*name.name) {
Some(id) => *id,
None => {
return Err(Error {
span: name.span,
msg: format!("no type named `{}`", name.name),
// Because resources are referred to directly by name,
// first check to see if we can look up this name as a
// resource.
if let Some(id) = self.resource_lookup.get(&*name.name) {
TypeDefKind::Type(Type::Handle(*id))
} else {
let id = match self.type_lookup.get(&*name.name) {
Some(id) => *id,
None => {
return Err(Error {
span: name.span,
msg: format!("no type named `{}`", name.name),
}
.into())
}
.into())
}
};
TypeDefKind::Type(Type::Id(id))
};
TypeDefKind::Type(Type::Id(id))
}
}
super::Type::List(list) => {
let ty = self.resolve_type(list)?;
Expand Down
4 changes: 3 additions & 1 deletion crates/wit-parser/tests/ui/comments.wit
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


type /* foo */ bar /* baz */ = //
handle //
stream < //
//
//

Expand All @@ -16,4 +16,6 @@ handle //

x

>

resource /* x */ x // ...
5 changes: 4 additions & 1 deletion crates/wit-parser/tests/ui/comments.wit.result
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
{
"idx": 0,
"name": "bar",
"primitive": "handle-0"
"stream": {
"element": "handle-0",
"end": null
}
},
{
"idx": 1,
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-parser/tests/ui/import-me-too.wit.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ resource x
## `handle`
```wit
/// This is handle.
type %handle = handle x
type handle = x
```

## `some-record`
Expand Down
2 changes: 1 addition & 1 deletion crates/wit-parser/tests/ui/import-me.wit
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ type foo = u32

resource x

type %handle = handle x
type handle = x

type some-record = tuple<u32, u64, float32>
8 changes: 3 additions & 5 deletions crates/wit-parser/tests/ui/imports-from-wit-md.wit
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ use { x as import-me-x } from import-me-too

type x = foo
type y = bar
type z1 = import-me-x
type z2 = handle import-me-x
type z = import-me-x

use { %handle } from import-me-too
use { handle } from import-me-too
resource xyz
type my-handle = handle xyz
type my-handle2 = xyz
type my-handle = xyz

use { some-record } from import-me-too
16 changes: 3 additions & 13 deletions crates/wit-parser/tests/ui/imports-from-wit-md.wit.result
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,17 @@
},
{
"idx": 6,
"name": "z1",
"primitive": "type-1"
},
{
"idx": 7,
"name": "z2",
"name": "z",
"primitive": "handle-0"
},
{
"idx": 8,
"idx": 7,
"primitive": "handle-1"
},
{
"idx": 9,
"idx": 8,
"name": "my-handle",
"primitive": "handle-1"
},
{
"idx": 10,
"name": "my-handle2",
"primitive": "type-8"
}
]
}
6 changes: 2 additions & 4 deletions crates/wit-parser/tests/ui/imports.wit
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use { x as import-me-x } from import-me
type x = foo
type y = bar
type z1 = import-me-x
type z2 = handle import-me-x

use { %handle } from import-me
use { handle } from import-me
resource xyz
type my-handle = handle xyz
type my-handle2 = xyz
type my-handle = xyz

use { some-record } from import-me
14 changes: 2 additions & 12 deletions crates/wit-parser/tests/ui/imports.wit.result
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,16 @@
{
"idx": 6,
"name": "z1",
"primitive": "type-1"
},
{
"idx": 7,
"name": "z2",
"primitive": "handle-0"
},
{
"idx": 8,
"idx": 7,
"primitive": "handle-1"
},
{
"idx": 9,
"idx": 8,
"name": "my-handle",
"primitive": "handle-1"
},
{
"idx": 10,
"name": "my-handle2",
"primitive": "type-8"
}
]
}
3 changes: 1 addition & 2 deletions crates/wit-parser/tests/ui/imports2.wit
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use * from import-me

type my-handle = handle x
type my-handle2 = x
type my-handle = x
5 changes: 0 additions & 5 deletions crates/wit-parser/tests/ui/imports2.wit.result
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@
"idx": 4,
"name": "my-handle",
"primitive": "handle-0"
},
{
"idx": 5,
"name": "my-handle2",
"primitive": "type-3"
}
]
}
2 changes: 2 additions & 0 deletions crates/wit-parser/tests/ui/parse-fail/alias-no-type.wit
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// parse-fail
type foo = bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
no type named `bar`
--> tests/ui/parse-fail/alias-no-type.wit:2:12
|
2 | type foo = bar
| ^--
2 changes: 0 additions & 2 deletions crates/wit-parser/tests/ui/parse-fail/handle-no-resource.wit

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion crates/wit-parser/tests/ui/types.wit
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type t15 = result<u32, u32>
type t16 = result<_, u32>
type t17 = result<u32>
type t18 = result
type t19 = handle x
type t19 = x
record t20 {}
record t21 { a: u32 }
record t22 { a: u32, }
Expand Down
26 changes: 13 additions & 13 deletions crates/wit-parser/tests/ui/wasi-http.wit
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
//
// Note: the `blocking` attribute means that different lowering can be generated for this
// function, either based on synchronous, blocking calls, or on a callback-style interface.
fetch: /*blocking*/ func(req: handle request) -> handle response
fetch: /*blocking*/ func(req: request) -> response

/// A request resource, with lots of things missing for now.
// Note: `resource` blocks serve two purposes:
// 1. namespacing the definition of a Handle type with functions for operating on that handle
// 2. providing a way to define said functions such that they implicitly take the handle itself as
// the first argument. E.g., `method` in `request` is roughly equivalent to
// `request::method: func(self: handle request) -> (string)`
// `request::method: func(self: request) -> (string)`
// ("Roughly" because the `resource` semantics allow us to generate better bindings for languages
// that have a concept of implicit receivers, such as `this` in JS.)
resource request {
Expand All @@ -47,26 +47,26 @@ resource request {
/// @return req - returns a new `request`
// Note: `static` here simply means that no implicit receiver argument will be passed.
// I.e., this is ~roughly~ analogous to a top-level definition of
// `request::request: func() -> (req: handle request)`
// `request::request: func() -> (req: request)`
// whereas without `static`, it'd be analogous to
// `request::request: func(self: handle<request>) -> (req: handle<request>)`
static request: func() -> handle request
// `request::request: func(self: request) -> (req: request)`
static request: func() -> request

method: func() -> string

// Note: We could consider allowing the parens to be omitted for single return values, like so:
headers: func() -> handle headers
headers: func() -> headers

// Note: Or we could even allow leaving off the return value identifier, making it use the
// function name, like so:
body: func() -> handle body // This return type would be shorthand for `(body: body)`
body: func() -> body // This return type would be shorthand for `(body: body)`
}

/// A response resource, with lots of things missing for now.
resource response {
status: func() -> u16
headers: func() -> handle headers
body: func() -> handle body
headers: func() -> headers
body: func() -> body
}

/// A headers resource, with lots of things missing for now.
Expand Down Expand Up @@ -101,13 +101,13 @@ interface nested-interface1 {
resource better-request {
// TODO: do we need to have a ctor with special semantics? E.g. to generate actual
// constructors for languages that have them, such as JS?
new: func(request: handle<request>) -> handle<better-request>
new: func(request: request) -> better-request
// Note: sadly, the sauce better-request uses must remain secret, so it doesn't actually
// expose any of its functionality, and hence doesn't need any other methods.
}
/// Maps a request to an even better request.
// Note: the containing scope's members are in scope in a nested interface
fun: func(request: handle<request>) -> handle<response>
fun: func(request: request) -> response
}
/// Another nested interface, doing something even more neat and elaborate.
Expand All @@ -127,7 +127,7 @@ interface nested-interface2 {
/// @return response - a boring, normal response
/// @return added-shiny - the shiny!
// Note: this just exists to demonstrate multiple return values, including their documentation
fun: func(request: better-request) -> tuple<handle<response>, handle<the-thiny>>
fun: func(request: better-request) -> tuple<response, the-shiny>
}
*/

Expand Down Expand Up @@ -155,4 +155,4 @@ record timestamp {
my-int: u32

/// A handle to a request
my-request: handle request
my-request: request

0 comments on commit 96b4e3f

Please sign in to comment.