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

feat: Add deprecated attribute #2041

Merged
merged 6 commits into from Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/ast/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl From<FunctionDefinition> for NoirFunction {
Some(Attribute::Foreign(_)) => FunctionKind::LowLevel,
Some(Attribute::Test) => FunctionKind::Normal,
Some(Attribute::Oracle(_)) => FunctionKind::Oracle,
None => FunctionKind::Normal,
Some(Attribute::Deprecated) | None => FunctionKind::Normal,
};

NoirFunction { def: fd, kind }
Expand Down
5 changes: 5 additions & 0 deletions crates/noirc_frontend/src/hir/type_check/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ pub enum TypeCheckError {
},
#[error("Cannot infer type of expression, type annotations needed before this point")]
TypeAnnotationsNeeded { span: Span },
#[error("use of deprecated function {name}")]
CallDeprecated { name: String, span: Span },
This conversation was marked as resolved.
Show resolved Hide resolved
#[error("{0}")]
ResolverError(ResolverError),
}
Expand Down Expand Up @@ -205,6 +207,9 @@ impl From<TypeCheckError> for Diagnostic {

Diagnostic::simple_error(message, String::new(), span)
}
TypeCheckError::CallDeprecated { span, .. } => {
Diagnostic::simple_warning(error.to_string(), String::new(), span)
}
}
}
}
27 changes: 27 additions & 0 deletions crates/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ use crate::{
use super::{errors::TypeCheckError, TypeChecker};

impl<'interner> TypeChecker<'interner> {
fn maybe_deprecated(&mut self, expr: &ExprId) {
let mut try_block = || -> Option<()> {
if let HirExpression::Ident(expr::HirIdent { location, id }) =
self.interner.expression(expr)
{
if let crate::node_interner::DefinitionKind::Function(func_id) =
self.interner.try_definition(id)?.kind
jfecher marked this conversation as resolved.
Show resolved Hide resolved
{
let meta = self.interner.function_meta(&func_id);
if let crate::token::Attribute::Deprecated = meta.attributes? {
let name = self.interner.definition_name(id);

self.errors.push(TypeCheckError::CallDeprecated {
name: name.to_string(),
span: location.span,
});
}
}
}

None
};

try_block();
}
/// Infers a type for a given expression, and return this type.
/// As a side-effect, this function will also remember this type in the NodeInterner
/// for the given expr_id key.
Expand Down Expand Up @@ -106,6 +131,8 @@ impl<'interner> TypeChecker<'interner> {
}
HirExpression::Index(index_expr) => self.check_index_expression(index_expr),
HirExpression::Call(call_expr) => {
self.maybe_deprecated(&call_expr.func);
jfecher marked this conversation as resolved.
Show resolved Hide resolved

let function = self.check_expression(&call_expr.func);
let args = vecmap(&call_expr.arguments, |arg| {
let typ = self.check_expression(arg);
Expand Down
30 changes: 11 additions & 19 deletions crates/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ pub enum Attribute {
Foreign(String),
Builtin(String),
Oracle(String),
Deprecated,
Test,
}

Expand All @@ -332,6 +333,7 @@ impl fmt::Display for Attribute {
Attribute::Builtin(ref k) => write!(f, "#[builtin({k})]"),
Attribute::Oracle(ref k) => write!(f, "#[oracle({k})]"),
Attribute::Test => write!(f, "#[test]"),
Attribute::Deprecated => write!(f, "#[deprecated]"),
}
}
}
Expand All @@ -345,29 +347,19 @@ impl Attribute {
.filter(|string_segment| !string_segment.is_empty())
.collect();

if word_segments.len() != 2 {
if word_segments.len() == 1 && word_segments[0] == "test" {
return Ok(Token::Attribute(Attribute::Test));
} else {
return Err(LexerErrorKind::MalformedFuncAttribute {
span,
found: word.to_owned(),
});
}
}
let attribute = match &word_segments[..] {
["foreign", name] => Attribute::Foreign(name.to_string()),
["builtin", name] => Attribute::Builtin(name.to_string()),
["oracle", name] => Attribute::Oracle(name.to_string()),
["deprecated"] => Attribute::Deprecated,

let attribute_type = word_segments[0];
let attribute_name = word_segments[1];

let tok = match attribute_type {
"foreign" => Token::Attribute(Attribute::Foreign(attribute_name.to_string())),
"builtin" => Token::Attribute(Attribute::Builtin(attribute_name.to_string())),
"oracle" => Token::Attribute(Attribute::Oracle(attribute_name.to_string())),
["test"] => Attribute::Test,
_ => {
return Err(LexerErrorKind::MalformedFuncAttribute { span, found: word.to_owned() })
}
};
Ok(tok)

Ok(Token::Attribute(attribute))
}

pub fn builtin(self) -> Option<String> {
Expand Down Expand Up @@ -399,7 +391,7 @@ impl AsRef<str> for Attribute {
Attribute::Foreign(string) => string,
Attribute::Builtin(string) => string,
Attribute::Oracle(string) => string,
Attribute::Test => "",
Attribute::Test | Attribute::Deprecated => "",
}
}
}
Expand Down