use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::files::{SimpleFile, SimpleFiles}; use codespan_reporting::term::{Chars, Config, DisplayStyle}; use std::sync::LazyLock;
let file_id1 = files.add( "one_line.rs",
unindent::unindent(
r#" fn main() { letmut v = vec![Some("foo"), Some("bar")];
v.push(v.pop().unwrap());
} "#,
),
);
let diagnostics = vec![
Diagnostic::error()
.with_code("E0499")
.with_message("cannot borrow `v` as mutable more than once at a time")
.with_labels(vec![
Label::primary(file_id1, 71..72)
.with_message("second mutable borrow occurs here"),
Label::secondary(file_id1, 64..65)
.with_message("first borrow later used by call"),
Label::secondary(file_id1, 66..70)
.with_message("first mutable borrow occurs here"),
]),
Diagnostic::error()
.with_message("aborting due to previous error")
.with_notes(vec![ "For more information about this error, try `rustc --explain E0499`."
.to_owned(),
]),
];
thread::spawn(move|| { let y = x;
println!("{:?}", y);
});
} "#,
),
);
let diagnostics = vec![
Diagnostic::error()
.with_code("E0666")
.with_message("nested `impl Trait` is not allowed")
.with_labels(vec![
Label::primary(file_id1, 129..139)
.with_message("nested `impl Trait` here"),
Label::secondary(file_id1, 119..140)
.with_message("outer `impl Trait`"),
]),
Diagnostic::error()
.with_code("E0121")
.with_message("the type placeholder `_` is not allowed within types on item signatures")
.with_labels(vec![
Label::primary(file_id2, 17..18)
.with_message("not allowed in type signatures"),
Label::secondary(file_id2, 17..18)
.with_message("help: replace with the correct return type: `i32`"),
]),
Diagnostic::error()
.with_code("E0121")
.with_message("the type placeholder `_` is not allowed within types on item signatures")
.with_labels(vec![
Label::primary(file_id2, 49..50)
.with_message("not allowed in type signatures"),
Label::primary(file_id2, 52..53)
.with_message("not allowed in type signatures"),
Label::secondary(file_id2, 48..54)
.with_message("help: replace with the correct return type: `(i32, i32)`"),
]),
Diagnostic::error()
.with_code("E0277")
.with_message("`std::rc::Rc<()>` cannot be sent between threads safely")
.with_labels(vec![
Label::primary(file_id4, 339..352)
.with_message("`std::rc::Rc<()>` cannot be sent between threads safely"),
Label::secondary(file_id4, 353..416)
.with_message("within this `[closure@no_send_res_ports.rs:29:19: 33:6 x:main::Foo]`"),
Label::secondary(file_id3, 141..145)
.with_message("required by this bound in `std::thread::spawn`"),
])
.with_notes(vec![ "help: within `[closure@no_send_res_ports.rs:29:19: 33:6 x:main::Foo]`, the trait `std::marker::Send` is not implemented for `std::rc::Rc<()>`".to_owned(), "note: required because it appears within the type `Port<()>`".to_owned(), "note: required because it appears within the type `main::Foo`".to_owned(), "note: required because it appears within the type `[closure@no_send_res_ports.rs:29:19: 33:6 x:main::Foo]`".to_owned(),
]),
Diagnostic::error()
.with_message("aborting due 5 previous errors")
.with_notes(vec![ "Some errors have detailed explanations: E0121, E0277, E0666.".to_owned(), "For more information about an error, try `rustc --explain E0121`.".to_owned(),
]),
];
let diagnostics = vec![
Diagnostic::note()
.with_message("middle")
.with_labels(vec![Label::primary((), 6..6).with_message("middle")]),
Diagnostic::note()
.with_message("end of line")
.with_labels(vec![Label::primary((), 12..12).with_message("end of line")]),
Diagnostic::note()
.with_message("end of line")
.with_labels(vec![Label::primary((), 23..23).with_message("end of line")]),
Diagnostic::note()
.with_message("end of file")
.with_labels(vec![
Label::primary((), eof..eof).with_message("end of file")
]),
];
_-_ : Nat → Nat → Nat
n₁ - zero = n₁
zero - succ n₂ = zero
succ n₁ - succ n₂ = n₁ - n₂ ",
),
);
let file_id2 = files.add( "Test.fun",
unindent::unindent(
r#"
module Test where
_ : Nat
_ = 123 + "hello" "#,
),
);
let diagnostics = vec![ // Unknown builtin error
Diagnostic::error()
.with_message("unknown builtin: `NATRAL`")
.with_labels(vec![
Label::primary(file_id1, 96..102).with_message("unknown builtin")
])
.with_notes(vec![ "there is a builtin with a similar name: `NATURAL`".to_owned()
]), // Unused parameter warning
Diagnostic::warning()
.with_message("unused parameter pattern: `n₂`")
.with_labels(vec![
Label::primary(file_id1, 285..289).with_message("unused parameter")
])
.with_notes(vec!["consider using a wildcard pattern: `_`".to_owned()]), // Unexpected type error
Diagnostic::error()
.with_message("unexpected type in application of `_+_`")
.with_code("E0001")
.with_labels(vec![
Label::primary(file_id2, 37..44)
.with_message("expected `Nat`, found `String`"),
Label::secondary(file_id1, 130..155)
.with_message("based on the definition of `_+_`"),
])
.with_notes(vec![unindent::unindent( "
expected type `Nat`
found type `String` ",
)]),
];
let file_id = files.add( "FizzBuzz.fun",
unindent::unindent(
r#"
module FizzBuzz where
fizz₁ : Nat → String
fizz₁ num = case (mod num 5) (mod num 3) of 00 => "FizzBuzz" 0 _ => "Fizz"
_ 0 => "Buzz"
_ _ => num
fizz₂ : Nat → String
fizz₂ num =
case (mod num 5) (mod num 3) of 00 => "FizzBuzz" 0 _ => "Fizz"
_ 0 => "Buzz"
_ _ => num "#,
),
);
let diagnostics = vec![ // Incompatible match clause error
Diagnostic::error()
.with_message("`case` clauses have incompatible types")
.with_code("E0308")
.with_labels(vec![
Label::primary(file_id, 163..166)
.with_message("expected `String`, found `Nat`"),
Label::secondary(file_id, 62..166)
.with_message("`case` clauses have incompatible types"),
Label::secondary(file_id, 41..47)
.with_message("expected type `String` found here"),
])
.with_notes(vec![unindent::unindent( "
expected type `String`
found type `Nat` ",
)]), // Incompatible match clause error
Diagnostic::error()
.with_message("`case` clauses have incompatible types")
.with_code("E0308")
.with_labels(vec![
Label::primary(file_id, 328..331)
.with_message("expected `String`, found `Nat`"),
Label::secondary(file_id, 211..331)
.with_message("`case` clauses have incompatible types"),
Label::secondary(file_id, 258..268)
.with_message("this is found to be of type `String`"),
Label::secondary(file_id, 284..290)
.with_message("this is found to be of type `String`"),
Label::secondary(file_id, 306..312)
.with_message("this is found to be of type `String`"),
Label::secondary(file_id, 186..192)
.with_message("expected type `String` found here"),
])
.with_notes(vec![unindent::unindent( "
expected type `String`
found type `Nat` ",
)]),
];
let diagnostics = vec![Diagnostic::error()
.with_message("match arms have incompatible types")
.with_code("E0308")
.with_labels(vec![ // this secondary label is before the primary label to test the locus calculation (see issue #259)
Label::secondary((), 89..134).with_message( "this is found to be of type `Result<ByteIndex, LineIndexOutOfBoundsError>`",
),
Label::primary((), 230..351).with_message( "expected enum `Result`, found struct `LineIndexOutOfBoundsError`",
),
Label::secondary((), 8..362).with_message("`match` arms have incompatible types"),
Label::secondary((), 167..195).with_message( "this is found to be of type `Result<ByteIndex, LineIndexOutOfBoundsError>`",
),
])
.with_notes(vec![unindent::unindent( "
expected type `Result<ByteIndex, LineIndexOutOfBoundsError>`
found type `LineIndexOutOfBoundsError` ",
)])];
static TEST_DATA: LazyTestData<'static, SimpleFile<&'static str, String>> =
LazyLock::new(|| { let moon_phases = r#""#.to_string(); let invalid_start = 1; let invalid_end = "".len() - 1;
assert!(!moon_phases.is_char_boundary(invalid_start));
assert!(!moon_phases.is_char_boundary(invalid_end));
assert_eq!("".len(), 4); let file = SimpleFile::new("moon_jump.rs", moon_phases); let diagnostics = vec![
Diagnostic::error()
.with_code("E01")
.with_message("cow may not jump during new moon.")
.with_labels(vec![
Label::primary((), invalid_start..invalid_end).with_message("Invalid jump")
]),
Diagnostic::note()
.with_message("invalid unicode range")
.with_labels(vec![Label::secondary((), invalid_start.."".len())
.with_message("Cow range does not start at boundary.")]),
Diagnostic::note()
.with_message("invalid unicode range")
.with_labels(vec![Label::secondary((), "".len().."".len() - 1)
.with_message("Cow range does not end at boundary.")]),
Diagnostic::note()
.with_message("invalid unicode range")
.with_labels(vec![Label::secondary(
(),
invalid_start.."".len() - 1,
)
.with_message("Cow does not start or end at boundary.")]),
];
TestData {
files: file,
diagnostics,
}
});
static TEST_DATA: LazyTestData<'static, SimpleFile<&'static str, String>> = LazyLock::new(
|| { let file = SimpleFile::new( "tests/main.js",
unindent::unindent( "\"use strict\"; let zero=0;
function foo() {
\"use strict\";
one=1;
}",
),
); let diagnostics = vec![
Diagnostic::warning()
.with_code("ParserWarning")
.with_message("The strict mode declaration in the body of function `foo` is redundant, as the outer scope is already in strict mode")
.with_labels(vec![
Label::primary((), 45..57)
.with_message("This strict mode declaration is redundant"),
Label::secondary((), 0..12)
.with_message("Strict mode is first declared here"),
]),
];
TestData {
files: file,
diagnostics,
}
},
);
let file_id1 = files.add( "empty_if_comments.lua",
[ "elseif 3 then", // primary label starts here "", // context line "", "", "", "", "", "", "", // context line "else", // primary label ends here
]
.join("\n"),
);
let file_id2 = files.add( "src/lib.rs",
[ "fn main() {", " 1", // primary label starts here " + 1", // context line " + 1", // skip " + 1", // skip " + 1", // skip " +1", // secondary label here " + 1", // this single line will not be skipped; the previously filtered out label must be retrieved " + 1", // context line " + 1", // primary label ends here "}",
]
.join("\n"),
);
let diagnostics = vec![
Diagnostic::error()
.with_message("empty elseif block")
.with_code("empty_if")
.with_labels(vec![
Label::primary(file_id1, 0..23),
Label::secondary(file_id1, 15..21)
.with_message("content should be in here"),
]),
Diagnostic::error()
.with_message("mismatched types")
.with_code("E0308")
.with_labels(vec![
Label::primary(file_id2, 17..80).with_message("expected (), found integer"),
Label::secondary(file_id2, 55..55).with_message("missing whitespace"),
])
.with_notes(vec![ "note:\texpected type `()`\n\tfound type `{integer}`".to_owned()
]),
];
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.