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

Remove eval from defcomp macro #761

Merged
merged 1 commit into from
Sep 12, 2020
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
16 changes: 5 additions & 11 deletions src/core/defcomp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ function add_variable(comp_id::ComponentId, name, datatype, dimensions, descript
end

function add_parameter(comp_def::ComponentDef, name, datatype, dimensions, description, unit, default)
if default !== nothing
if length(dimensions) != ndims(default)
error("Default value has different number of dimensions ($(ndims(dflt))) than parameter '$name' ($(length(dimensions)))")
end
Comment on lines +86 to +88
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah actually it is just the "end" line of the if statement that codecov is complaining about. But I was surprised that the error type for that error would be UndefVarError and not still a LoadError, so I think there might be a problem here that dflt isn't defined here and that it should be default?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, good catch! The test is now reporting a bug in the line that throws the error, i.e. in line 87 :) The test should actually test for an ErrorException, right? That is what is thrown, so any other type of error would mean something went wrong.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I was lazy when I changed that, I’ll change the error type back and correct dflt to default, I think that should solve it.

Copy link
Collaborator Author

@lrennels lrennels Sep 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but did we conclude that it should be a LoadError or ErrorException? It's now an ErrorException (see #762 ), but even with default instead of dflt. Is there a problem here because we're calling ndims on default before it is evaluated ... or has it been evaluated at this point?

Copy link
Collaborator

@corakingdon corakingdon Sep 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think David is right, it should now be ErrorException (it was previously LoadError when it was being thrown from within eval I think)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if you just run the part of the test file that's supposed to test this error, but run it outside of the test_throws then you can see if the error message is our constructed error message or if something else is wrong, just to be safe

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ckingdon95 smart yes I just did that and the constructed error message looks correct, so I think we can merge the PR now if someone can approve it

end
p = ParameterDef(name, comp_def.comp_path, datatype, dimensions, description, unit, default)
comp_def[name] = p # adds to namespace and checks for duplicate
dirty!(comp_def)
Expand Down Expand Up @@ -261,19 +266,8 @@ macro defcomp(comp_name, ex)

@debug " index $(Tuple(dimensions)), unit '$unit', desc '$desc'"

if dflt !== nothing
dflt = Base.eval(Main, dflt)
if length(dimensions) != ndims(dflt)
error("Default value has different number of dimensions ($(ndims(dflt))) than parameter '$name' ($(length(dimensions)))")
end
end

@debug "before: datum type is $datum_type, which is of type $(typeof(datum_type))"
datum_type = (datum_type === nothing ? :Number : datum_type)
@debug "after: datum type is $datum_type, which is of type $(typeof(datum_type))"

addexpr(_generate_var_or_param(elt_type, name, datum_type, dimensions, dflt, desc, unit))

else
error("Unrecognized element type: $elt_type")
end
Expand Down
4 changes: 2 additions & 2 deletions test/test_parametertypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ expr = :(
end
end
)
@test_throws LoadError eval(expr)
@test_throws UndefVarError eval(expr)

expr = :(
@defcomp BadComp2 begin
Expand All @@ -26,7 +26,7 @@ expr = :(
end
end
)
@test_throws LoadError eval(expr)
@test_throws UndefVarError eval(expr)

#
# Test that the old type parameterization syntax errors
Expand Down