Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't crash on AsTraitPath with empty path #6454

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,8 @@ impl Path {
self.last_segment().ident
}

pub fn first_name(&self) -> &str {
assert!(!self.segments.is_empty());
&self.segments.first().unwrap().ident.0.contents
pub fn first_name(&self) -> Option<&str> {
self.segments.first().map(|segment| segment.ident.0.contents.as_str())
}

pub fn last_name(&self) -> &str {
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/path_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<'context> Elaborator<'context> {
pub(super) fn resolve_path(&mut self, mut path: Path) -> PathResolutionResult {
let mut module_id = self.module_id();

if path.kind == PathKind::Plain && path.first_name() == SELF_TYPE_NAME {
if path.kind == PathKind::Plain && path.first_name() == Some(SELF_TYPE_NAME) {
if let Some(Type::Struct(struct_type, _)) = &self.self_type {
let struct_type = struct_type.borrow();
if path.segments.len() == 1 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@

// Resolve Self::Foo to an associated type on the current trait or trait impl
fn lookup_associated_type_on_self(&self, path: &Path) -> Option<Type> {
if path.segments.len() == 2 && path.first_name() == SELF_TYPE_NAME {
if path.segments.len() == 2 && path.first_name() == Some(SELF_TYPE_NAME) {
if let Some(trait_id) = self.current_trait {
let the_trait = self.interner.get_trait(trait_id);
if let Some(typ) = the_trait.get_associated_type(path.last_name()) {
Expand Down Expand Up @@ -420,7 +420,7 @@
// TODO(https://github.com/noir-lang/noir/issues/6238):
// support non-u32 generics here
if !kind.unifies(&Kind::u32()) {
let error = TypeCheckError::EvaluatedGlobalIsntU32 {

Check warning on line 423 in compiler/noirc_frontend/src/elaborator/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
expected_kind: Kind::u32().to_string(),
expr_kind: kind.to_string(),
expr_span: path.span(),
Expand Down
11 changes: 10 additions & 1 deletion compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@
}

pub(crate) fn get_program(src: &str) -> (ParsedModule, Context, Vec<(CompilationError, FileId)>) {
get_program_with_maybe_parser_errors(
src, false, // allow parser errors
)
}

pub(crate) fn get_program_with_maybe_parser_errors(
src: &str,
allow_parser_errors: bool,
) -> (ParsedModule, Context, Vec<(CompilationError, FileId)>) {
let root = std::path::Path::new("/");
let fm = FileManager::new(root);

Expand All @@ -73,7 +82,7 @@
let mut errors = vecmap(parser_errors, |e| (e.into(), root_file_id));
remove_experimental_warnings(&mut errors);

if !has_parser_error(&errors) {
if allow_parser_errors || !has_parser_error(&errors) {
let inner_attributes: Vec<SecondaryAttribute> = program
.items
.iter()
Expand Down Expand Up @@ -1988,7 +1997,7 @@
}

// TODO(https://github.com/noir-lang/noir/issues/6238):
// The EvaluatedGlobalIsntU32 warning is a stopgap

Check warning on line 2000 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
// (originally from https://github.com/noir-lang/noir/issues/6125)
#[test]
fn numeric_generic_field_larger_than_u32() {
Expand All @@ -2005,7 +2014,7 @@
assert_eq!(errors.len(), 2);
assert!(matches!(
errors[0].0,
CompilationError::TypeError(TypeCheckError::EvaluatedGlobalIsntU32 { .. }),

Check warning on line 2017 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
));
assert!(matches!(
errors[1].0,
Expand All @@ -2014,7 +2023,7 @@
}

// TODO(https://github.com/noir-lang/noir/issues/6238):
// The EvaluatedGlobalIsntU32 warning is a stopgap

Check warning on line 2026 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
// (originally from https://github.com/noir-lang/noir/issues/6126)
#[test]
fn numeric_generic_field_arithmetic_larger_than_u32() {
Expand Down Expand Up @@ -2043,7 +2052,7 @@

assert!(matches!(
errors[0].0,
CompilationError::TypeError(TypeCheckError::EvaluatedGlobalIsntU32 { .. }),

Check warning on line 2055 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
));

assert!(matches!(
Expand Down Expand Up @@ -2179,7 +2188,7 @@
assert_eq!(errors.len(), 3);

// TODO(https://github.com/noir-lang/noir/issues/6238):
// The EvaluatedGlobalIsntU32 warning is a stopgap

Check warning on line 2191 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Isnt)
assert!(matches!(
errors[0].0,
CompilationError::TypeError(TypeCheckError::EvaluatedGlobalIsntU32 { .. }),
Expand Down Expand Up @@ -3202,7 +3211,7 @@
}

#[test]
fn arithmetic_generics_canonicalization_deduplication_regression() {

Check warning on line 3214 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (canonicalization)
let source = r#"
struct ArrData<let N: u32> {
a: [Field; N],
Expand Down Expand Up @@ -3536,7 +3545,7 @@

#[test]
fn unconditional_recursion_fail() {
let srcs = vec![

Check warning on line 3548 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
r#"
fn main() {
main()
Expand Down Expand Up @@ -3598,7 +3607,7 @@
"#,
];

for src in srcs {

Check warning on line 3610 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
let errors = get_program_errors(src);
assert!(
!errors.is_empty(),
Expand All @@ -3617,7 +3626,7 @@

#[test]
fn unconditional_recursion_pass() {
let srcs = vec![

Check warning on line 3629 in compiler/noirc_frontend/src/tests.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (srcs)
r#"
fn main() {
if false { main(); }
Expand Down
18 changes: 17 additions & 1 deletion compiler/noirc_frontend/src/tests/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::hir::def_collector::dc_crate::CompilationError;
use crate::hir::resolution::errors::ResolverError;
use crate::tests::get_program_errors;
use crate::tests::{get_program_errors, get_program_with_maybe_parser_errors};

use super::assert_no_errors;

Expand Down Expand Up @@ -390,3 +390,19 @@ fn trait_bounds_which_are_dependent_on_generic_types_are_resolved_correctly() {
"#;
assert_no_errors(src);
}

#[test]
fn does_not_crash_on_as_trait_path_with_empty_path() {
let src = r#"
struct Foo {
x: <N>,
}

fn main() {}
"#;

let (_, _, errors) = get_program_with_maybe_parser_errors(
src, true, // allow parser errors
);
assert!(!errors.is_empty());
}
Loading