mirror of
https://github.com/salsa-rs/salsa.git
synced 2025-01-22 21:05:11 +00:00
359: fix: typos in tutorial ir chapter r=nikomatsakis a=dawnofmidnight Looking at the tutorial, I believe `ProgramSource` is a typo for `SourceProgram`. I'm also pretty sure the `define` -> `derive` is correct, unless `define` is some attribute I'm not aware of. 361: test for compiling failure: lru can not be used with specify r=nikomatsakis a=XFFXFF ref #354 Test for compile failures with [tybuild](https://github.com/dtolnay/trybuild.git). This is an example to show how it works. Co-authored-by: dawn <78233879+dawnofmidnight@users.noreply.github.com> Co-authored-by: XFFXFF <1247714429@qq.com>
This commit is contained in:
commit
80d0d14194
5 changed files with 39 additions and 6 deletions
|
@ -34,7 +34,7 @@ Inputs are defined as Rust structs with a `#[salsa::input]` annotation:
|
||||||
{{#include ../../../calc-example/calc/src/ir.rs:input}}
|
{{#include ../../../calc-example/calc/src/ir.rs:input}}
|
||||||
```
|
```
|
||||||
|
|
||||||
In our compiler, we have just one simple input, the `ProgramSource`, which has a `text` field (the string).
|
In our compiler, we have just one simple input, the `SourceProgram`, which has a `text` field (the string).
|
||||||
|
|
||||||
### The data lives in the database
|
### The data lives in the database
|
||||||
|
|
||||||
|
@ -43,18 +43,18 @@ The values of their fields are stored in the salsa database, and the struct itse
|
||||||
This means that the struct instances are copy (no matter what fields they contain).
|
This means that the struct instances are copy (no matter what fields they contain).
|
||||||
Creating instances of the struct and accessing fields is done by invoking methods like `new` as well as getters and setters.
|
Creating instances of the struct and accessing fields is done by invoking methods like `new` as well as getters and setters.
|
||||||
|
|
||||||
More concretely, the `#[salsa::input]` annotation will generate a struct for `ProgramSource` like this:
|
More concretely, the `#[salsa::input]` annotation will generate a struct for `SourceProgram` like this:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
#[define(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
pub struct ProgramSource(salsa::Id);
|
pub struct SourceProgram(salsa::Id);
|
||||||
```
|
```
|
||||||
|
|
||||||
It will also generate a method `new` that lets you create a `ProgramSource` in the database.
|
It will also generate a method `new` that lets you create a `SourceProgram` in the database.
|
||||||
For an input, a `&mut db` reference is required, along with the values for each field:
|
For an input, a `&mut db` reference is required, along with the values for each field:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
let source = ProgramSource::new(&mut db, "print 11 + 11".to_string());
|
let source = SourceProgram::new(&mut db, "print 11 + 11".to_string());
|
||||||
```
|
```
|
||||||
|
|
||||||
You can read the value of the field with `source.text(&db)`,
|
You can read the value of the field with `source.text(&db)`,
|
||||||
|
|
|
@ -11,3 +11,4 @@ expect-test = "1.4.0"
|
||||||
parking_lot = "0.12.1"
|
parking_lot = "0.12.1"
|
||||||
test-log = "0.2.11"
|
test-log = "0.2.11"
|
||||||
env_logger = "*"
|
env_logger = "*"
|
||||||
|
trybuild = "1.0"
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
#[salsa::jar(db = Db)]
|
||||||
|
struct Jar(MyInput, lru_can_not_be_used_with_specify);
|
||||||
|
|
||||||
|
trait Db: salsa::DbWithJar<Jar> {}
|
||||||
|
|
||||||
|
#[salsa::input(jar = Jar)]
|
||||||
|
struct MyInput {
|
||||||
|
field: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[salsa::tracked(jar = Jar, lru = 3, specify)]
|
||||||
|
fn lru_can_not_be_used_with_specify(db: &dyn Db, input: MyInput) -> u32 {
|
||||||
|
input.field(db)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
|
@ -0,0 +1,11 @@
|
||||||
|
error: `specify` and `lru` cannot be used together
|
||||||
|
--> tests/compile-fail/lru_can_not_be_used_with_specify.rs:11:38
|
||||||
|
|
|
||||||
|
11 | #[salsa::tracked(jar = Jar, lru = 3, specify)]
|
||||||
|
| ^^^^^^^
|
||||||
|
|
||||||
|
error[E0412]: cannot find type `lru_can_not_be_used_with_specify` in this scope
|
||||||
|
--> tests/compile-fail/lru_can_not_be_used_with_specify.rs:2:21
|
||||||
|
|
|
||||||
|
2 | struct Jar(MyInput, lru_can_not_be_used_with_specify);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
|
5
salsa-2022-tests/tests/compile_fail.rs
Normal file
5
salsa-2022-tests/tests/compile_fail.rs
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#[test]
|
||||||
|
fn compile_fail() {
|
||||||
|
let t = trybuild::TestCases::new();
|
||||||
|
t.compile_fail("tests/compile-fail/*.rs");
|
||||||
|
}
|
Loading…
Reference in a new issue