Skip to content

Commit

Permalink
sanity check that FQNs are valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Chriscbr committed Aug 22, 2024
1 parent 53f9e48 commit d0de029
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion libs/wingc/src/dtsify/snapshots/declarations.snap
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class ParentClass extends $stdlib.std.Resource {
});
}
}
class Child extends ParentClass {
class Child extends (globalThis.$ClassFactory.resolveType("rootpkg.ParentClass") ?? ParentClass) {
constructor($scope, $id, ) {
super($scope, $id);
}
Expand Down
17 changes: 16 additions & 1 deletion libs/wingc/src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1888,6 +1888,9 @@ pub struct TypeChecker<'a> {
/// The JSII type system
jsii_types: &'a mut TypeSystem,

// A sanity check to ensure we don't generate the same FQN multiple times.
generated_fqns: HashSet<String>,

is_in_mut_json: bool,

ctx: VisitContext,
Expand All @@ -1910,6 +1913,7 @@ impl<'a> TypeChecker<'a> {
file_graph,
library_roots,
jsii_imports,
generated_fqns: HashSet::new(),
is_in_mut_json: false,
ctx: VisitContext::new(),
}
Expand Down Expand Up @@ -4584,7 +4588,8 @@ It should primarily be used in preflight or in inflights that are guaranteed to
}
}

// Create the resource/class type and add it to the current environment (so class implementation can reference itself)
// Only public classes are guaranteed to be unique across the package and
// can be referenced by their fully qualified name.
let fqn = if ast_class.access == AccessModifier::Public {
let package_root = self
.library_roots
Expand All @@ -4596,6 +4601,16 @@ It should primarily be used in preflight or in inflights that are guaranteed to
None
};

// Check if the FQN is already used
if let Some(fqn) = &fqn {
if self.generated_fqns.contains(fqn) {
self.spanned_error(stmt, format!("The fully qualified name {} is already in use", fqn));
} else {
self.generated_fqns.insert(fqn.clone());
}
}

// Create the resource/class type and add it to the current environment (so class implementation can reference itself)
let class_spec = Class {
name: ast_class.name.clone(),
fqn,
Expand Down

0 comments on commit d0de029

Please sign in to comment.