| 1 | |
| 2 |
|
| 3 | #[cfg(feature = "parsing")]
|
| 4 | use crate::buffer::Cursor;
|
| 5 | use crate::thread::ThreadBound;
|
| 6 | use proc_macro2::{
|
| 7 | Delimiter, Group, Ident, LexError, Literal, Punct, Spacing, Span, TokenStream, TokenTree,
|
| 8 | };
|
| 9 | #[cfg(feature = "printing")]
|
| 10 | use quote::ToTokens;
|
| 11 | use std::fmt::{self, Debug, Display};
|
| 12 | use std::slice;
|
| 13 | use std::vec;
|
| 14 |
|
| 15 | |
| 16 | pub type Result<T> = std::result::Result<T, Error>;
|
| 17 |
|
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | pub struct Error {
|
| 103 | messages: Vec<ErrorMessage>,
|
| 104 | }
|
| 105 |
|
| 106 | struct ErrorMessage {
|
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | span: ThreadBound<SpanRange>,
|
| 113 | message: String,
|
| 114 | }
|
| 115 |
|
| 116 | |
| 117 | |
| 118 | |
| 119 | struct SpanRange {
|
| 120 | start: Span,
|
| 121 | end: Span,
|
| 122 | }
|
| 123 |
|
| 124 | #[cfg(test)]
|
| 125 | struct _Test
|
| 126 | where
|
| 127 | Error: Send + Sync;
|
| 128 |
|
| 129 | impl Error {
|
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | pub fn new<T: Display>(span: Span, message: T) -> Self {
|
| 161 | return new(span, message.to_string());
|
| 162 |
|
| 163 | fn new(span: Span, message: String) -> Error {
|
| 164 | Error {
|
| 165 | messages: vec![ErrorMessage {
|
| 166 | span: ThreadBound::new(SpanRange {
|
| 167 | start: span,
|
| 168 | end: span,
|
| 169 | }),
|
| 170 | message,
|
| 171 | }],
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | #[cfg(feature = "printing")]
|
| 190 | #[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
|
| 191 | pub fn new_spanned<T: ToTokens, U: Display>(tokens: T, message: U) -> Self {
|
| 192 | return new_spanned(tokens.into_token_stream(), message.to_string());
|
| 193 |
|
| 194 | fn new_spanned(tokens: TokenStream, message: String) -> Error {
|
| 195 | let mut iter = tokens.into_iter();
|
| 196 | let start = iter.next().map_or_else(Span::call_site, |t| t.span());
|
| 197 | let end = iter.last().map_or(start, |t| t.span());
|
| 198 | Error {
|
| 199 | messages: vec![ErrorMessage {
|
| 200 | span: ThreadBound::new(SpanRange { start, end }),
|
| 201 | message,
|
| 202 | }],
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | pub fn span(&self) -> Span {
|
| 213 | let SpanRange { start, end } = match self.messages[0].span.get() {
|
| 214 | Some(span) => *span,
|
| 215 | None => return Span::call_site(),
|
| 216 | };
|
| 217 | start.join(end).unwrap_or(start)
|
| 218 | }
|
| 219 |
|
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | pub fn to_compile_error(&self) -> TokenStream {
|
| 228 | self.messages
|
| 229 | .iter()
|
| 230 | .map(ErrorMessage::to_compile_error)
|
| 231 | .collect()
|
| 232 | }
|
| 233 |
|
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | pub fn into_compile_error(self) -> TokenStream {
|
| 267 | self.to_compile_error()
|
| 268 | }
|
| 269 |
|
| 270 | |
| 271 | |
| 272 | pub fn combine(&mut self, another: Error) {
|
| 273 | self.messages.extend(another.messages);
|
| 274 | }
|
| 275 | }
|
| 276 |
|
| 277 | impl ErrorMessage {
|
| 278 | fn to_compile_error(&self) -> TokenStream {
|
| 279 | let (start, end) = match self.span.get() {
|
| 280 | Some(range) => (range.start, range.end),
|
| 281 | None => (Span::call_site(), Span::call_site()),
|
| 282 | };
|
| 283 |
|
| 284 | |
| 285 | TokenStream::from_iter([
|
| 286 | TokenTree::Punct({
|
| 287 | let mut punct = Punct::new(':', Spacing::Joint);
|
| 288 | punct.set_span(start);
|
| 289 | punct
|
| 290 | }),
|
| 291 | TokenTree::Punct({
|
| 292 | let mut punct = Punct::new(':', Spacing::Alone);
|
| 293 | punct.set_span(start);
|
| 294 | punct
|
| 295 | }),
|
| 296 | TokenTree::Ident(Ident::new("core", start)),
|
| 297 | TokenTree::Punct({
|
| 298 | let mut punct = Punct::new(':', Spacing::Joint);
|
| 299 | punct.set_span(start);
|
| 300 | punct
|
| 301 | }),
|
| 302 | TokenTree::Punct({
|
| 303 | let mut punct = Punct::new(':', Spacing::Alone);
|
| 304 | punct.set_span(start);
|
| 305 | punct
|
| 306 | }),
|
| 307 | TokenTree::Ident(Ident::new("compile_error", start)),
|
| 308 | TokenTree::Punct({
|
| 309 | let mut punct = Punct::new('!', Spacing::Alone);
|
| 310 | punct.set_span(start);
|
| 311 | punct
|
| 312 | }),
|
| 313 | TokenTree::Group({
|
| 314 | let mut group = Group::new(Delimiter::Brace, {
|
| 315 | TokenStream::from_iter([TokenTree::Literal({
|
| 316 | let mut string = Literal::string(&self.message);
|
| 317 | string.set_span(end);
|
| 318 | string
|
| 319 | })])
|
| 320 | });
|
| 321 | group.set_span(end);
|
| 322 | group
|
| 323 | }),
|
| 324 | ])
|
| 325 | }
|
| 326 | }
|
| 327 |
|
| 328 | #[cfg(feature = "parsing")]
|
| 329 | pub(crate) fn new_at<T: Display>(scope: Span, cursor: Cursor, message: T) -> Error {
|
| 330 | if cursor.eof() {
|
| 331 | Error::new(scope, format!("unexpected end of input, {}", message))
|
| 332 | } else {
|
| 333 | let span = crate::buffer::open_span_of_group(cursor);
|
| 334 | Error::new(span, message)
|
| 335 | }
|
| 336 | }
|
| 337 |
|
| 338 | #[cfg(all(feature = "parsing", any(feature = "full", feature = "derive")))]
|
| 339 | pub(crate) fn new2<T: Display>(start: Span, end: Span, message: T) -> Error {
|
| 340 | return new2(start, end, message.to_string());
|
| 341 |
|
| 342 | fn new2(start: Span, end: Span, message: String) -> Error {
|
| 343 | Error {
|
| 344 | messages: vec![ErrorMessage {
|
| 345 | span: ThreadBound::new(SpanRange { start, end }),
|
| 346 | message,
|
| 347 | }],
|
| 348 | }
|
| 349 | }
|
| 350 | }
|
| 351 |
|
| 352 | impl Debug for Error {
|
| 353 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
| 354 | if self.messages.len() == 1 {
|
| 355 | formatter
|
| 356 | .debug_tuple("Error")
|
| 357 | .field(&self.messages[0])
|
| 358 | .finish()
|
| 359 | } else {
|
| 360 | formatter
|
| 361 | .debug_tuple("Error")
|
| 362 | .field(&self.messages)
|
| 363 | .finish()
|
| 364 | }
|
| 365 | }
|
| 366 | }
|
| 367 |
|
| 368 | impl Debug for ErrorMessage {
|
| 369 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
| 370 | Debug::fmt(&self.message, formatter)
|
| 371 | }
|
| 372 | }
|
| 373 |
|
| 374 | impl Display for Error {
|
| 375 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
| 376 | formatter.write_str(&self.messages[0].message)
|
| 377 | }
|
| 378 | }
|
| 379 |
|
| 380 | impl Clone for Error {
|
| 381 | fn clone(&self) -> Self {
|
| 382 | Error {
|
| 383 | messages: self.messages.clone(),
|
| 384 | }
|
| 385 | }
|
| 386 | }
|
| 387 |
|
| 388 | impl Clone for ErrorMessage {
|
| 389 | fn clone(&self) -> Self {
|
| 390 | ErrorMessage {
|
| 391 | span: self.span,
|
| 392 | message: self.message.clone(),
|
| 393 | }
|
| 394 | }
|
| 395 | }
|
| 396 |
|
| 397 | impl Clone for SpanRange {
|
| 398 | fn clone(&self) -> Self {
|
| 399 | *self
|
| 400 | }
|
| 401 | }
|
| 402 |
|
| 403 | impl Copy for SpanRange {}
|
| 404 |
|
| 405 | impl std::error::Error for Error {}
|
| 406 |
|
| 407 | impl From<LexError> for Error {
|
| 408 | fn from(err: LexError) -> Self {
|
| 409 | Error::new(err.span(), err)
|
| 410 | }
|
| 411 | }
|
| 412 |
|
| 413 | impl IntoIterator for Error {
|
| 414 | type Item = Error;
|
| 415 | type IntoIter = IntoIter;
|
| 416 |
|
| 417 | fn into_iter(self) -> Self::IntoIter {
|
| 418 | IntoIter {
|
| 419 | messages: self.messages.into_iter(),
|
| 420 | }
|
| 421 | }
|
| 422 | }
|
| 423 |
|
| 424 | pub struct IntoIter {
|
| 425 | messages: vec::IntoIter<ErrorMessage>,
|
| 426 | }
|
| 427 |
|
| 428 | impl Iterator for IntoIter {
|
| 429 | type Item = Error;
|
| 430 |
|
| 431 | fn next(&mut self) -> Option<Self::Item> {
|
| 432 | Some(Error {
|
| 433 | messages: vec![self.messages.next()?],
|
| 434 | })
|
| 435 | }
|
| 436 | }
|
| 437 |
|
| 438 | impl<'a> IntoIterator for &'a Error {
|
| 439 | type Item = Error;
|
| 440 | type IntoIter = Iter<'a>;
|
| 441 |
|
| 442 | fn into_iter(self) -> Self::IntoIter {
|
| 443 | Iter {
|
| 444 | messages: self.messages.iter(),
|
| 445 | }
|
| 446 | }
|
| 447 | }
|
| 448 |
|
| 449 | pub struct Iter<'a> {
|
| 450 | messages: slice::Iter<'a, ErrorMessage>,
|
| 451 | }
|
| 452 |
|
| 453 | impl<'a> Iterator for Iter<'a> {
|
| 454 | type Item = Error;
|
| 455 |
|
| 456 | fn next(&mut self) -> Option<Self::Item> {
|
| 457 | Some(Error {
|
| 458 | messages: vec![self.messages.next()?.clone()],
|
| 459 | })
|
| 460 | }
|
| 461 | }
|
| 462 |
|
| 463 | impl Extend<Error> for Error {
|
| 464 | fn extend<T: IntoIterator<Item = Error>>(&mut self, iter: T) {
|
| 465 | for err in iter {
|
| 466 | self.combine(err);
|
| 467 | }
|
| 468 | }
|
| 469 | }
|