Skip to content

Commit

Permalink
Remove null argument issue
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 29, 2024
1 parent eba934e commit 6f33969
Show file tree
Hide file tree
Showing 20 changed files with 176 additions and 278 deletions.
365 changes: 133 additions & 232 deletions src/analyzer/expr/call/argument_analyzer.rs

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/analyzer/expr/echo_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::stmt_analyzer::AnalysisError;
use hakana_reflection_info::code_location::HPos;
use hakana_reflection_info::function_context::FunctionLikeIdentifier;
use hakana_reflection_info::functionlike_parameter::FunctionLikeParameter;
use hakana_reflection_info::t_atomic::TAtomic;
use hakana_reflection_info::t_union::TUnion;
use hakana_reflection_info::{StrId, EFFECT_IMPURE};
use hakana_type::{get_arraykey, get_mixed_any};
use oxidized::ast_defs::Pos;
Expand All @@ -22,7 +24,7 @@ pub(crate) fn analyze(
analysis_data: &mut FunctionAnalysisData,
context: &mut ScopeContext,
) -> Result<(), AnalysisError> {
let echo_param = FunctionLikeParameter::new(
let mut echo_param = FunctionLikeParameter::new(
"var".to_string(),
HPos::new(call_pos, *statements_analyzer.get_file_path(), None),
HPos::new(call_pos, *statements_analyzer.get_file_path(), None),
Expand All @@ -44,7 +46,7 @@ pub(crate) fn analyze(
argument_analyzer::verify_type(
statements_analyzer,
&arg_type.unwrap_or(Rc::new(get_mixed_any())),
&get_arraykey(false),
&TUnion::new(vec![TAtomic::TScalar, TAtomic::TNull]),
&FunctionLikeIdentifier::Function(StrId::ECHO),
i,
arg_expr,
Expand Down
9 changes: 1 addition & 8 deletions src/analyzer/function_analysis_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,11 @@ impl FunctionAnalysisData {
| IssueKind::MixedPropertyTypeCoercion
| IssueKind::PropertyTypeCoercion
| IssueKind::NonNullableReturnType
| IssueKind::NullArgument
| IssueKind::NullablePropertyAssignment
| IssueKind::NullableReturnStatement
| IssueKind::NullableReturnValue
| IssueKind::PossiblyFalseArgument
| IssueKind::PossiblyInvalidArgument
| IssueKind::PossiblyNullArgument
| IssueKind::InvalidPropertyAssignmentValue
| IssueKind::LessSpecificNestedAnyReturnStatement
| IssueKind::LessSpecificNestedAnyArgumentType => {
Expand Down Expand Up @@ -296,19 +294,14 @@ impl FunctionAnalysisData {
}
_ => {}
},
4323 => {
if let IssueKind::PossiblyNullArgument = &issue_kind {
return true;
}
}
4063 => match &issue_kind {
IssueKind::MixedArrayAccess | IssueKind::PossiblyNullArrayAccess => {
return true;
}
_ => {}
},
4064 => match &issue_kind {
IssueKind::PossiblyNullArgument | IssueKind::PossiblyNullPropertyFetch => {
IssueKind::PossiblyNullPropertyFetch => {
return true;
}
_ => {}
Expand Down
2 changes: 0 additions & 2 deletions src/code_info/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ pub enum IssueKind {
NoJoinInAsyncFunction,
NonNullableReturnType,
NothingReturn,
NullArgument,
NullArrayOffset,
NullIterator,
NullablePropertyAssignment,
Expand All @@ -86,7 +85,6 @@ pub enum IssueKind {
PossiblyInvalidArgument,
PossiblyInvalidArrayAccess,
PossiblyInvalidMethodCall,
PossiblyNullArgument,
PossiblyNullArrayAccess,
PossiblyNullArrayOffset,
PossiblyNullIterator,
Expand Down
24 changes: 13 additions & 11 deletions src/ttype/type_comparator/atomic_type_comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,20 @@ pub fn is_contained_by(
return false;
}

if matches!(input_type_part, TAtomic::TNull { .. }) {
return false;
}
if input_type_part.is_some_scalar() {
if let TAtomic::TScalar = container_type_part {
return true;
}

if input_type_part.is_some_scalar() && container_type_part.is_some_scalar() {
return scalar_type_comparator::is_contained_by(
codebase,
input_type_part,
container_type_part,
inside_assertion,
atomic_comparison_result,
);
if container_type_part.is_some_scalar() {
return scalar_type_comparator::is_contained_by(
codebase,
input_type_part,
container_type_part,
inside_assertion,
atomic_comparison_result,
);
}
}

if let TAtomic::TNamedObject {
Expand Down
2 changes: 1 addition & 1 deletion tests/diff/classConstTypeChanged/output.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ERROR: InvalidArgument - input.hack:13:14 - Argument 1 of echo expects arraykey, different type Exception provided
ERROR: InvalidArgument - input.hack:13:14 - Argument 1 of echo expects scalar|null, different type Exception provided
4 changes: 2 additions & 2 deletions tests/diff/commentOutClassInOtherFile/output.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ERROR: NonExistentClass - input.hack:8:11 - Cannot call new on undefined class A
ERROR: NonExistentType - input.hack:12:10 - Unknown class A
ERROR: MixedAnyArgument - input.hack:12:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:12:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: NonExistentType - input.hack:17:14 - Unknown class A
ERROR: MixedAnyArgument - input.hack:17:14 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:17:14 - Argument 1 of echo expects scalar|null, any provided
ERROR: NonExistentClass - input.hack:21:15 - Cannot call new on undefined class A
ERROR: InvalidReturnStatement - input.hack:26:16 - The type string(a) does not match the declared return type int for B::bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ERROR: PossiblyUndefinedStringArrayOffset - input.hack:6:10 - Fetch on dict<arraykey,any> using possibly-undefined key 'foo'
ERROR: MixedAnyArgument - input.hack:6:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:6:10 - Argument 1 of echo expects scalar|null, any provided
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
function foo(dict<string, mixed> $args): void {
$a = idx($args, 'a', null);
$b = idx($args, 'b');
if ($a === null) {}
if ($b === null) {}
}
4 changes: 4 additions & 0 deletions tests/inference/FunctionCall/idxMixedImplicitNull/input.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function foo(dict<string, mixed> $args): void {
$b = idx($args, 'b');
if ($b === null) {}
}
2 changes: 1 addition & 1 deletion tests/inference/FunctionCall/nullArgument/output.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
NullArgument
InvalidArgument
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ERROR: InvalidArgument - input.hack:21:10 - Argument 1 of echo expects arraykey, different type Node provided
ERROR: InvalidArgument - input.hack:21:10 - Argument 1 of echo expects scalar|null, different type Node provided
2 changes: 1 addition & 1 deletion tests/inference/MethodCall/dateTimeNullFirstArg/output.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
NullArgument
InvalidArgument
Original file line number Diff line number Diff line change
@@ -1 +1 @@
PossiblyNullArgument
PossiblyInvalidArgument
2 changes: 1 addition & 1 deletion tests/inference/TypeAlias/classConstTypeAlias/output.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ERROR: InvalidArgument - input.hack:11:14 - Argument 1 of echo expects arraykey, different type Exception provided
ERROR: InvalidArgument - input.hack:11:14 - Argument 1 of echo expects scalar|null, different type Exception provided
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ERROR: InvalidArgument - input.hack:23:14 - Argument 1 of echo expects arraykey, different type Exception provided
ERROR: InvalidArgument - input.hack:23:14 - Argument 1 of echo expects scalar|null, different type Exception provided
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ERROR: MixedAnyArrayAccess - input.hack:3:10 - Unsafe array access on value with type any
ERROR: MixedAnyArrayAccess - input.hack:4:10 - Unsafe array access on value with type nonnull-from-any
ERROR: MixedAnyArgument - input.hack:4:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:4:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: MixedAnyReturnStatement - input.hack:5:12 - Could not infer a proper return type — saw nonnull-from-any
10 changes: 5 additions & 5 deletions tests/nopanic/noclass/output.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
ERROR: NonExistentClass - input.hack:2:12 - Cannot call new on undefined class Foo
ERROR: MixedAnyReturnStatement - input.hack:2:12 - Could not infer a proper return type — saw any
ERROR: NonExistentClasslike - input.hack:6:10 - Unknown classlike Foo
ERROR: MixedAnyArgument - input.hack:6:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:6:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: NonExistentClasslike - input.hack:9:12 - Class, enum or interface Foo cannot be found
ERROR: NonExistentClasslike - input.hack:13:10 - Unknown classlike Foo
ERROR: MixedAnyArgument - input.hack:13:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:13:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: NonExistentClasslike - input.hack:23:5 - Unknown classlike Foo
ERROR: NonExistentClass - input.hack:28:10 - Cannot access property on undefined class Foo
ERROR: MixedAnyArgument - input.hack:28:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:28:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: NonExistentClass - input.hack:33:10 - Cannot access property on undefined class Foo
ERROR: InvalidArgument - input.hack:33:10 - Argument 1 of echo expects arraykey, different type unknown-ref(Foo) provided
ERROR: InvalidArgument - input.hack:33:10 - Argument 1 of echo expects scalar|null, different type unknown-ref(Foo) provided
ERROR: NonExistentClass - input.hack:37:10 - Cannot access property on undefined class Foo
ERROR: MixedAnyArgument - input.hack:37:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:37:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: NonExistentClass - input.hack:42:9 - Undefined class Foo
8 changes: 4 additions & 4 deletions tests/nopanic/noclasslikemember/output.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
ERROR: NonExistentClassConstant - input.hack:8:10 - Unknown class constant Foo::B1
ERROR: MixedAnyArgument - input.hack:8:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:8:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: NonExistentClassConstant - input.hack:16:5 - Unknown class constant Foo::B3
ERROR: NonExistentClassConstant - input.hack:20:10 - Unknown class constant Foo::B4
ERROR: MixedAnyArgument - input.hack:20:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:20:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: NonExistentMethod - input.hack:24:5 - Method Foo::b5 does not exist
ERROR: NonExistentMethod - input.hack:28:5 - Method Foo::b6 does not exist
ERROR: NonExistentProperty - input.hack:32:10 - Cannot access undefined property Foo::$b7
ERROR: MixedAnyArgument - input.hack:32:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:32:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: NonExistentProperty - input.hack:36:10 - Cannot access undefined property Foo::$b8
ERROR: InvalidArgument - input.hack:36:10 - Argument 1 of echo expects arraykey, different type Foo provided
ERROR: InvalidArgument - input.hack:36:10 - Argument 1 of echo expects scalar|null, different type Foo provided
ERROR: NonExistentProperty - input.hack:40:10 - Property Foo::$b9 is undefined
ERROR: NonExistentProperty - input.hack:44:9 - Undefined property Foo::$b10
2 changes: 1 addition & 1 deletion tests/nopanic/nofunction/output.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ERROR: NonExistentFunction - input.hack:2:5 - Function f1 is not defined
ERROR: NonExistentFunction - input.hack:6:10 - Unknown function f2
ERROR: MixedAnyArgument - input.hack:6:10 - Argument 1 of echo expects arraykey, any provided
ERROR: MixedAnyArgument - input.hack:6:10 - Argument 1 of echo expects scalar|null, any provided
ERROR: NonExistentFunction - input.hack:10:5 - Function f3 is not defined

0 comments on commit 6f33969

Please sign in to comment.