wesl 0.3.2

The WESL compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use std::fmt::Display;

use wgsl_parse::{
    span::Span,
    syntax::{Expression, Ident, ModulePath},
};

use crate::{Mangler, ResolveError, SourceMap, ValidateError};

#[cfg(feature = "generics")]
use crate::GenericsError;

use crate::CondCompError;
use crate::ImportError;

#[cfg(feature = "eval")]
use crate::eval::{Context, EvalError};

/// Any WESL error.
#[derive(Clone, Debug, thiserror::Error)]
pub enum Error {
    #[error("{0}")]
    ParseError(#[from] wgsl_parse::Error),
    #[error("{0}")]
    ValidateError(#[from] ValidateError),
    #[error("{0}")]
    ResolveError(#[from] ResolveError),
    #[error("{0}")]
    ImportError(#[from] ImportError),
    #[error("{0}")]
    CondCompError(#[from] CondCompError),
    #[cfg(feature = "generics")]
    #[error("{0}")]
    GenericsError(#[from] GenericsError),
    #[cfg(feature = "eval")]
    #[error("{0}")]
    EvalError(#[from] EvalError),
    #[error("{0}")]
    Error(#[from] Diagnostic<Error>),
    #[error("{0}")]
    Custom(String),
}

/// Error diagnostics. Display user-friendly error snippets with `Display`.
///
/// A diagnostic is a wrapper around an error with extra contextual metadata: the source,
/// the declaration name, the span, ...
#[derive(Clone, Debug)]
pub struct Diagnostic<E: std::error::Error> {
    pub error: Box<E>,
    pub detail: Box<Detail>,
}

#[derive(Clone, Debug)]
pub struct Detail {
    pub source: Option<String>,
    pub output: Option<String>,
    pub module_path: Option<ModulePath>,
    pub display_name: Option<String>,
    pub declaration: Option<String>,
    pub span: Option<Span>,
}

impl From<wgsl_parse::Error> for Diagnostic<Error> {
    fn from(error: wgsl_parse::Error) -> Self {
        let span = error.span;
        let mut res = Self::new(Error::ParseError(error));
        res.detail.span = Some(span);
        res
    }
}

impl From<ValidateError> for Diagnostic<Error> {
    fn from(error: ValidateError) -> Self {
        Self::new(error.into())
    }
}

impl From<ResolveError> for Diagnostic<Error> {
    fn from(error: ResolveError) -> Self {
        match error {
            ResolveError::FileNotFound(_, _) | ResolveError::ModuleNotFound(_, _) => {
                Self::new(error.into())
            }
            ResolveError::Error(e) => e,
        }
    }
}

impl From<ImportError> for Diagnostic<Error> {
    fn from(error: ImportError) -> Self {
        match error {
            ImportError::ResolveError(e) => Self::from(e),
            _ => Self::new(error.into()),
        }
    }
}

impl From<CondCompError> for Diagnostic<Error> {
    fn from(error: CondCompError) -> Self {
        Self::new(error.into())
    }
}

#[cfg(feature = "generics")]
impl From<GenericsError> for Diagnostic<Error> {
    fn from(error: GenericsError) -> Self {
        Self::new(error.into())
    }
}

#[cfg(feature = "eval")]
impl From<EvalError> for Diagnostic<Error> {
    fn from(error: EvalError) -> Self {
        Self::new(error.into())
    }
}

impl From<Error> for Diagnostic<Error> {
    fn from(error: Error) -> Self {
        match error {
            Error::ParseError(e) => e.into(),
            Error::ResolveError(e) => e.into(),
            Error::ImportError(e) => e.into(),
            Error::Error(e) => e,
            error => Self::new(error),
        }
    }
}

impl<E: std::error::Error> Diagnostic<E> {
    /// Create an empty diagnostic from an error. No metadata is attached.
    fn new(error: E) -> Diagnostic<E> {
        Self {
            error: Box::new(error),
            detail: Box::new(Detail {
                source: None,
                output: None,
                module_path: None,
                display_name: None,
                declaration: None,
                span: None,
            }),
        }
    }
    /// Provide the source code from which the error was emitted.
    /// You should also provide the span with [`Self::with_span`].
    pub fn with_source(mut self, source: String) -> Self {
        if self.detail.source.is_none() {
            self.detail.source = Some(source);
        }
        self
    }
    /// Provide the span (chunk of source code) where the error originated.
    /// You should also provide the source with [`Self::with_source`].
    /// Subsequent calls to this function do not override the span.
    pub fn with_span(mut self, span: Span) -> Self {
        if self.detail.span.is_none() {
            self.detail.span = Some(span);
        }
        self
    }
    /// Provide the declaration in which the error originated.
    pub fn with_declaration(mut self, decl: String) -> Self {
        if self.detail.declaration.is_none() {
            self.detail.declaration = Some(decl);
        }
        self
    }
    /// Provide the output code that was generated, even if an error was emitted.
    pub fn with_output(mut self, out: String) -> Self {
        if self.detail.output.is_none() {
            self.detail.output = Some(out);
        }
        self
    }
    /// Provide the module path in which the error was emitted. The `disp_name` is
    /// usually the file name of the module.
    pub fn with_module_path(mut self, path: ModulePath, disp_name: Option<String>) -> Self {
        if self.detail.module_path.is_none() {
            self.detail.module_path = Some(path);
            self.detail.display_name = disp_name;
        }
        self
    }
    /// Add metadata collected by the evaluation/execution context.
    #[cfg(feature = "eval")]
    pub fn with_ctx(mut self, ctx: &Context) -> Self {
        let (decl, span) = ctx.err_ctx();
        self.detail.declaration = decl.map(|id| id.to_string());
        self.detail.span = span;
        self
    }

    /// Add metadata collected by the sourcemap. If the mangled declaration name was set,
    /// this will automatically add the source, the module path and the declaration name.
    pub fn with_sourcemap(mut self, sourcemap: &impl SourceMap) -> Self {
        if let Some(decl) = &self.detail.declaration {
            if let Some((path, decl)) = sourcemap.get_decl(decl) {
                self.detail.module_path = Some(path.clone());
                self.detail.declaration = Some(decl.to_string());
                self.detail.display_name = sourcemap
                    .get_display_name(path)
                    .map(|name| name.to_string());
                self.detail.source = sourcemap
                    .get_source(path)
                    .map(|s| s.to_string())
                    .or(self.detail.source);
            }
        }

        if self.detail.source.is_none() {
            if let Some(path) = &self.detail.module_path {
                self.detail.source = sourcemap.get_source(path).map(|s| s.to_string());
            } else {
                self.detail.source = sourcemap.get_default_source().map(|s| s.to_string());
            }
        }

        self
    }

    pub(crate) fn display_origin(&self) -> String {
        match (&self.detail.module_path, &self.detail.display_name) {
            (Some(res), Some(name)) => {
                format!("{res} ({name})")
            }
            (Some(res), None) => res.to_string(),
            (None, Some(name)) => name.to_string(),
            (None, None) => "unknown module".to_string(),
        }
    }

    pub(crate) fn display_short_origin(&self) -> Option<String> {
        self.detail
            .display_name
            .clone()
            .or_else(|| self.detail.module_path.as_ref().map(|res| res.to_string()))
    }
}

impl Diagnostic<Error> {
    // XXX: this function has issues when the root module identifiers are not mangled.
    /// unmangle any mangled identifiers in the error.
    ///
    /// The mangled must be the same used for compiling the WGSL source. It must have
    /// unmangling capabilities. If not, you might want to use a [`crate::SourceMapper`].
    pub fn unmangle(
        mut self,
        sourcemap: Option<&impl SourceMap>,
        mangler: Option<&impl Mangler>,
    ) -> Self {
        fn unmangle_id(
            id: &mut Ident,
            sourcemap: Option<&impl SourceMap>,
            mangler: Option<&impl Mangler>,
        ) {
            let res_name = if let Some(sourcemap) = sourcemap {
                sourcemap
                    .get_decl(&id.name())
                    .map(|(res, name)| (res.clone(), name.to_string()))
            } else if let Some(mangler) = mangler {
                mangler.unmangle(&id.name())
            } else {
                None
            };
            if let Some((res, name)) = res_name {
                *id = Ident::new(format!("{res}::{name}"));
            }
        }

        fn unmangle_name(
            mangled: &mut String,
            sourcemap: Option<&impl SourceMap>,
            mangler: Option<&impl Mangler>,
        ) {
            let res_name = if let Some(sourcemap) = sourcemap {
                sourcemap
                    .get_decl(mangled)
                    .map(|(res, name)| (res.clone(), name.to_string()))
            } else if let Some(mangler) = mangler {
                mangler.unmangle(mangled)
            } else {
                None
            };
            if let Some((res, name)) = res_name {
                *mangled = format!("{res}::{name}");
            }
        }

        fn unmangle_expr(
            expr: &mut Expression,
            sourcemap: Option<&impl SourceMap>,
            mangler: Option<&impl Mangler>,
        ) {
            match expr {
                Expression::Literal(_) => {}
                Expression::Parenthesized(e) => {
                    unmangle_expr(&mut e.expression, sourcemap, mangler)
                }
                Expression::NamedComponent(e) => unmangle_expr(&mut e.base, sourcemap, mangler),
                Expression::Indexing(e) => unmangle_expr(&mut e.base, sourcemap, mangler),
                Expression::Unary(e) => unmangle_expr(&mut e.operand, sourcemap, mangler),
                Expression::Binary(e) => {
                    unmangle_expr(&mut e.left, sourcemap, mangler);
                    unmangle_expr(&mut e.right, sourcemap, mangler);
                }
                Expression::FunctionCall(e) => {
                    unmangle_id(&mut e.ty.ident, sourcemap, mangler);
                    for arg in &mut e.arguments {
                        unmangle_expr(arg, sourcemap, mangler);
                    }
                }
                Expression::TypeOrIdentifier(ty) => unmangle_id(&mut ty.ident, sourcemap, mangler),
            }
        }

        #[cfg(feature = "eval")]
        fn unmangle_ty(
            mangled: &mut wgsl_types::ty::Type,
            sourcemap: Option<&impl SourceMap>,
            mangler: Option<&impl Mangler>,
        ) {
            use wgsl_types::ty::Type;
            match mangled {
                // TODO unmangle components!
                Type::Struct(s) => {
                    unmangle_name(&mut s.name, sourcemap, mangler);
                    for m in s.members.iter_mut() {
                        unmangle_ty(&mut m.ty, sourcemap, mangler);
                    }
                }
                Type::Array(ty, _) => unmangle_ty(&mut *ty, sourcemap, mangler),
                Type::Atomic(ty) => unmangle_ty(&mut *ty, sourcemap, mangler),
                Type::Ptr(_, ty, _) => unmangle_ty(&mut *ty, sourcemap, mangler),
                Type::Ref(_, ty, _) => unmangle_ty(&mut *ty, sourcemap, mangler),
                _ => (),
            }
        }

        #[cfg(feature = "eval")]
        fn unmangle_inst(
            mangled: &mut wgsl_types::inst::Instance,
            sourcemap: Option<&impl SourceMap>,
            mangler: Option<&impl Mangler>,
        ) {
            use wgsl_types::inst::Instance;
            match mangled {
                Instance::Struct(inst) => {
                    unmangle_name(&mut inst.ty.name, sourcemap, mangler);
                    for inst in inst.members.iter_mut() {
                        unmangle_inst(inst, sourcemap, mangler);
                    }
                }
                Instance::Array(inst) => {
                    for c in inst.iter_mut() {
                        unmangle_inst(c, sourcemap, mangler);
                    }
                }
                Instance::Ptr(inst) => {
                    unmangle_ty(&mut inst.ptr.ty, sourcemap, mangler);
                }
                Instance::Ref(inst) => {
                    unmangle_ty(&mut inst.ty, sourcemap, mangler);
                }
                Instance::Atomic(inst) => {
                    unmangle_inst(inst.inner_mut(), sourcemap, mangler);
                }
                Instance::Deferred(ty) => unmangle_ty(ty, sourcemap, mangler),
                Instance::Literal(_) | Instance::Vec(_) | Instance::Mat(_) => {}
            }
        }

        match &mut *self.error {
            Error::ParseError(_) => {}
            Error::ValidateError(e) => match e {
                ValidateError::UndefinedSymbol(name)
                | ValidateError::ParamCount(name, _, _)
                | ValidateError::NotCallable(name)
                | ValidateError::Duplicate(name) => unmangle_name(name, sourcemap, mangler),
                ValidateError::Cycle(name1, name2) => {
                    unmangle_name(name1, sourcemap, mangler);
                    unmangle_name(name2, sourcemap, mangler);
                }
            },
            Error::ResolveError(_) => {}
            Error::ImportError(_) => {}
            Error::CondCompError(e) => match e {
                CondCompError::InvalidExpression(expr) => unmangle_expr(expr, sourcemap, mangler),
                CondCompError::InvalidFeatureFlag(_)
                | CondCompError::UnexpectedFeatureFlag(_)
                | CondCompError::NoPrecedingIf
                | CondCompError::DuplicateIf => {}
            },
            #[cfg(feature = "generics")]
            Error::GenericsError(_) => {}
            #[cfg(feature = "eval")]
            Error::EvalError(e) => match e {
                EvalError::NotScalar(ty) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::NotConstructible(ty) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::Type(ty1, ty2) => {
                    unmangle_ty(ty1, sourcemap, mangler);
                    unmangle_ty(ty2, sourcemap, mangler);
                }
                EvalError::SampledType(ty) => {
                    unmangle_ty(ty, sourcemap, mangler);
                }
                EvalError::NotType(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::UnknownType(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::UnknownStruct(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::NotAccessible(name, _) => unmangle_name(name, sourcemap, mangler),
                EvalError::UnexpectedTemplate(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::View(ty, _) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::RefType(ty1, ty2) => {
                    unmangle_ty(ty1, sourcemap, mangler);
                    unmangle_ty(ty2, sourcemap, mangler);
                }
                EvalError::WriteRefType(ty1, ty2) => {
                    unmangle_ty(ty1, sourcemap, mangler);
                    unmangle_ty(ty2, sourcemap, mangler);
                }
                EvalError::Conversion(ty1, ty2) => {
                    unmangle_ty(ty1, sourcemap, mangler);
                    unmangle_ty(ty2, sourcemap, mangler);
                }
                EvalError::ConvOverflow(_, ty) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::Component(ty, _) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::Index(ty) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::NotIndexable(ty) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::OutOfBounds(_, ty, _) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::Unary(_, ty) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::Binary(_, ty1, ty2) => {
                    unmangle_ty(ty1, sourcemap, mangler);
                    unmangle_ty(ty2, sourcemap, mangler);
                }
                EvalError::CompwiseBinary(ty1, ty2) => {
                    unmangle_ty(ty1, sourcemap, mangler);
                    unmangle_ty(ty2, sourcemap, mangler);
                }
                EvalError::UnknownFunction(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::NotCallable(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::Signature(sig) => {
                    unmangle_name(&mut sig.name, sourcemap, mangler);
                    for tplt in sig.tplt.iter_mut().flatten() {
                        match tplt {
                            wgsl_types::tplt::TpltParam::Type(ty) => {
                                unmangle_ty(ty, sourcemap, mangler)
                            }
                            wgsl_types::tplt::TpltParam::Instance(inst) => {
                                unmangle_inst(inst, sourcemap, mangler)
                            }
                            wgsl_types::tplt::TpltParam::Enumerant(_) => {}
                        }
                    }
                    for arg in &mut sig.args {
                        unmangle_ty(arg, sourcemap, mangler);
                    }
                }
                EvalError::ParamCount(name, _, _) => unmangle_name(name, sourcemap, mangler),
                EvalError::ParamType(ty1, ty2) => {
                    unmangle_ty(ty1, sourcemap, mangler);
                    unmangle_ty(ty2, sourcemap, mangler);
                }
                EvalError::ReturnType(ty1, name, ty2) => {
                    unmangle_ty(ty1, sourcemap, mangler);
                    unmangle_name(name, sourcemap, mangler);
                    unmangle_ty(ty2, sourcemap, mangler);
                }
                EvalError::NoReturn(name, ty) => {
                    unmangle_name(name, sourcemap, mangler);
                    unmangle_ty(ty, sourcemap, mangler);
                }
                EvalError::UnexpectedReturn(name, ty) => {
                    unmangle_name(name, sourcemap, mangler);
                    unmangle_ty(ty, sourcemap, mangler);
                }
                EvalError::NotConst(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::Void(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::MustUse(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::NotEntrypoint(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::UnknownDecl(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::UninitConst(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::UninitLet(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::UninitOverride(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::DuplicateDecl(name) => unmangle_name(name, sourcemap, mangler),
                EvalError::AssignType(ty1, ty2) => {
                    unmangle_ty(ty1, sourcemap, mangler);
                    unmangle_ty(ty2, sourcemap, mangler);
                }
                EvalError::IncrType(ty) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::DecrType(ty) => unmangle_ty(ty, sourcemap, mangler),
                EvalError::ConstAssertFailure(expr) => unmangle_expr(expr, sourcemap, mangler),
                EvalError::Todo(_)
                | EvalError::MissingTemplate(_)
                | EvalError::NotWrite
                | EvalError::NotRead
                | EvalError::NotReadWrite
                | EvalError::PtrHandle
                | EvalError::PtrVecComp
                | EvalError::Swizzle(_)
                | EvalError::NegOverflow
                | EvalError::AddOverflow
                | EvalError::SubOverflow
                | EvalError::MulOverflow
                | EvalError::DivByZero
                | EvalError::RemZeroDiv
                | EvalError::ShlOverflow(_, _)
                | EvalError::ShrOverflow(_, _)
                | EvalError::Builtin(_)
                | EvalError::TemplateArgs(_)
                | EvalError::InvalidEntrypointParam(_)
                | EvalError::MissingBuiltinInput(_, _)
                | EvalError::OutputBuiltin(_)
                | EvalError::InputBuiltin(_)
                | EvalError::MissingUserInput(_, _)
                | EvalError::OverrideInConst
                | EvalError::OverrideInFn
                | EvalError::LetInMod
                | EvalError::ForbiddenInitializer(_)
                | EvalError::UntypedDecl
                | EvalError::ForbiddenDecl(_, _)
                | EvalError::MissingResource(_, _)
                | EvalError::AddressSpace(_, _)
                | EvalError::AccessMode(_, _)
                | EvalError::MissingBindAttr
                | EvalError::MissingWorkgroupSize
                | EvalError::NegativeAttr(_)
                | EvalError::InvalidBlendSrc(_)
                | EvalError::NotRef(_)
                | EvalError::IncrOverflow
                | EvalError::DecrOverflow
                | EvalError::FlowInContinuing(_)
                | EvalError::DiscardInConst
                | EvalError::FlowInFunction(_)
                | EvalError::FlowInModule(_) => {}
            },
            Error::Error(_) => {}
            Error::Custom(_) => {}
        };

        self
    }
}

impl<E: std::error::Error> std::error::Error for Diagnostic<E> {}

impl<E: std::error::Error> Display for Diagnostic<E> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        use annotate_snippets::*;
        let msg = format!("{}", self.error);
        let title = Level::ERROR.primary_title(&msg);
        let mut group = Group::with_title(title);

        let orig = self.display_origin();
        let short_orig = self.display_short_origin();

        if let Some(span) = &self.detail.span {
            let source = self.detail.source.as_deref();

            if let Some(source) = source {
                if span.range().end <= source.len() {
                    let annot = AnnotationKind::Primary.span(span.range()).label(&msg);
                    let mut snip = Snippet::source(source).fold(true).annotation(annot);

                    if let Some(orig) = &short_orig {
                        snip = snip.path(orig);
                    }

                    group = group.element(snip);
                } else {
                    group = group.element(
                        Level::NOTE.message("cannot display snippet: invalid source location"),
                    )
                }
            } else {
                group = group
                    .element(Level::NOTE.message("cannot display snippet: missing source file"))
            }
        }

        let note;
        if let Some(decl) = &self.detail.declaration {
            note = format!("in declaration of `{decl}` in {orig}");
        } else {
            note = format!("in {orig}");
        }
        let group = group.element(Level::NOTE.message(&note));

        let renderer = Renderer::styled();
        let rendered = renderer.render(&[group]);
        write!(f, "{rendered}")
    }
}